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