tangxu
2023-11-12 8e7279907bb106a103d764d87a9ecf6ea200c93b
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Permissions;
 
namespace Hydro.Model
{
    [DataContract]
    public class Point2d :  ICloneable
    {
        [DataMember]
        public double X { get { return x; } set { x = value; } }
        [DataMember]
        public double Y { get { return y; } set { y = value; } }
 
 
        protected double x;
        protected double y; 
 
        #region 构造函数
        public Point2d() { }
 
 
 
        public Point2d(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
        public Point2d(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public Point2d(decimal x, decimal y)
        {
            this.x = Convert.ToDouble(x);
            this.y = Convert.ToDouble(y);
        }
        public Point2d(Point  rhs)
        {
            this.X = rhs.X;
            this.Y = rhs.Y;
        }
        public Point2d(Point2d rhs)
        {
            this.X = rhs.X;
            this.Y = rhs.Y; 
        }
        public Point2d(string strPara)
        {
            if (string.IsNullOrEmpty(strPara))
                return;
            string[] strPara_split_array = strPara.Split(',');
            if (strPara_split_array.Count() < 2)
                return;
            X = Convert.ToDouble(strPara_split_array[0]);
            Y = Convert.ToDouble(strPara_split_array[1]); 
        }
        public static Point2d ToParameter(string strPara)
        {
            if (string.IsNullOrEmpty(strPara))
                return null;
            string[] strPara_split_array = strPara.Split(',');
            if (strPara_split_array.Count() < 2)
                return null;
            Point2d pt = new Point2d();
            pt.X = Convert.ToDouble(strPara_split_array[0]);
            pt.Y = Convert.ToDouble(strPara_split_array[1]); 
            return pt;
        }
 
        #endregion
 
        #region ToString
        public override string ToString()
        {
            return string.Format("X:{0},Y:{1}", X, Y );
        }
 
        #endregion
 
        #region Clone
        public Point2d Clone()  //对外提供一个创建自身的浅表副本的能力
        {
            return new Point2d(x, y);
        }
        object ICloneable.Clone()
        {
            return this.Clone();
        }
        public override bool Equals(object obj)
        {
            Point2d rhs = obj as Point2d;
            return this.X == rhs.X && this.Y == rhs.Y;
        }
        public static Point2d Copy(Point2d pt)
        {
            if (pt == null)
                return null;
            return new Point2d(pt);
        }
        public static List<Point2d> Copy(List<Point2d> list)
        {
            if (list == null)
                return null;
            List<Point2d> newList = new List<Point2d>();
            foreach (var pt in list)
                newList.Add(new Point2d(pt));
 
            return newList;
        }
        #endregion
 
        /// <summary>
        /// 偏置
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public Point2d Offset(double x, double y)
        {
            return new Point2d(this.x + x, this.y + y);
        }
 
 
 
        /// <summary>
        /// Return the HashCode from the base class.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
 
        /// <summary>
        /// Missing values are represented internally using <see cref="System.Double.MaxValue"/>.
        /// </summary>
        public const double Missing = Double.MaxValue;
 
        /// <summary>
        /// The default format to be used for displaying model values via the
        /// <see cref="ToString()"/> method.
        /// </summary>
        public const string DefaultFormat = "N2";
 
        #region Serialization
 
        public const int schema = 11;
 
        protected Point2d(SerializationInfo info, StreamingContext context)
        {
 
            X = info.GetDouble("X");
            Y = info.GetDouble("Y");
        }
 
        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("schema", schema);
            info.AddValue("X", X);
            info.AddValue("Y", Y); 
        }
        public string ToDsString()
        {
            return ToDsString(this);
        }
 
        public static string ToDsString(Point2d point)
        {
            return string.Format("{0},{1}", point.X, point.Y );
        }
 
 
        #endregion
 
        #region Properties
 
        /// <summary>
        /// Readonly value that determines if either the X or the Y
        /// coordinate in this PointPair is a missing value.
        /// </summary>
        /// <returns>true if either value is missing</returns>
        public bool IsMissing()
        {
             return this.X == Point2d.Missing || this.Y == Point2d.Missing  ; 
        }
 
        /// <summary>
        /// Readonly value that determines if either the X or the Y
        /// coordinate in this PointPair is an invalid (not plotable) value.
        /// It is considered invalid if it is missing (equal to System.Double.MaxWholeAxis),
        /// Infinity, or NaN.
        /// </summary>
        /// <returns>true if either value is invalid</returns>
        public bool IsInvalid()
        {
            return this.X == Point2d.Missing ||
                    this.Y == Point2d.Missing || 
                    Double.IsInfinity(this.X) ||
                    Double.IsInfinity(this.Y) || 
                    Double.IsNaN(this.X) ||
                    Double.IsNaN(this.Y)  ;
 
        }
        public bool IsZeroPt()
        {
            if ((Math.Abs(x) < 0.00001) && (Math.Abs(y) < 0.00001)  )
                return true;
            else
                return false;
        }
        /// <summary>
        /// static method to determine if the specified model value is invalid.
        /// </summary>
        /// <remarks>The value is considered invalid if it is <see cref="PointPairBase.Missing"/>,
        /// <see cref="Double.PositiveInfinity"/>, <see cref="Double.NegativeInfinity"/>
        /// or <see cref="Double.NaN"/>.</remarks>
        /// <param name="value">The value to be checked for validity.</param>
        /// <returns>true if the value is invalid, false otherwise</returns>
        public static bool IsValueInvalid(double value)
        {
            return (value == Point2d.Missing ||
                    Double.IsInfinity(value) ||
                    Double.IsNaN(value));
        }
 
        #endregion
 
 
        public enum eSortType
        {
            X,
            Y 
        }
        public enum eCompareType
        {
            X,
            Y, 
            ALL
        }
 
        //排序与比较
        public class Comparer : IComparer<Hydro.Model.Point2d>
        {
            private Hydro.Model.Point2d.eSortType sortType;
            public Comparer(Hydro.Model.Point2d.eSortType type)
            {
                sortType = type;
            }
 
            #region IComparer<FeatPoint> 成员
            int IComparer<Hydro.Model.Point2d>.Compare(Hydro.Model.Point2d obj1, Hydro.Model.Point2d obj2)
            {
                if (sortType == Hydro.Model.Point2d.eSortType.X)
                {
                    if (Math.Abs(obj1.X - obj2.X) < 0.00001)
                    {
                        return 0;
                    }
                    else if (obj1.X > obj2.X)
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
           
                else
                {
                    if (Math.Abs(obj1.Y - obj2.Y) < 0.00001)
                    {
                        return 0;
                    }
                    else if (obj1.Y > obj2.Y)
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
            }
 
            #endregion
        }
 
        //比较相同:主要用与LIST的Contains方法和Distinct
        public class EqualComparer : IEqualityComparer<Hydro.Model.Point2d>
        {
            private double ignoreDis = 0.00001;
            public Hydro.Model.Point2d.eCompareType CompareType = eCompareType.X;//比较类型
            public EqualComparer()
            {
                ignoreDis = 0.00001;
            }
            public EqualComparer(double dis, Hydro.Model.Point2d.eCompareType compareType)
            {
                ignoreDis = dis;
                CompareType = compareType;
            }
 
            public bool Equals(Hydro.Model.Point2d lhs, Hydro.Model.Point2d rhs)
            {
                switch (CompareType)
                {
                    case eCompareType.X:
                        if (Math.Abs(lhs.X - rhs.X) < ignoreDis)
                            return true;
                        break;
                    case eCompareType.Y:
                        if (Math.Abs(lhs.Y - rhs.Y) < ignoreDis)
                            return true;
                        break; 
                    case eCompareType.ALL:
                        if (Math.Abs(lhs.X - rhs.X) < ignoreDis && Math.Abs(lhs.Y - rhs.Y) < ignoreDis )
                            return true;
                        break;
                }
                return false;
            }
 
            public int GetHashCode(Hydro.Model.Point2d obj)
            {
                //return obj.X.GetHashCode() + obj.Y.GetHashCode(); 
                return 0;
 
            }
        }
 
        //距离
        public double Distance(Point2d pt)
        {
            return Math.Sqrt((pt.X - this.X) * (pt.X - this.X) 
                + (pt.Y - this.Y) * (pt.Y - this.Y)
 
                );
        }
    }
 
 
 
 
}