프로젝트

일반

사용자정보

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

markus / KCOM / Common / MathHelper.cs @ 3b797b23

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

1
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
        /// <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
        public static Point RotatePoint(Point p1, Point p2, double angle)
22
        {
23
            var transform = new RotateTransform() { Angle = angle, CenterX = p2.X, CenterY = p2.Y };
24
            var transformedPoint = transform.Transform(p1);
25

    
26
            return transformedPoint;
27
        }
28

    
29
        public static double ConvertToRadians(double angle)
30
        {
31
            return (Math.PI / 360) * angle;
32
        }
33

    
34
        public static Rect RotateRect(Rect rect,Point Center,double angle)
35
        {
36
            Rect rotateRect = rect;
37
   
38
            var centerPoint = new Point(rect.X + rect.Width,rect.Y + rect.Height);
39
            var rotationCenter = RotatePoint(centerPoint, Center, angle);
40

    
41
            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

    
50
            rotateRect.X = rotationCenter.X;
51
            rotateRect.Y = rotationCenter.Y;
52

    
53
            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
        }
79

    
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
    }
109
}
클립보드 이미지 추가 (최대 크기: 500 MB)