ningshuxia
2024-04-25 769413fc5ff52240f001fb4bcfcca21728fb275a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using IStation.Epanet.Util;
 
namespace IStation.Epanet.Network.Structures
{
 
    ///<summary>Simple 2d point.</summary>
 
    public struct EnPoint : IComparable<EnPoint>, IEquatable<EnPoint>
    {
        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);
 
        ///<summary>Absciss coordinate.</summary>
        public double X => _x;
 
        ///<summary>Ordinate coordinate.</summary>
        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);
        }
    }
 
}