markus / ZoomAndPan / MatrixHelper.cs @ e1c892f7
이력 | 보기 | 이력해설 | 다운로드 (6.06 KB)
1 |
// Copyright (c) Wiesław Šoltés. All rights reserved. |
---|---|
2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 |
using System.Windows; |
4 |
using System.Windows.Media; |
5 |
using static System.Math; |
6 |
|
7 |
namespace ZoomAndPan |
8 |
{ |
9 |
/// <summary> |
10 |
/// WPF Matrix helper methods. |
11 |
/// </summary> |
12 |
public static class MatrixHelper |
13 |
{ |
14 |
/// <summary> |
15 |
/// Creates a translation matrix using the specified offsets. |
16 |
/// </summary> |
17 |
/// <param name="offsetX">X-coordinate offset.</param> |
18 |
/// <param name="offsetY">Y-coordinate offset.</param> |
19 |
/// <returns>The created translation matrix.</returns> |
20 |
public static Matrix Translate(double offsetX, double offsetY) |
21 |
{ |
22 |
return new Matrix(1.0, 0.0, 0.0, 1.0, offsetX, offsetY); |
23 |
} |
24 |
|
25 |
/// <summary> |
26 |
/// Prepends a translation around the center of provided matrix. |
27 |
/// </summary> |
28 |
/// <param name="matrix">The matrix to prepend translation.</param> |
29 |
/// <param name="offsetX">X-coordinate offset.</param> |
30 |
/// <param name="offsetY">Y-coordinate offset.</param> |
31 |
/// <returns>The created translation matrix.</returns> |
32 |
public static Matrix TranslatePrepend(Matrix matrix, double offsetX, double offsetY) |
33 |
{ |
34 |
return Translate(offsetX, offsetY) * matrix; |
35 |
} |
36 |
|
37 |
/// <summary> |
38 |
/// Creates a matrix that scales along the x-axis and y-axis. |
39 |
/// </summary> |
40 |
/// <param name="scaleX">Scaling factor that is applied along the x-axis.</param> |
41 |
/// <param name="scaleY">Scaling factor that is applied along the y-axis.</param> |
42 |
/// <returns>The created scaling matrix.</returns> |
43 |
public static Matrix Scale(double scaleX, double scaleY) |
44 |
{ |
45 |
return new Matrix(scaleX, 0, 0, scaleY, 0.0, 0.0); |
46 |
} |
47 |
|
48 |
/// <summary> |
49 |
/// Creates a matrix that is scaling from a specified center. |
50 |
/// </summary> |
51 |
/// <param name="scaleX">Scaling factor that is applied along the x-axis.</param> |
52 |
/// <param name="scaleY">Scaling factor that is applied along the y-axis.</param> |
53 |
/// <param name="centerX">The center X-coordinate of the scaling.</param> |
54 |
/// <param name="centerY">The center Y-coordinate of the scaling.</param> |
55 |
/// <returns>The created scaling matrix.</returns> |
56 |
public static Matrix ScaleAt(double scaleX, double scaleY, double centerX, double centerY) |
57 |
{ |
58 |
return new Matrix(scaleX, 0, 0, scaleY, centerX - (scaleX * centerX), centerY - (scaleY * centerY)); |
59 |
} |
60 |
|
61 |
/// <summary> |
62 |
/// Prepends a scale around the center of provided matrix. |
63 |
/// </summary> |
64 |
/// <param name="matrix">The matrix to prepend scale.</param> |
65 |
/// <param name="scaleX">Scaling factor that is applied along the x-axis.</param> |
66 |
/// <param name="scaleY">Scaling factor that is applied along the y-axis.</param> |
67 |
/// <param name="centerX">The center X-coordinate of the scaling.</param> |
68 |
/// <param name="centerY">The center Y-coordinate of the scaling.</param> |
69 |
/// <returns>The created scaling matrix.</returns> |
70 |
public static Matrix ScaleAtPrepend(Matrix matrix, double scaleX, double scaleY, double centerX, double centerY) |
71 |
{ |
72 |
return ScaleAt(scaleX, scaleY, centerX, centerY) * matrix; |
73 |
} |
74 |
|
75 |
/// <summary> |
76 |
/// Creates a skew matrix. |
77 |
/// </summary> |
78 |
/// <param name="angleX">Angle of skew along the X-axis in radians.</param> |
79 |
/// <param name="angleY">Angle of skew along the Y-axis in radians.</param> |
80 |
/// <returns>When the method completes, contains the created skew matrix.</returns> |
81 |
public static Matrix Skew(float angleX, float angleY) |
82 |
{ |
83 |
return new Matrix(1.0, Tan(angleX), Tan(angleY), 1.0, 0.0, 0.0); |
84 |
} |
85 |
|
86 |
/// <summary> |
87 |
/// Creates a matrix that rotates. |
88 |
/// </summary> |
89 |
/// <param name="radians">Angle of rotation in radians. Angles are measured clockwise when looking along the rotation axis.</param> |
90 |
/// <returns>The created rotation matrix.</returns> |
91 |
public static Matrix Rotation(double radians) |
92 |
{ |
93 |
double cos = Cos(radians); |
94 |
double sin = Sin(radians); |
95 |
return new Matrix(cos, sin, -sin, cos, 0, 0); |
96 |
} |
97 |
|
98 |
/// <summary> |
99 |
/// Creates a matrix that rotates about a specified center. |
100 |
/// </summary> |
101 |
/// <param name="angle">Angle of rotation in radians.</param> |
102 |
/// <param name="centerX">The center X-coordinate of the rotation.</param> |
103 |
/// <param name="centerY">The center Y-coordinate of the rotation.</param> |
104 |
/// <returns>The created rotation matrix.</returns> |
105 |
public static Matrix Rotation(double angle, double centerX, double centerY) |
106 |
{ |
107 |
return Translate(-centerX, -centerY) * Rotation(angle) * Translate(centerX, centerY); |
108 |
} |
109 |
|
110 |
/// <summary> |
111 |
/// Creates a matrix that rotates about a specified center. |
112 |
/// </summary> |
113 |
/// <param name="angle">Angle of rotation in radians.</param> |
114 |
/// <param name="center">The center of the rotation.</param> |
115 |
/// <returns>The created rotation matrix.</returns> |
116 |
public static Matrix Rotation(double angle, Vector center) |
117 |
{ |
118 |
return Translate(-center.X, -center.Y) * Rotation(angle) * Translate(center.X, center.Y); |
119 |
} |
120 |
|
121 |
/// <summary> |
122 |
/// Transforms a point by this matrix. |
123 |
/// </summary> |
124 |
/// <param name="matrix">The matrix to use as a transformation matrix.</param> |
125 |
/// <param name="point">>The original point to apply the transformation.</param> |
126 |
/// <returns>The result of the transformation for the input point.</returns> |
127 |
public static Point TransformPoint(Matrix matrix, Point point) |
128 |
{ |
129 |
return new Point( |
130 |
(point.X * matrix.M11) + (point.Y * matrix.M21) + matrix.OffsetX, |
131 |
(point.X * matrix.M12) + (point.Y * matrix.M22) + matrix.OffsetY); |
132 |
} |
133 |
} |
134 |
} |