-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoordinates.cs
More file actions
40 lines (35 loc) · 1.36 KB
/
Coordinates.cs
File metadata and controls
40 lines (35 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace ValueObjects.Location
{
public class Coordinates : ValueObject
{
const float EarthRadiusInKilometers = 6378.0F;
private readonly Latitude latitude;
private readonly Longitude longitude;
public Coordinates(Latitude latitude, Longitude longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
public float StraightDistanceInKilometersTo(Coordinates position)
{
/// Haversine formula
var latitudeInRadian = ToRadian(position.latitude - latitude);
var longitudeInRadian = ToRadian(position.longitude - longitude);
var a = Math.Pow(Math.Sin(latitudeInRadian / 2), 2) +
Math.Cos(ToRadian(latitudeInRadian)) *
Math.Cos(ToRadian(position.latitude)) *
Math.Pow(Math.Sin(longitudeInRadian / 2), 2);
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
return EarthRadiusInKilometers * Convert.ToSingle(c);
}
protected override IEnumerable<object> GetAtomicValues()
{
yield return latitude;
yield return longitude;
}
private float ToRadian(float value)
{
return Convert.ToSingle(Math.PI / 180) * value;
}
}
}