PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/SolarSystem/SolarSystemBodyFactory.cs

https://bitbucket.org/efouts/solarsystem
C# | 46 lines | 40 code | 6 blank | 0 comment | 0 complexity | f168fe4096fa316e66d33e6824bb5ad9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MathNet.Numerics.LinearAlgebra.Double;
  6. namespace SolarSystem
  7. {
  8. public class SolarSystemBodyFactory
  9. {
  10. private static readonly Double MassOfSun = 1.9891 * Math.Pow(10, 30);
  11. public static Body CreateSun()
  12. {
  13. return new Body()
  14. {
  15. Name = "Sun",
  16. Radius = 6.96342 * Math.Pow(10, 5),
  17. Mass = 1.9891 * Math.Pow(10, 30),
  18. Position = new DenseVector(new[] { 0d, 0d }),
  19. Velocity = new DenseVector(new[] { 0d, 0d }),
  20. Rotation = 0,
  21. RotationVelocity = 0
  22. };
  23. }
  24. public static Body CreateBody(String name, Double radius, Double mass, Double semiMajorAxis, Double eccentricity, Double rotationVelocity)
  25. {
  26. var position = new DenseVector(new[] { GetDistanceAtAphelion(semiMajorAxis, eccentricity), 0 });
  27. var velocity = new DenseVector(new[] { 0, GetVelocityAtAphelion(semiMajorAxis, eccentricity) });
  28. return new Body() { Name = name, Radius = radius, Position = position, Velocity = velocity, Rotation = 0, RotationVelocity = rotationVelocity };
  29. }
  30. private static Double GetDistanceAtAphelion(Double semiMajorAxis, Double eccentricity)
  31. {
  32. return (1 + eccentricity) * semiMajorAxis;
  33. }
  34. private static Double GetVelocityAtAphelion(Double semiMajorAxis, Double eccentricity)
  35. {
  36. var distance = GetDistanceAtAphelion(semiMajorAxis, eccentricity);
  37. return Math.Sqrt(Constants.GravitationalConstant * MassOfSun * (2 / distance - 1 / semiMajorAxis));
  38. }
  39. }
  40. }