Equality methods

This commit is contained in:
ClemensFischer 2026-01-09 23:43:39 +01:00
parent 23a8e49efb
commit 2686cda333
9 changed files with 47 additions and 91 deletions

View file

@ -28,8 +28,7 @@ namespace MapControl
public override Point? LocationToMap(double latitude, double longitude)
{
if (Location.Equals(latitude, Center.Latitude) &&
Location.Equals(longitude, Center.Longitude))
if (Center.Equals(latitude, longitude))
{
return new Point();
}

View file

@ -28,8 +28,7 @@ namespace MapControl
public override Point? LocationToMap(double latitude, double longitude)
{
if (Location.Equals(latitude, Center.Latitude) &&
Location.Equals(longitude, Center.Longitude))
if (Center.Equals(latitude, longitude))
{
return new Point();
}

View file

@ -30,32 +30,15 @@ namespace MapControl
public double Latitude { get; }
public double Longitude { get; }
public bool Equals(Location location)
{
return location != null &&
Equals(Latitude, location.Latitude) &&
Equals(Longitude, location.Longitude);
}
public bool Equals(double latitude, double longitude) => Latitude == latitude && Longitude == longitude;
public override bool Equals(object obj)
{
return Equals(obj as Location);
}
public bool Equals(Location location) => location != null && Equals(location.Latitude, location.Longitude);
public override int GetHashCode()
{
return Latitude.GetHashCode() ^ Longitude.GetHashCode();
}
public override bool Equals(object obj) => Equals(obj as Location);
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "{0},{1}", Latitude, Longitude);
}
public override int GetHashCode() => Latitude.GetHashCode() ^ Longitude.GetHashCode();
public static bool CoordinateEquals(double coordinate1, double coordinate2)
{
return Math.Abs(coordinate1 - coordinate2) < 1e-9;
}
public override string ToString() => string.Format(CultureInfo.InvariantCulture, "{0:0.########},{1:0.########}", Latitude, Longitude);
/// <summary>
/// Creates a Location instance from a string containing a comma-separated pair of floating point numbers.
@ -66,7 +49,7 @@ namespace MapControl
if (!string.IsNullOrEmpty(location))
{
values = location.Split(new char[] { ',' });
values = location.Split([',']);
}
if (values?.Length != 2)

View file

@ -28,8 +28,7 @@ namespace MapControl
public override Point? LocationToMap(double latitude, double longitude)
{
if (Location.Equals(latitude, Center.Latitude) &&
Location.Equals(longitude, Center.Longitude))
if (Center.Equals(latitude, longitude))
{
return new Point();
}

View file

@ -28,8 +28,7 @@ namespace MapControl
public override Point? LocationToMap(double latitude, double longitude)
{
if (Location.Equals(latitude, Center.Latitude) &&
Location.Equals(longitude, Center.Longitude))
if (Center.Equals(latitude, longitude))
{
return new Point();
}

View file

@ -180,7 +180,7 @@ namespace MapControl
{
var extension = Path.GetExtension(uri.LocalPath).ToLower();
if (string.IsNullOrEmpty(extension) || extension.Equals(".jpeg"))
if (string.IsNullOrEmpty(extension) || extension == ".jpeg")
{
extension = ".jpg";
}

View file

@ -49,17 +49,16 @@ namespace MapControl
{
public const string DefaultCrsId = "AUTO2:42001";
private readonly string autoCrsId;
public Wgs84AutoUtmProjection()
: this(DefaultCrsId)
{
// XAML needs parameterless constructor
}
public Wgs84AutoUtmProjection(string crsId = DefaultCrsId)
public Wgs84AutoUtmProjection(string crsId)
: base(31, true)
{
autoCrsId = crsId;
if (!string.IsNullOrEmpty(autoCrsId))
{
CrsId = autoCrsId;
}
CrsId = crsId;
}
public override Location Center
@ -77,12 +76,9 @@ namespace MapControl
if (Zone != zone || IsNorth != north)
{
var crsId = CrsId;
SetZone(zone, north);
if (!string.IsNullOrEmpty(autoCrsId))
{
CrsId = autoCrsId;
}
CrsId = crsId;
}
}
}

View file

@ -1,47 +1,27 @@
namespace MapControl
using System;
namespace MapControl
{
/// <summary>
/// Replaces Windows.Foundation.Point for double floating point precision.
/// </summary>
public readonly struct Point
public readonly struct Point(double x, double y) : IEquatable<Point>
{
public Point(double x, double y)
{
X = x;
Y = y;
}
public double X => x;
public double Y => y;
public double X { get; }
public double Y { get; }
public static implicit operator Windows.Foundation.Point(Point p) => new(p.X, p.Y);
public static implicit operator Windows.Foundation.Point(Point p)
{
return new Windows.Foundation.Point(p.X, p.Y);
}
public static implicit operator Point(Windows.Foundation.Point p) => new(p.X, p.Y);
public static implicit operator Point(Windows.Foundation.Point p)
{
return new Point(p.X, p.Y);
}
public static bool operator ==(Point p1, Point p2) => p1.Equals(p2);
public static bool operator ==(Point p1, Point p2)
{
return p1.X == p2.X && p1.Y == p2.Y;
}
public static bool operator !=(Point p1, Point p2) => !p1.Equals(p2);
public static bool operator !=(Point p1, Point p2)
{
return !(p1 == p2);
}
public bool Equals(Point p) => X == p.X && Y == p.Y;
public override bool Equals(object obj)
{
return obj is Point p && this == p;
}
public override bool Equals(object obj) => obj is Point p && Equals(p);
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
}
public override int GetHashCode() => X.GetHashCode() ^ Y.GetHashCode();
}
}

View file

@ -5,7 +5,7 @@ namespace MapControl
/// <summary>
/// Replaces Windows.Foundation.Rect for double floating point precision.
/// </summary>
public readonly struct Rect
public readonly struct Rect : IEquatable<Rect>
{
public Rect(double x, double y, double width, double height)
{
@ -28,19 +28,20 @@ namespace MapControl
public double Width { get; }
public double Height { get; }
public bool Contains(Point p)
{
return p.X >= X && p.X <= X + Width && p.Y >= Y && p.Y <= Y + Height;
}
public static implicit operator Windows.Foundation.Rect(Rect r) => new(r.X, r.Y, r.Width, r.Height);
public static implicit operator Windows.Foundation.Rect(Rect r)
{
return new Windows.Foundation.Rect(r.X, r.Y, r.Width, r.Height);
}
public static implicit operator Rect(Windows.Foundation.Rect r) => new(r.X, r.Y, r.Width, r.Height);
public static implicit operator Rect(Windows.Foundation.Rect r)
{
return new Rect(r.X, r.Y, r.Width, r.Height);
}
public static bool operator ==(Rect r1, Rect r2) => r1.Equals(r2);
public static bool operator !=(Rect r1, Rect r2) => !r1.Equals(r2);
public bool Equals(Rect r) => X == r.X && Y == r.Y && Width == r.Width && Height == r.Height;
public override bool Equals(object obj) => obj is Rect r && Equals(r);
public override int GetHashCode() => X.GetHashCode() ^ Y.GetHashCode() ^ Width.GetHashCode() ^ Height.GetHashCode();
public bool Contains(Point p) => p.X >= X && p.X <= X + Width && p.Y >= Y && p.Y <= Y + Height;
}
}