Avoid unnecessary Location and Point allocations

This commit is contained in:
ClemensFischer 2025-12-12 21:28:45 +01:00
parent f44d2207e5
commit e268be2948
20 changed files with 205 additions and 172 deletions

View file

@ -33,8 +33,8 @@ namespace MapControl
public bool Equals(Location location)
{
return location != null &&
Math.Abs(location.Latitude - Latitude) < 1e-9 &&
Math.Abs(location.Longitude - Longitude) < 1e-9;
Equals(Latitude, location.Latitude) &&
Equals(Longitude, location.Longitude);
}
public override bool Equals(object obj)
@ -52,6 +52,11 @@ namespace MapControl
return string.Format(CultureInfo.InvariantCulture, "{0},{1}", Latitude, Longitude);
}
public static bool CoordinateEquals(double coordinate1, double coordinate2)
{
return Math.Abs(coordinate1 - coordinate2) < 1e-9;
}
/// <summary>
/// Creates a Location instance from a string containing a comma-separated pair of floating point numbers.
/// </summary>
@ -87,12 +92,12 @@ namespace MapControl
/// <summary>
/// Calculates great circle azimuth and distance in radians between this and the specified Location.
/// </summary>
public void GetAzimuthDistance(Location location, out double azimuth, out double distance)
public void GetAzimuthDistance(double latitude, double longitude, out double azimuth, out double distance)
{
var lat1 = Latitude * Math.PI / 180d;
var lon1 = Longitude * Math.PI / 180d;
var lat2 = location.Latitude * Math.PI / 180d;
var lon2 = location.Longitude * Math.PI / 180d;
var lat2 = latitude * Math.PI / 180d;
var lon2 = longitude * Math.PI / 180d;
var cosLat1 = Math.Cos(lat1);
var sinLat1 = Math.Sin(lat1);
var cosLat2 = Math.Cos(lat2);
@ -112,7 +117,7 @@ namespace MapControl
/// </summary>
public double GetDistance(Location location, double earthRadius = MapProjection.Wgs84EquatorialRadius)
{
GetAzimuthDistance(location, out double _, out double distance);
GetAzimuthDistance(location.Latitude, location.Longitude, out _, out double distance);
return earthRadius * distance;
}