using IStation.Epanet.Util; using System; namespace IStation.Epanet.Network.Structures { ///Simple 2d point. public struct EnPoint : IComparable, IEquatable { public static readonly EnPoint Invalid = new EnPoint(double.NaN, double.NaN); private readonly double _x; private readonly double _y; public EnPoint(double x, double y) { _x = x; _y = y; } public bool IsInvalid => double.IsNaN(_x) || double.IsNaN(_y); ///Absciss coordinate. public double X => _x; ///Ordinate coordinate. public double Y => _y; public double DistanceTo(EnPoint other) { double dx = _x - other._x; double dy = _y - other._y; return Math.Sqrt(dx * dx + dy * dy); } public int CompareTo(EnPoint other) { var cmp = _x.CompareTo(other._x); return cmp == 0 ? _y.CompareTo(other._y) : cmp; } public bool Equals(EnPoint other) { bool ex = _x.EqualsTo(other._x) || double.IsNaN(_x) && double.IsNaN(other._x); bool ey = _y.EqualsTo(other._y) || double.IsNaN(_y) && double.IsNaN(other._y); return ex && ey; } public override bool Equals(object obj) { return obj is EnPoint point && Equals(point); } public override int GetHashCode() { return _x.GetHashCode() ^ _y.GetHashCode(); } public override string ToString() { return string.Format(nameof(EnPoint) + "{{x={0}, y={1}}}", _x, _y); } } }