markus / KCOM / Common / MathHelper.cs @ b79d6e7f
이력 | 보기 | 이력해설 | 다운로드 (3.46 KB)
1 | 8de55603 | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Text; |
||
5 | using System.Threading.Tasks; |
||
6 | using System.Windows; |
||
7 | using System.Windows.Media; |
||
8 | |||
9 | namespace KCOM.Common |
||
10 | { |
||
11 | public static class MathHelper |
||
12 | { |
||
13 | |||
14 | 3b797b23 | humkyung | /// <summary> |
15 | /// p2를 기준으로 p1을 angle만큼 회전시킨다. |
||
16 | /// </summary> |
||
17 | /// <param name="p1"></param> |
||
18 | /// <param name="p2"></param> |
||
19 | /// <param name="angle">in degree</param> |
||
20 | /// <returns></returns> |
||
21 | 8de55603 | taeseongkim | public static Point RotatePoint(Point p1, Point p2, double angle) |
22 | { |
||
23 | 39f208de | taeseongkim | var transform = new RotateTransform() { Angle = angle, CenterX = p2.X, CenterY = p2.Y }; |
24 | var transformedPoint = transform.Transform(p1); |
||
25 | |||
26 | return transformedPoint; |
||
27 | 8de55603 | taeseongkim | } |
28 | |||
29 | public static double ConvertToRadians(double angle) |
||
30 | { |
||
31 | f3ab410f | taeseongkim | return (Math.PI / 360) * angle; |
32 | 8de55603 | taeseongkim | } |
33 | |||
34 | f3ab410f | taeseongkim | public static Rect RotateRect(Rect rect,Point Center,double angle) |
35 | 8de55603 | taeseongkim | { |
36 | f3ab410f | taeseongkim | Rect rotateRect = rect; |
37 | 39f208de | taeseongkim | |
38 | var centerPoint = new Point(rect.X + rect.Width,rect.Y + rect.Height); |
||
39 | f3ab410f | taeseongkim | var rotationCenter = RotatePoint(centerPoint, Center, angle); |
40 | 8de55603 | taeseongkim | |
41 | f3ab410f | taeseongkim | if (angle == 270 || angle == 90) |
42 | { |
||
43 | rotateRect = new Rect(0,0, rect.Height, rect.Width); |
||
44 | } |
||
45 | else |
||
46 | { |
||
47 | rotateRect = new Rect(0,0, rect.Width, rect.Height); |
||
48 | } |
||
49 | 8de55603 | taeseongkim | |
50 | f3ab410f | taeseongkim | rotateRect.X = rotationCenter.X; |
51 | rotateRect.Y = rotationCenter.Y; |
||
52 | 8de55603 | taeseongkim | |
53 | 39f208de | taeseongkim | rect.Transform(RotateAroundPoint(angle, Center)); |
54 | |||
55 | //var points = RectToPoints(rect).Select(x=> RotateAroundPoint(x,Center,angle)); |
||
56 | |||
57 | //var newtest = PointsToRect(points); |
||
58 | |||
59 | return rect; |
||
60 | } |
||
61 | |||
62 | private static Matrix RotateAroundPoint(double angle, Point center) |
||
63 | { |
||
64 | // Translate the point to the origin. |
||
65 | Matrix result = new Matrix(); |
||
66 | result.RotateAt(angle, center.X, center.Y); |
||
67 | return result; |
||
68 | } |
||
69 | |||
70 | private static IEnumerable<Point> RectToPoints(Rect rect) |
||
71 | { |
||
72 | return new [] { rect.TopLeft, rect.TopRight, rect.BottomLeft, rect.BottomRight }; |
||
73 | } |
||
74 | |||
75 | private static Rect PointsToRect(IEnumerable<Point> points) |
||
76 | { |
||
77 | return new Rect(points.ToArray()[0],points.ToArray()[3]); |
||
78 | 8de55603 | taeseongkim | } |
79 | 3b938959 | taeseongkim | |
80 | public static Rect CombineRects(List<Rect> rects) |
||
81 | { |
||
82 | if (rects == null || rects.Count == 0) |
||
83 | throw new ArgumentException("Rect list must not be null or empty."); |
||
84 | |||
85 | if (rects.Count == 1) |
||
86 | return rects[0]; |
||
87 | |||
88 | // 초기화를 위해 첫 번째 Rect를 사용합니다. |
||
89 | double minX = rects[0].Left; |
||
90 | double minY = rects[0].Top; |
||
91 | double maxX = rects[0].Right; |
||
92 | double maxY = rects[0].Bottom; |
||
93 | |||
94 | // 모든 Rect를 반복하면서 최소 및 최대 좌표를 찾습니다. |
||
95 | foreach (Rect rect in rects) |
||
96 | { |
||
97 | minX = Math.Min(minX, rect.Left); |
||
98 | minY = Math.Min(minY, rect.Top); |
||
99 | maxX = Math.Max(maxX, rect.Right); |
||
100 | maxY = Math.Max(maxY, rect.Bottom); |
||
101 | } |
||
102 | |||
103 | // 계산된 최소 및 최대 좌표를 사용하여 새로운 Rect를 생성합니다. |
||
104 | double width = maxX - minX; |
||
105 | double height = maxY - minY; |
||
106 | return new Rect(minX, minY, width, height); |
||
107 | } |
||
108 | 8de55603 | taeseongkim | } |
109 | } |