asmrobot
2019-11-21 589ed88a5924a7494e21b95b6bbff5e46ff49ddd
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
using RichCreator.Utility.Structs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace RichCreator.Utility.Utilitys
{
 
    /// <summary>
    /// 几何体之间的关系类型
    /// </summary>
    public enum Intersection
    {
        None,//没关系
        Tangent,//切线,正切
        Intersection,//相交,交叉
        Containment//包含
    }
 
    /// <summary>
    /// 与几何相关的辅助类
    /// </summary>
    public static class GeoHelper
    {
 
        /// <summary>
        /// 计算两点之间的距离
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static double GetDistance(ZTPoint start, ZTPoint end)
        {
            Int32 subX = end.X - start.X;
            Int32 subY = end.Y - start.Y;
            return Math.Sqrt(subX * subX + subY * subY);
        }
 
        /// <summary>
        /// 指定点是否在方框内
        /// </summary>
        /// <param name="point"></param>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static bool IsInRect(ZTPoint point, ZTRectangle rect)
        {
            if (point.Y >= rect.Start.Y &&
                point.Y <= rect.End.Y &&
                point.X >= rect.Start.X &&
                point.X <= rect.End.X)
            {
                return true;
            }
            return false;
        }
 
 
 
        /// <summary>
        /// 获取点到点的距离
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double GetPointDistance(ZTPoint a, ZTPoint b)
        {
            return Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
        }
 
        /// <summary>
        /// 获取点到直线的距离
        /// </summary>
        /// <param name="line"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        public static double GetNearestDistance(ZTLinePoint line, ZTPoint point)
        {
            double a, b, c;
            a = GetPointDistance(line.P2, point);
            if (a <= 0.00001)
            {
                return 0.0f;
            }
 
            b = GetPointDistance(line.P1, point);
            if (b <= 0.00001)
            {
                return 0.0f;
            }
 
            c = GetPointDistance(line.P1, line.P2);
            if (c <= 0.00001)
            {
                //如果PA和PB坐标相同,则退出函数,并返回距离
                return a;
            }
 
            if (a * a >= b * b + c * c)
            {
                return b;      //如果是钝角返回b
            }
 
            if (b * b >= a * a + c * c)
            {
                return a;      //如果是钝角返回a
            }
 
            double l = (a + b + c) / 2;     //周长的一半
            double s = Math.Sqrt(Math.Abs(l * (l - a) * (l - b) * (l - c)));  //海伦公式求面积,也可以用矢量求
            return 2 * s / c;
        }
 
 
        
        /// <summary>
        /// 判断线段与多边形的关系
        /// </summary>
        /// <param name="line"></param>
        /// <param name="polygon"></param>
        /// <returns></returns>
        public static Intersection IntersectionOf(ZTLinePoint line, ZTPolygon polygon)
        {
            if (polygon.Length == 0)
            {
                return Intersection.None;
            }
            if (polygon.Length == 1)
            {
                return IntersectionOf(polygon[0], line);
            }
            bool tangent = false;
            for (int index = 0; index < polygon.Length; index++)
            {
                int index2 = (index + 1) % polygon.Length;
                Intersection intersection = IntersectionOf(line, new ZTLinePoint(polygon[index], polygon[index2]));
                if (intersection == Intersection.Intersection)
                {
                    return intersection;
                }
                if (intersection == Intersection.Tangent)
                {
                    tangent = true;
                }
            }
            return tangent ? Intersection.Tangent : IntersectionOf(line.P1, polygon);
        }
 
 
        /// <summary>
        /// 判断点与多边形的关系
        /// </summary>
        /// <param name="point"></param>
        /// <param name="polygon"></param>
        /// <returns></returns>
        public static Intersection IntersectionOf(ZTPoint point, ZTPolygon polygon)
        {
            switch (polygon.Length)
            {
                case 0:
                    return Intersection.None;
                case 1:
                    if (polygon[0].X == point.X && polygon[0].Y == point.Y)
                    {
                        return Intersection.Tangent;
                    }
                    else
                    {
                        return Intersection.None;
                    }
                case 2:
                    return IntersectionOf(point, new ZTLinePoint(polygon[0], polygon[1]));
            }
 
            int counter = 0;
            int i;
            ZTPoint p1;
            int n = polygon.Length;
            p1 = polygon[0];
            if (point .Equals( p1))
            {
                return Intersection.Tangent;
            }
 
            for (i = 1; i <= n; i++)
            {
                ZTPoint p2 = polygon[i % n];
                if (point .Equals( p2))
                {
                    return Intersection.Tangent;
                }
                if (point.Y > Math.Min(p1.Y, p2.Y))
                {
                    if (point.Y <= Math.Max(p1.Y, p2.Y))
                    {
                        if (point.X <= Math.Max(p1.X, p2.X))
                        {
                            if (p1.Y != p2.Y)
                            {
                                double xinters = (point.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
                                if (p1.X == p2.X || point.X <= xinters)
                                    counter++;
                            }
                        }
                    }
                }
                p1 = p2;
            }
 
            return (counter % 2 == 1) ? Intersection.Containment : Intersection.None;
        }
 
        /// <summary>
        /// 判断点与直线的关系
        /// </summary>
        /// <param name="point"></param>
        /// <param name="line"></param>
        /// <returns></returns>
        public static Intersection IntersectionOf(ZTPoint point, ZTLinePoint line)
        {
            float bottomY = Math.Min(line.Y1, line.Y2);
            float topY = Math.Max(line.Y1, line.Y2);
            bool heightIsRight = point.Y >= bottomY &&
                                 point.Y <= topY;
            //Vertical line, slope is divideByZero error!
            if (line.X1 == line.X2)
            {
                if (point.X == line.X1 && heightIsRight)
                {
                    return Intersection.Tangent;
                }
                else
                {
                    return Intersection.None;
                }
            }
            float slope = (line.X2 - line.X1) / (line.Y2 - line.Y1);
            bool onLine = (line.Y1 - point.Y) == (slope * (line.X1 - point.X));
            if (onLine && heightIsRight)
            {
                return Intersection.Tangent;
            }
            else
            {
                return Intersection.None;
            }
        }
 
        /// <summary>
        /// 判断直线与直线的关系
        /// </summary>
        /// <param name="line1"></param>
        /// <param name="line2"></param>
        /// <returns></returns>
        public static Intersection IntersectionOf(ZTLinePoint line1, ZTLinePoint line2)
        {
            //  Fail if either line segment is zero-length.
            if (line1.X1 == line1.X2 && line1.Y1 == line1.Y2 || line2.X1 == line2.X2 && line2.Y1 == line2.Y2)
                return Intersection.None;
 
            if (line1.X1 == line2.X1 && line1.Y1 == line2.Y1 || line1.X2 == line2.X1 && line1.Y2 == line2.Y1)
                return Intersection.Intersection;
            if (line1.X1 == line2.X2 && line1.Y1 == line2.Y2 || line1.X2 == line2.X2 && line1.Y2 == line2.Y2)
                return Intersection.Intersection;
 
            //  (1) Translate the system so that point A is on the origin.
            line1.X2 -= line1.X1; line1.Y2 -= line1.Y1;
            line2.X1 -= line1.X1; line2.Y1 -= line1.Y1;
            line2.X2 -= line1.X1; line2.Y2 -= line1.Y1;
 
            //  Discover the length of segment A-B.
            double distAB = Math.Sqrt(line1.X2 * line1.X2 + line1.Y2 * line1.Y2);
 
            //  (2) Rotate the system so that point B is on the positive X axis.
            double theCos = line1.X2 / distAB;
            double theSin = line1.Y2 / distAB;
            double newX = line2.X1 * theCos + line2.Y1 * theSin;
            line2.Y1 = (Int32)(line2.Y1 * theCos - line2.X1 * theSin); line2.X1 = (Int32)newX;
            newX = line2.X2 * theCos + line2.Y2 * theSin;
            line2.Y2 = (Int32)(line2.Y2 * theCos - line2.X2 * theSin); line2.X2 = (Int32)newX;
 
            //  Fail if segment C-D doesn't cross line A-B.
            if (line2.Y1 < 0 && line2.Y2 < 0 || line2.Y1 >= 0 && line2.Y2 >= 0)
                return Intersection.None;
 
            //  (3) Discover the position of the intersection point along line A-B.
            double posAB = line2.X2 + (line2.X1 - line2.X2) * line2.Y2 / (line2.Y2 - line2.Y1);
 
            //  Fail if segment C-D crosses line A-B outside of segment A-B.
            if (posAB < 0 || posAB > distAB)
                return Intersection.None;
 
            //  (4) Apply the discovered position to line A-B in the original coordinate system.
            return Intersection.Intersection;
        }
    }
 
 
}