프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkupToPDF / Controls / Common / MathSet.cs @ e65e8c5c

이력 | 보기 | 이력해설 | 다운로드 (14.8 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Windows;
5

    
6
namespace MarkupToPDF.Controls.Common
7
{
8
    public class MathSet
9
    {
10
        private const double Rad2Deg = 180.0 / Math.PI;
11
        private const double Deg2Rad = Math.PI / 180.0;
12
        private const double UnitAngle = 15;
13

    
14
        public static List<double> angleSet = new List<double>();
15

    
16
        public static double getMultipleAngle(double increase, double YourAngle)
17
        {
18
            angleSet.Clear();
19

    
20
            for (double i = 0; i <= 360; i += increase)
21
            {
22
                angleSet.Add(i);
23
            }
24

    
25
            if (YourAngle < 0)
26
            {
27
                YourAngle += 360;
28
            }
29
            double TargetData = YourAngle;
30
            double NearAngle = 0;
31
            double k = 0;
32

    
33
            foreach (var item in angleSet)
34
            {
35
                double j = 0;
36
                j = TargetData - item;
37
                j = j < 0 ? -j : j;
38
                if (j < k)
39
                {
40
                    NearAngle = item;
41
                }
42
                k = j;
43
            }
44
            return NearAngle;
45
        }
46

    
47
        public static Point getNearPoint(List<Point> pointList, Point setPoint)
48
        {
49
            Point nearPoint = pointList.Select
50
            (n => new
51
            {
52
                n,
53
                distance = Math.Sqrt
54
                    (
55
                        Math.Pow(
56
                                    (n.X - setPoint.X), 2
57
                                )
58

    
59
                        + System.Math.Pow
60
                        (
61
                            (n.Y - setPoint.Y), 2
62
                        )
63
                    )
64
            }
65
            ).OrderBy(p => p.distance).First().n;
66

    
67
            return nearPoint;
68
        }
69

    
70
        public class ClipLine
71
        {
72
            public static int DONT_INTERSECT = -0x01;
73
            public static int COLLINEAR = 0x00;
74
            public static int INTERSECT = 0x01;
75
            public static double TOLER = 0.0;
76

    
77
            public Point _start;
78
            public Point _end;
79

    
80
            public ClipLine(Point start, Point end)
81
            {
82
                _start = start;
83
                _end = end;
84
            }
85

    
86
            public bool IsLeftSide(Point pt)
87
            {
88
                double dx1 = _end.X - _start.X;
89
                double dy1 = _end.Y - _start.Y;
90
                double dx2 = pt.X - _start.X;
91
                double dy2 = pt.Y - _start.Y;
92

    
93
                return ((dx1 * dy2 - dy1 * dx2) > 0.0);
94
            }
95

    
96
            public bool IsRightSide(Point pt)
97
            {
98
                double dx1 = _end.X - _start.X;
99
                double dy1 = _end.Y - _start.Y;
100
                double dx2 = pt.X - _start.X;
101
                double dy2 = pt.Y - _start.Y;
102

    
103
                return ((dx1 * dy2 - dy1 * dx2) < 0.0);
104
            }
105

    
106
            /// <summary>
107
            /// get intersection point between this and line2d
108
            /// </summary>
109
            /// <author>humkyung</author>
110
            /// <date>2012.08.30</date>
111
            /// <param name="intsec"></param>
112
            /// <param name="line2d"></param>
113
            /// <returns></returns>
114
            public int IntersectWith(ref Point intsec, ClipLine line2d)
115
            {
116
                double Ax = 0.0, Bx = 0.0, Cx = 0.0, Ay = 0.0, By = 0.0, Cy = 0.0, d = 0.0, e = 0.0, f = 0.0;
117
                double x1lo = 0.0, x1hi = 0.0, y1lo = 0.0, y1hi = 0.0;
118

    
119
                Ax = _end.X - _start.X;
120
                Bx = line2d._start.X - line2d._end.X;
121
                // X bound box test
122
                if (Ax < 0.0)
123
                {
124
                    x1lo = _end.X;
125
                    x1hi = _start.X;
126
                }
127
                else
128
                {
129
                    x1hi = _end.X;
130
                    x1lo = _start.X;
131
                }
132

    
133
                if (Bx > 0.0)
134
                {
135
                    if ((x1hi < line2d._end.X) || (line2d._start.X < x1lo)) return ClipLine.DONT_INTERSECT;
136
                }
137
                else
138
                {
139
                    if ((x1hi < line2d._start.X) || (line2d._end.X < x1lo)) return ClipLine.DONT_INTERSECT;
140
                }
141

    
142
                Ay = _end.Y - _start.Y;
143
                By = line2d._start.Y - line2d._end.Y;
144
                /* Y bound box test*/
145
                if (Ay < 0)
146
                {
147
                    y1lo = _end.Y;
148
                    y1hi = _start.Y;
149
                }
150
                else
151
                {
152
                    y1hi = _end.Y;
153
                    y1lo = _start.Y;
154
                }
155

    
156
                if (By > 0)
157
                {
158
                    if ((y1hi < line2d._end.Y) || (line2d._start.Y < y1lo)) return ClipLine.DONT_INTERSECT;
159
                }
160
                else
161
                {
162
                    if ((y1hi < line2d._start.Y) || (line2d._end.Y < y1lo)) return ClipLine.DONT_INTERSECT;
163
                }
164

    
165
                Cx = _start.X - line2d._start.X;
166
                Cy = _start.Y - line2d._start.Y;
167
                f = Ay * Bx - Ax * By;	/* both denominator*/
168
                /// lines are collinear.
169
                if (0.0 == f) return ClipLine.COLLINEAR;
170

    
171
                d = By * Cx - Bx * Cy;	/* alpha numerator*/
172
                if (f > 0.0)
173
                {		/* alpha tests*/
174
                    if ((d < (0 + ClipLine.TOLER)) || (d > (f - ClipLine.TOLER))) return ClipLine.DONT_INTERSECT;
175
                }
176
                else
177
                {
178
                    if ((d > (0 - ClipLine.TOLER)) || (d < (f + ClipLine.TOLER))) return ClipLine.DONT_INTERSECT;
179
                }
180

    
181
                e = Ax * Cy - Ay * Cx;	/* beta numerator*/
182
                if (f > 0.0)
183
                {		/* beta tests*/
184
                    if ((e < (0 + ClipLine.TOLER)) || (e > (f - ClipLine.TOLER))) return ClipLine.DONT_INTERSECT;
185
                }
186
                else
187
                {
188
                    if ((e > (0 - ClipLine.TOLER)) || (e < (f + ClipLine.TOLER))) return ClipLine.DONT_INTERSECT;
189
                }
190

    
191
                /*compute intersection coordinates*/
192
                double alpha = d / f;
193

    
194
                if (_start.X == _end.X)
195
                    intsec.X = _start.X;
196
                else intsec.X = _start.X + alpha * Ax;
197
                if (_start.Y == _end.Y)
198
                    intsec.Y = _start.Y;
199
                else intsec.Y = _start.Y + alpha * Ay;
200

    
201
                return ClipLine.INTERSECT;
202
            }
203
        }
204

    
205
        public class ClipRect
206
        {
207
            public Point center;
208
            public double width, height, angle;	/// angle is degree
209

    
210
            public int IntersectWith(ref Point intsec, ClipLine line2d)
211
            {
212
                double radians = Math.PI * angle / 180.0;
213
                double cs = Math.Cos(radians), sn = Math.Sin(radians);
214
                Point[] corner = new Point[4];
215
                corner[0].X = center.X - width * 0.5;
216
                corner[0].Y = center.Y - height * 0.5;
217
                corner[1] = corner[0];
218
                corner[1].X += width;
219
                corner[2] = corner[1];
220
                corner[2].Y += height;
221
                corner[3] = corner[2];
222
                corner[3].X -= width;
223
                /// rotate 4 corners about angle
224
                for (int i = 0; i < corner.Length; ++i)
225
                {
226
                    double ptx = corner[i].X - center.X;
227
                    double pty = corner[i].Y - center.Y;
228
                    corner[i].X = center.X + ((cs * ptx) - (sn * pty));
229
                    corner[i].Y = center.Y + ((sn * ptx) + (cs * pty));
230
                }
231

    
232
                for (int i = 0; i < corner.Length; ++i)
233
                {
234
                    int si = i % corner.Length, ei = (i + 1) % corner.Length;
235
                    if (ClipLine.INTERSECT == line2d.IntersectWith(ref intsec, new ClipLine(corner[si], corner[ei])))
236
                    {
237
                        return ClipLine.INTERSECT;
238
                    }
239
                }
240

    
241
                return ClipLine.DONT_INTERSECT;
242
            }
243
        }
244

    
245
        public static double AngleMethod(Point StartPoint, Point EndPoint)
246
        {
247
            return Math.Abs(Math.Atan2(EndPoint.X - StartPoint.X, StartPoint.Y - EndPoint.Y) * Rad2Deg);
248
        }
249

    
250
        public static double DegreesToRadians(double angle)
251
        {
252
            return ((angle * Math.PI) / 180f);
253
        }
254

    
255
        public static double DistanceTo(Point p1, Point p2)
256
        {
257
            double dx = p2.X - p1.X;
258
            double dy = p2.Y - p1.Y;
259
            return Math.Sqrt(dx * dx + dy * dy);
260
        }
261

    
262
        public static Point getMiddlePoint(Point p1, Point p2)
263
        {
264
            return new Point { X = (p1.X + p2.X) * 0.5, Y = (p1.Y + p2.Y) * 0.5 };
265
        }
266

    
267
        /// <summary>
268
        /// return area of polygon
269
        /// </summary>
270
        /// <author>humkyung</author>
271
        /// <date>2012.07.04</date>
272
        /// <param name="points"></param>
273
        /// <returns></returns>
274
        public static double AreaOf(List<Point> points)
275
        {
276
            double res = 0;
277
            int p = 0, q = 0;
278

    
279
            for (p = points.Count - 1, q = 0; q < points.Count; p = q++)
280
            {
281
                res += points[p].X * points[q].Y - points[p].Y * points[q].X;
282
            }
283

    
284
            return res;
285
        }
286

    
287
        /// <summary>
288
        /// return normal vector from p1 to p2
289
        /// </summary>
290
        /// <author>humkyung</author>
291
        /// <date>2012.07.19</date>
292
        /// <param name="p1"></param>
293
        /// <param name="p2"></param>
294
        /// <returns></returns>
295
        public static Point GetNormVectorBetween(Point p1, Point p2)
296
        {
297
            Point res = new Point();
298

    
299
            double d = MathSet.DistanceTo(p1, p2);
300
            if (d > 0)
301
            {
302
                res.X = (p2.X - p1.X) / d;
303
                res.Y = (p2.Y - p1.Y) / d;
304
            }
305

    
306
            return res;
307
        }
308

    
309
        public static Point FindCentroid(List<Point> pntSet)
310
        {
311
            Double getThePointX = new Double();
312
            Double getThePointY = new Double();
313

    
314
            for (int i = 0; i < pntSet.Count; i++)
315
            {
316
                int ReIndex = (i + pntSet.Count / 2) % pntSet.Count;
317
                Point p = (MathSet.getMiddlePoint(pntSet[i], pntSet[ReIndex]));
318
                getThePointX += p.X;
319
                getThePointY += p.Y;
320
            }
321
            double count = Convert.ToDouble(pntSet.Count);
322
            //return new Point(getThePointX/Convert.ToDouble(pntSet.Count(),getThePointY/Convert.ToDouble(pntSet.Count());
323
            return new Point(getThePointX / count, getThePointY / count);
324
        }
325

    
326
        /// <summary>
327
        /// org 기준으로 dest를 dAngle(in degree)만큼 회전 시킨다.
328
        /// </summary>
329
        /// <param name="org"></param>
330
        /// <param name="dest"></param>
331
        /// <param name="dAngle"></param>
332
        /// <returns></returns>
333
        public static Point RotateAbout(Point org, Point dest, double dAngle)
334
        {
335
            var transform = new RotateTransform() { Angle = dAngle, CenterX = org.X, CenterY = org.Y };
336
            return transform.Transform(dest);
337
        }
338

    
339
        public static double getAngle(double x1, double y1, double x2, double y2)
340
        {
341
            double alpha = 0;
342
            double dx = x2 - x1;
343
            double dy = y2 - y1;
344
            double l = Math.Sqrt(dx * dx + dy * dy);
345

    
346
            if (l > 0)
347
            {
348
                alpha = Math.Acos(dx / l);
349
                double cross = MathSet.CrossProduct(1, 0, dx, dy);
350
                if (cross < 0) alpha = -alpha;
351

    
352
                alpha *= MathSet.Rad2Deg;
353
            }
354
            else
355
            {
356
                alpha = 0;
357
            }
358

    
359
            return alpha;
360
        }
361

    
362
        public static double DotProduct(double x1, double y1, double x2, double y2)
363
        {
364
            return (x1 * x2 + y1 * y2);
365
        }
366

    
367
        public static double CrossProduct(double x1, double y1, double x2, double y2)
368
        {
369
            return (x1 * y2 - y1 * x2);
370
        }
371

    
372
        /// <summary>
373
        /// return angle in degree between given two vectors
374
        /// </summary>
375
        /// <author>humkyung</author>
376
        /// <date>2018.05.09</date>
377
        /// <param name="vec1"></param>
378
        /// <param name="vec2"></param>
379
        /// <returns></returns>
380
        public static double getAngleBetweenVectors(Point vec1, Point vec2)
381
        {
382
            double dot = MathSet.DotProduct(vec1.X, vec1.Y, vec2.X, vec2.Y);
383
            double length1 = Math.Sqrt(vec1.X * vec1.X + vec1.Y * vec1.Y);
384
            double length2 = Math.Sqrt(vec2.X * vec2.X + vec2.Y * vec2.Y);
385
            double cross = MathSet.CrossProduct(vec1.X, vec1.Y, vec2.X, vec2.Y);
386
            double radian = Math.Acos(dot / (length1 * length2));
387

    
388
            return (cross > 0) ? radian * MathSet.Rad2Deg : -(radian * MathSet.Rad2Deg);
389
        }
390

    
391
        // 사용용도가 불분명함.
392
        public static string returnAngleString(Point start, ref Point end, bool PressShift)
393
        {
394
            double angle = MathSet.getAngle(start.X, start.Y, end.X, end.Y);
395
            double approxAngle = MathSet.getMultipleAngle(UnitAngle, angle);
396

    
397
            if (PressShift)
398
            {
399
                double distance = MathSet.DistanceTo(start, end);
400
                end = MathSet.RotateAbout(start, new Point(start.X + distance, start.Y), approxAngle);
401
                return String.Format("{1}({0})", approxAngle.ToString("0.#") + "°", Math.Abs(approxAngle - 360).ToString("0.#") + "°");
402
            }
403
            else
404
            {
405
                angle *= -1;
406

    
407
                if (angle < 0)
408
                {
409
                    angle = angle + 360;
410
                }
411
                return String.Format("{0}", angle.ToString("0.#") + "°");
412
            }
413
        }
414

    
415
        /// <summary>
416
        /// returnAngleString을 변경하여 수정
417
        /// 상단 컨트롤에 Angle값을 보여주기 위해 수정함.
418
        /// </summary>
419
        /// <param name="start"></param>
420
        /// <param name="end"></param>
421
        /// <param name="PressShift"></param>
422
        /// <returns></returns>
423
        public static double returnAngle(Point start, ref Point end, bool PressShift)
424
        {
425
            double angle = MathSet.getAngle(start.X, start.Y, end.X, end.Y);
426
            double approxAngle = MathSet.getMultipleAngle(UnitAngle, angle);
427

    
428
            if (PressShift)
429
            {
430
                double distance = MathSet.DistanceTo(start, end);
431
                end = MathSet.RotateAbout(start, new Point(start.X + distance, start.Y), approxAngle);
432

    
433
                angle = approxAngle;
434
            }
435

    
436
            if (angle < 0)
437
            {
438
                angle = 360 + angle;
439
            }
440

    
441
            return angle;
442
        }
443

    
444
        public static Point getRectMiddlePoint(Rect data)
445
        {
446
            Point startP = new Point(data.X, data.Y);
447
            Point endP = new Point(data.Right, data.Bottom);
448
            return MathSet.getMiddlePoint(startP, endP);
449
        }
450
    }
451
}
클립보드 이미지 추가 (최대 크기: 500 MB)