mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-05 10:36:30 +01:00
65 lines
2 KiB
C#
65 lines
2 KiB
C#
|
using System;
|
|||
|
|
|||
|
namespace TINK.Model
|
|||
|
{
|
|||
|
public class Position : IPosition
|
|||
|
{
|
|||
|
private const double PRECISSION_LATITUDE_LONGITUDE = 0.000000000000001;
|
|||
|
|
|||
|
internal Position(double latitude, double longitude)
|
|||
|
{
|
|||
|
if (!GetIsValid(latitude, longitude))
|
|||
|
throw new ArgumentNullException();
|
|||
|
|
|||
|
Latitude = latitude;
|
|||
|
Longitude = longitude;
|
|||
|
}
|
|||
|
|
|||
|
public double Latitude { get; private set; }
|
|||
|
|
|||
|
public double Longitude { get; private set; }
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Compares position with a target position.
|
|||
|
/// </summary>
|
|||
|
/// <param name="target">Target position to compare with.</param>
|
|||
|
/// <returns>True if positions are equal.</returns>
|
|||
|
public override bool Equals(object target)
|
|||
|
{
|
|||
|
if (!(target is IPosition targetPosition))
|
|||
|
return false;
|
|||
|
|
|||
|
return GetEquals(this, targetPosition);
|
|||
|
}
|
|||
|
|
|||
|
public bool Equals(IPosition other)
|
|||
|
=> (other is Position position) && position.Equals(this as object);
|
|||
|
|
|||
|
public override int GetHashCode()
|
|||
|
{
|
|||
|
var hashCode = -1416534245;
|
|||
|
hashCode = hashCode * -1521134295 + Latitude.GetHashCode();
|
|||
|
hashCode = hashCode * -1521134295 + Longitude.GetHashCode();
|
|||
|
return hashCode;
|
|||
|
}
|
|||
|
|
|||
|
public bool IsValid => GetIsValid(Latitude, Longitude);
|
|||
|
|
|||
|
public static bool GetEquals(IPosition source, IPosition target)
|
|||
|
{
|
|||
|
if (source == null || target == null)
|
|||
|
return source == null ^ target == null;
|
|||
|
|
|||
|
if (!source.IsValid || !target.IsValid)
|
|||
|
return !source.IsValid ^ !target.IsValid;
|
|||
|
|
|||
|
return Math.Abs(source.Latitude - target.Latitude) < PRECISSION_LATITUDE_LONGITUDE
|
|||
|
&& Math.Abs(source.Longitude - target.Longitude) < PRECISSION_LATITUDE_LONGITUDE;
|
|||
|
}
|
|||
|
|
|||
|
public static bool GetIsValid(double latitude, double longitude)
|
|||
|
=> !double.IsNaN(latitude) && !double.IsNaN(longitude);
|
|||
|
}
|
|||
|
}
|