/Sciences/Geodesics/Distance.cs |
@@ -0,0 +1,85 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
|
namespace wasSharp.Geo |
{ |
public enum DistanceUnits |
{ |
METERS = 1, |
KILOMETERS = 2 |
} |
|
public class Distance : IComparable<Distance>, IEquatable<Distance> |
{ |
public Distance(double meters) |
{ |
Meters = meters; |
} |
|
public double Kilometers => Meters / 1000d; |
|
public double Meters { get; } |
|
public int CompareTo(Distance other) |
{ |
return Meters.CompareTo(other.Meters); |
} |
|
public bool Equals(Distance other) |
{ |
return this == other; |
} |
|
public static bool operator >(Distance a, Distance b) |
{ |
return a != null && b != null && a.Meters > b.Meters; |
} |
|
public static bool operator >=(Distance a, Distance b) |
{ |
return (a == null && b == null) || (a != null && b != null && a.Meters >= b.Meters); |
} |
|
public static bool operator <(Distance a, Distance b) |
{ |
return a != null && b != null && a.Meters < b.Meters; |
} |
|
public static bool operator <=(Distance a, Distance b) |
{ |
return (a == null && b == null) || (a != null && b != null && a.Meters <= b.Meters); |
} |
|
public static bool operator ==(Distance a, Distance b) |
{ |
return (ReferenceEquals(a, null) && ReferenceEquals(b, null)) || |
(!ReferenceEquals(a, null) && !ReferenceEquals(b, null) && a.Meters == b.Meters); |
} |
|
public static bool operator !=(Distance a, Distance b) |
{ |
return !(a == b); |
} |
|
public override bool Equals(object obj) |
{ |
if (obj == null) |
return false; |
|
if (obj is Distance) |
return Equals(obj as Distance); |
|
return ReferenceEquals(this, obj); |
} |
|
public override int GetHashCode() |
{ |
return base.GetHashCode(); |
} |
} |
} |
/Sciences/Geodesics/GeodesicExtensions.cs |
@@ -0,0 +1,37 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
|
namespace wasSharp.Geo |
{ |
public static class GeodesicExtensions |
{ |
public static Distance HaversineDistanceTo(this GeographicCoordinate sourceGeographicCoordinate, |
GeographicCoordinate targetGeographicCoordinate) |
{ |
return HaversineDistanceTo(sourceGeographicCoordinate, targetGeographicCoordinate, DistanceUnits.KILOMETERS); |
} |
|
public static Distance HaversineDistanceTo(this GeographicCoordinate sourceGeographicCoordinate, |
GeographicCoordinate targetGeographicCoordinate, |
DistanceUnits distanceUnits) |
{ |
var sourcePhi = Math.PI * sourceGeographicCoordinate.Latitude / 180; |
var targetPhi = Math.PI * targetGeographicCoordinate.Latitude / 180; |
var deltaPhi = Math.PI * (targetPhi - sourcePhi) / 180; |
var deltaLam = Math.PI * (targetGeographicCoordinate.Longitude - sourceGeographicCoordinate.Longitude) / 180; |
|
var a = Math.Sin(deltaPhi / 2) * Math.Sin(deltaPhi / 2) + |
Math.Cos(sourcePhi) * Math.Cos(targetPhi) * |
Math.Sin(deltaLam / 2) * Math.Sin(deltaLam / 2); |
|
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); |
|
return new Distance(Constants.EARTH_MEAN_RADIUS.Meters * c); |
} |
} |
} |
/Sciences/Geodesics/GeographicCoordinate.cs |
@@ -0,0 +1,21 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
namespace wasSharp.Geo |
{ |
public class GeographicCoordinate |
{ |
public GeographicCoordinate(double latitude, double longitude) |
{ |
Latitude = latitude; |
Longitude = longitude; |
} |
|
public double Latitude { get; } |
|
public double Longitude { get; } |
} |
} |
/Sciences/Mathematics/Numerics.cs |
@@ -0,0 +1,41 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
namespace wasSharp |
{ |
public static class Numerics |
{ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Given a value in a source value range and a target range, map |
/// the value from the source range into the target range. |
/// </summary> |
/// <remarks> |
/// value - the value to map |
/// xMin - the lower bound of the source range |
/// xMax - the upper bound of the source range |
/// yMin - the lower bound of the target range |
/// yMax - the upper bound of the target range |
/// </remarks> |
/// <returns>a value in x mapped in the range of y</returns> |
public static double MapValueToRange(double value, double xMin, double xMax, double yMin, double yMax) |
{ |
return yMin + (yMax - yMin) * (value - xMin) / (xMax - xMin); |
} |
|
public static bool IsNullOrDefault<T>(T value) |
{ |
return Equals(value, default(T)); |
} |
|
public static T DefaultOrValue<T>(this T initial, T value) |
{ |
return Equals(initial, default(T)) ? value : initial; |
} |
} |
} |