markus / KCOM / Common / MathHelper.cs @ 3abe8d4e
이력 | 보기 | 이력해설 | 다운로드 (2.6 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 | public static Point RotatePoint(Point p1, Point p2, double angle) |
||
15 | { |
||
16 | |||
17 | 39f208de | taeseongkim | //double radians = ConvertToRadians(angle); |
18 | //double sin = Math.Sin(radians); |
||
19 | //double cos = Math.Cos(radians); |
||
20 | 8de55603 | taeseongkim | |
21 | 39f208de | taeseongkim | //// Translate point back to origin |
22 | //p1.X -= p2.X; |
||
23 | //p1.Y -= p2.Y; |
||
24 | 8de55603 | taeseongkim | |
25 | 39f208de | taeseongkim | //// Rotate point |
26 | //double xnew = p1.X * cos - p1.Y * sin; |
||
27 | //double ynew = p1.X * sin + p1.Y * cos; |
||
28 | 8de55603 | taeseongkim | |
29 | 39f208de | taeseongkim | //// 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 | 8de55603 | taeseongkim | } |
38 | |||
39 | public static double ConvertToRadians(double angle) |
||
40 | { |
||
41 | f3ab410f | taeseongkim | return (Math.PI / 360) * angle; |
42 | 8de55603 | taeseongkim | } |
43 | |||
44 | f3ab410f | taeseongkim | public static Rect RotateRect(Rect rect,Point Center,double angle) |
45 | 8de55603 | taeseongkim | { |
46 | f3ab410f | taeseongkim | Rect rotateRect = rect; |
47 | 39f208de | taeseongkim | |
48 | var centerPoint = new Point(rect.X + rect.Width,rect.Y + rect.Height); |
||
49 | f3ab410f | taeseongkim | var rotationCenter = RotatePoint(centerPoint, Center, angle); |
50 | 8de55603 | taeseongkim | |
51 | f3ab410f | taeseongkim | 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 | 8de55603 | taeseongkim | |
60 | f3ab410f | taeseongkim | rotateRect.X = rotationCenter.X; |
61 | rotateRect.Y = rotationCenter.Y; |
||
62 | 8de55603 | taeseongkim | |
63 | 39f208de | taeseongkim | 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 | 8de55603 | taeseongkim | } |
89 | } |
||
90 | } |