| 
									
										
										
										
											2022-09-04 11:35:13 +01:00
										 |  |  | using Godot; | 
					
						
							|  |  |  | using System; | 
					
						
							|  |  |  | using Vim.Math3d; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 00:41:32 +01:00
										 |  |  | public struct Ellipse : IEllipse | 
					
						
							| 
									
										
										
										
											2022-09-04 11:35:13 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     public Ellipse(double a = 1, double e = 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         SemiMajorAxis = a; | 
					
						
							|  |  |  |         Eccentricity = e; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // TODO: this is an immutable struct, so initialise everything else  | 
					
						
							|  |  |  |         // in the constructor to avoid recalculating properties whenever | 
					
						
							|  |  |  |         // they are accessed | 
					
						
							| 
									
										
										
										
											2022-09-07 00:41:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Apisides = new Apisides(a, e); | 
					
						
							|  |  |  |         b = Math.Sqrt(Apisides.max * Apisides.min); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var d = Math.Sqrt(a * a - b * b); | 
					
						
							|  |  |  |         Foci = new DVector2[] | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             new DVector2(-d, 0), | 
					
						
							|  |  |  |             new DVector2(d, 0) | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2022-09-04 11:35:13 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// <summary> | 
					
						
							|  |  |  |     /// Semi-major axis | 
					
						
							|  |  |  |     /// </summary> | 
					
						
							|  |  |  |     public double a => SemiMajorAxis; | 
					
						
							|  |  |  |     public double SemiMajorAxis { get; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// <summary> | 
					
						
							|  |  |  |     /// Eccentricity | 
					
						
							|  |  |  |     /// </summary> | 
					
						
							|  |  |  |     public double e => Eccentricity; | 
					
						
							|  |  |  |     public double Eccentricity { get; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /// <summary> | 
					
						
							|  |  |  |     /// Semi-minor axis | 
					
						
							|  |  |  |     /// </summary> | 
					
						
							| 
									
										
										
										
											2022-09-07 00:41:32 +01:00
										 |  |  |     public double b { get; } | 
					
						
							| 
									
										
										
										
											2022-09-04 11:35:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /// <summary> | 
					
						
							|  |  |  |     /// Get a position on the auxiliary circle | 
					
						
							|  |  |  |     /// </summary> | 
					
						
							|  |  |  |     /// <param name="t">Angle in radians around the circle</param> | 
					
						
							|  |  |  |     /// <returns>2D position on the circle scaled by the semi-major axis</returns> | 
					
						
							|  |  |  |     public DVector2 GetAuxiliaryPosition2D(double t = 0) => | 
					
						
							|  |  |  |         new DVector2(Math.Cos(t), Math.Sin(t)) * a; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 00:41:32 +01:00
										 |  |  |     private Apisides Apisides { get; } | 
					
						
							| 
									
										
										
										
											2022-09-04 11:35:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 00:41:32 +01:00
										 |  |  |     public DVector2[] Foci { get; } | 
					
						
							| 
									
										
										
										
											2022-09-04 11:35:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private double GetFocusDistance() => Math.Sqrt(a * a - b * b); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public struct Apisides | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public readonly double min; | 
					
						
							|  |  |  |     public readonly double max; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public Apisides(double a, double e) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         min = a * (1 - e); | 
					
						
							|  |  |  |         max = a * (1 + e); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |