프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Common / MathSet.cs @ 3b797b23

이력 | 보기 | 이력해설 | 다운로드 (15.2 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 betwwen 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 AngleMethod(double px1, double py1, double px2, double py2)
256
        //{
257
        //    return Math.Abs(Math.Atan2(px2 - px1, py1 - py2) * Rad2Deg);
258
        //}
259
        public static double DistanceTo(Point p1, Point p2)
260
        {
261
            double dx = p2.X - p1.X;
262
            double dy = p2.Y - p1.Y;
263
            return Math.Sqrt(dx * dx + dy * dy);
264
        }
265

    
266
        public static Point getMiddlePoint(Point p1, Point p2)
267
        {
268
            return new Point { X = (p1.X + p2.X) * 0.5, Y = (p1.Y + p2.Y) * 0.5 };
269
        }
270

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

    
283
            for (p = points.Count - 1, q = 0; q < points.Count; p = q++)
284
            {
285
                res += points[p].X * points[q].Y - points[p].Y * points[q].X;
286
            }
287

    
288
            return res;
289
        }
290

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

    
303
            double d = MathSet.DistanceTo(p1, p2);
304
            if (d > 0)
305
            {
306
                res.X = (p2.X - p1.X) / d;
307
                res.Y = (p2.Y - p1.Y) / d;
308
            }
309

    
310
            return res;
311
        }
312

    
313
        public static Point FindCentroid(List<Point> pntSet)
314
        {
315
            Double getThePointX = new Double();
316
            Double getThePointY = new Double();
317

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

    
330
        /// <summary>
331
        /// org 기준으로 dest를 dAngle(in degree)만큼 회전 시킨다.
332
        /// </summary>
333
        /// <param name="org"></param>
334
        /// <param name="dest"></param>
335
        /// <param name="dAngle"></param>
336
        /// <returns></returns>
337
        public static Point RotateAbout(Point org, Point dest, double dAngle)
338
        {
339
            double ptx = dest.X - org.X;
340
            double pty = dest.Y - org.Y;
341
            double radians = Math.PI * dAngle / 180.0;
342
            double pt1x = org.X + ((Math.Cos(radians) * ptx) - (Math.Sin(radians) * pty));
343
            double pt2x = org.Y + ((Math.Sin(radians) * ptx) + (Math.Cos(radians) * pty));
344
            return new Point(pt1x, pt2x);
345
        }
346

    
347
        public static double getAngle(double x1, double y1, double x2, double y2)
348
        {
349
            double alpha = 0;
350
            double dx = x2 - x1;
351
            double dy = y2 - y1;
352
            double l = Math.Sqrt(dx * dx + dy * dy);
353

    
354
            if (l > 0)
355
            {
356
                alpha = Math.Acos(dx / l);
357
                double cross = MathSet.CrossProduct(1, 0, dx, dy);
358
                if (cross < 0) alpha = -alpha;
359

    
360
                alpha *= MathSet.Rad2Deg;
361
            }
362
            else
363
            {
364
                alpha = 0;
365
            }
366

    
367
            return alpha;
368
        }
369

    
370
        public static double DotProduct(double x1, double y1, double x2, double y2)
371
        {
372
            return (x1 * x2 + y1 * y2);
373
        }
374

    
375
        public static double CrossProduct(double x1, double y1, double x2, double y2)
376
        {
377
            return (x1 * y2 - y1 * x2);
378
        }
379

    
380
        /// <summary>
381
        /// return angle in degree between given two vectors
382
        /// </summary>
383
        /// <author>humkyung</author>
384
        /// <date>2018.05.09</date>
385
        /// <param name="vec1"></param>
386
        /// <param name="vec2"></param>
387
        /// <returns></returns>
388
        public static double getAngleBetweenVectors(Point vec1, Point vec2)
389
        {
390
            double dot = MathSet.DotProduct(vec1.X, vec1.Y, vec2.X, vec2.Y);
391
            double length1 = Math.Sqrt(vec1.X * vec1.X + vec1.Y * vec1.Y);
392
            double length2 = Math.Sqrt(vec2.X * vec2.X + vec2.Y * vec2.Y);
393
            double cross = MathSet.CrossProduct(vec1.X, vec1.Y, vec2.X, vec2.Y);
394
            double radian = Math.Acos(dot / (length1 * length2));
395

    
396
            return (cross > 0) ? radian * MathSet.Rad2Deg : -(radian * MathSet.Rad2Deg);
397
        }
398

    
399
        // 사용용도가 불분명함.
400
        public static string returnAngleString(Point start, ref Point end, bool PressShift)
401
        {
402
            double angle = MathSet.getAngle(start.X, start.Y, end.X, end.Y);
403
            double approxAngle = MathSet.getMultipleAngle(UnitAngle, angle);
404

    
405
            if (PressShift)
406
            {
407
                double distance = MathSet.DistanceTo(start, end);
408
                end = MathSet.RotateAbout(start, new Point(start.X + distance, start.Y), approxAngle);
409
                return String.Format("{1}({0})", approxAngle.ToString("0.#") + "°", Math.Abs(approxAngle - 360).ToString("0.#") + "°");
410
            }
411
            else
412
            {
413
                angle *= -1;
414

    
415
                if (angle < 0)
416
                {
417
                    angle = angle + 360;
418
                }
419
                return String.Format("{0}", angle.ToString("0.#") + "°");
420
            }
421
        }
422

    
423
        /// <summary>
424
        /// returnAngleString을 변경하여 수정
425
        /// 상단 컨트롤에 Angle값을 보여주기 위해 수정함.
426
        /// </summary>
427
        /// <param name="start"></param>
428
        /// <param name="end"></param>
429
        /// <param name="PressShift"></param>
430
        /// <returns></returns>
431
        public static double returnAngle(Point start, ref Point end, bool PressShift)
432
        {
433
            double angle = MathSet.getAngle(start.X, start.Y, end.X, end.Y);
434
            double approxAngle = MathSet.getMultipleAngle(UnitAngle, angle);
435

    
436
            if (PressShift)
437
            {
438
                double distance = MathSet.DistanceTo(start, end);
439
                end = MathSet.RotateAbout(start, new Point(start.X + distance, start.Y), approxAngle);
440

    
441
                angle = approxAngle;
442
            }
443

    
444
            if (angle < 0)
445
            {
446
                angle = 360 + angle;
447
            }
448

    
449
            return angle;
450
        }
451

    
452
        public static Point getRectMiddlePoint(Rect data)
453
        {
454
            Point startP = new Point(data.X, data.Y);
455
            Point endP = new Point(data.Right, data.Bottom);
456
            return MathSet.getMiddlePoint(startP, endP);
457
        }
458
    }
459
}
클립보드 이미지 추가 (최대 크기: 500 MB)