프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (3.69 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
        public static Point RotatePoint(Point p1, Point p2, double angle)
15
        {
16

    
17
            //double radians = ConvertToRadians(angle);
18
            //double sin = Math.Sin(radians);
19
            //double cos = Math.Cos(radians);
20

    
21
            //// Translate point back to origin
22
            //p1.X -= p2.X;
23
            //p1.Y -= p2.Y;
24

    
25
            //// Rotate point
26
            //double xnew = p1.X * cos - p1.Y * sin;
27
            //double ynew = p1.X * sin + p1.Y * cos;
28

    
29
            //// Translate point back
30
            //Point newPoint = new Point((int)xnew + p2.X, (int)ynew + p2.Y);
31

    
32

    
33
            var transform = new RotateTransform() { Angle = angle, CenterX = p2.X, CenterY = p2.Y };
34
            var transformedPoint = transform.Transform(p1);
35

    
36
            return transformedPoint;
37
        }
38

    
39
        public static double ConvertToRadians(double angle)
40
        {
41
            return (Math.PI / 360) * angle;
42
        }
43

    
44
        public static Rect RotateRect(Rect rect,Point Center,double angle)
45
        {
46
            Rect rotateRect = rect;
47
   
48
            var centerPoint = new Point(rect.X + rect.Width,rect.Y + rect.Height);
49
            var rotationCenter = RotatePoint(centerPoint, Center, angle);
50

    
51
            if (angle == 270 || angle == 90)
52
            {
53
                rotateRect = new Rect(0,0, rect.Height, rect.Width);
54
            }
55
            else
56
            {
57
                rotateRect = new Rect(0,0, rect.Width, rect.Height);
58
            }
59

    
60
            rotateRect.X = rotationCenter.X;
61
            rotateRect.Y = rotationCenter.Y;
62

    
63
            rect.Transform(RotateAroundPoint(angle, Center));
64

    
65
            //var points = RectToPoints(rect).Select(x=> RotateAroundPoint(x,Center,angle));
66

    
67
            //var newtest = PointsToRect(points);
68

    
69
            return rect;
70
        }
71

    
72
        private static Matrix RotateAroundPoint(double angle, Point center)
73
        {
74
            // Translate the point to the origin.
75
            Matrix result = new Matrix();
76
            result.RotateAt(angle, center.X, center.Y);
77
            return result;
78
        }
79

    
80
        private static IEnumerable<Point> RectToPoints(Rect rect)
81
        {
82
            return new [] { rect.TopLeft, rect.TopRight, rect.BottomLeft, rect.BottomRight };
83
        }
84

    
85
        private static Rect PointsToRect(IEnumerable<Point> points)
86
        {
87
            return new Rect(points.ToArray()[0],points.ToArray()[3]);
88
        }
89

    
90
        public static Rect CombineRects(List<Rect> rects)
91
        {
92
            if (rects == null || rects.Count == 0)
93
                throw new ArgumentException("Rect list must not be null or empty.");
94

    
95
            if (rects.Count == 1)
96
                return rects[0];
97

    
98
            // 초기화를 위해 첫 번째 Rect를 사용합니다.
99
            double minX = rects[0].Left;
100
            double minY = rects[0].Top;
101
            double maxX = rects[0].Right;
102
            double maxY = rects[0].Bottom;
103

    
104
            // 모든 Rect를 반복하면서 최소 및 최대 좌표를 찾습니다.
105
            foreach (Rect rect in rects)
106
            {
107
                minX = Math.Min(minX, rect.Left);
108
                minY = Math.Min(minY, rect.Top);
109
                maxX = Math.Max(maxX, rect.Right);
110
                maxY = Math.Max(maxY, rect.Bottom);
111
            }
112

    
113
            // 계산된 최소 및 최대 좌표를 사용하여 새로운 Rect를 생성합니다.
114
            double width = maxX - minX;
115
            double height = maxY - minY;
116
            return new Rect(minX, minY, width, height);
117
        }
118
    }
119
}
클립보드 이미지 추가 (최대 크기: 500 MB)