Location.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Drawing;
  3. namespace InABox.Core
  4. {
  5. public class UnitOfLength
  6. {
  7. public static UnitOfLength Kilometers = new UnitOfLength(1.609344);
  8. public static UnitOfLength NauticalMiles = new UnitOfLength(0.8684);
  9. public static UnitOfLength Miles = new UnitOfLength(1);
  10. private readonly double _fromMilesFactor;
  11. private UnitOfLength(double fromMilesFactor)
  12. {
  13. _fromMilesFactor = fromMilesFactor;
  14. }
  15. public double ConvertFromMiles(double input)
  16. {
  17. return input * _fromMilesFactor;
  18. }
  19. }
  20. public class Location : EnclosedEntity
  21. {
  22. [DoubleEditor(Editable = Editable.Hidden, Visible = Visible.Optional)]
  23. public double Longitude { get; set; }
  24. [DoubleEditor(Editable = Editable.Hidden, Visible = Visible.Optional)]
  25. public double Latitude { get; set; }
  26. [DateTimeEditor(Editable = Editable.Hidden, Visible = Visible.Optional)]
  27. public DateTime Timestamp { get; set; }
  28. [TextBoxEditor(Editable = Editable.Hidden, Visible = Visible.Optional)]
  29. public string Address { get; set; }
  30. public static double DistanceBetween(PointF from, PointF to, UnitOfLength unitOfLength)
  31. {
  32. var baseRad = Math.PI * from.Y / 180;
  33. var targetRad = Math.PI * to.Y / 180;
  34. var theta = from.X - to.X;
  35. var thetaRad = Math.PI * theta / 180;
  36. var dist =
  37. Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) *
  38. Math.Cos(targetRad) * Math.Cos(thetaRad);
  39. dist = Math.Acos(dist);
  40. dist = dist * 180 / Math.PI;
  41. dist = dist * 60 * 1.1515;
  42. return unitOfLength.ConvertFromMiles(dist);
  43. }
  44. public double DistanceTo(Location location, UnitOfLength unitOfLength)
  45. {
  46. return DistanceBetween(
  47. new PointF((float)Longitude, (float)Latitude),
  48. new PointF((float)location.Longitude, (float)location.Latitude),
  49. unitOfLength
  50. );
  51. }
  52. public void CopyFrom(Location location)
  53. {
  54. Longitude = location.Longitude;
  55. Latitude = location.Latitude;
  56. Timestamp = location.Timestamp;
  57. Address = location.Address;
  58. }
  59. }
  60. }