using static System.Math; namespace Avalonia.Controls.PanAndZoom; /// /// Avalonia Matrix helper methods. /// public static class MatrixHelper { /// /// Creates a translation matrix using the specified offsets. /// /// X-coordinate offset. /// Y-coordinate offset. /// The created translation matrix. public static Matrix Translate(double offsetX, double offsetY) { return new Matrix(1.0, 0.0, 0.0, 1.0, offsetX, offsetY); } /// /// Prepends a translation around the center of provided matrix. /// /// The matrix to prepend translation. /// X-coordinate offset. /// Y-coordinate offset. /// The created translation matrix. public static Matrix TranslatePrepend(Matrix matrix, double offsetX, double offsetY) { return Translate(offsetX, offsetY) * matrix; } /// /// Creates a matrix that scales along the x-axis and y-axis. /// /// Scaling factor that is applied along the x-axis. /// Scaling factor that is applied along the y-axis. /// The created scaling matrix. public static Matrix Scale(double scaleX, double scaleY) { return new Matrix(scaleX, 0, 0, scaleY, 0.0, 0.0); } /// /// Creates a matrix that is scaling from a specified center. /// /// Scaling factor that is applied along the x-axis. /// Scaling factor that is applied along the y-axis. /// The center X-coordinate of the scaling. /// The center Y-coordinate of the scaling. /// The created scaling matrix. public static Matrix ScaleAt(double scaleX, double scaleY, double centerX, double centerY) { return new Matrix(scaleX, 0, 0, scaleY, centerX - (scaleX * centerX), centerY - (scaleY * centerY)); } /// /// Prepends a scale around the center of provided matrix. /// /// The matrix to prepend scale. /// Scaling factor that is applied along the x-axis. /// Scaling factor that is applied along the y-axis. /// The center X-coordinate of the scaling. /// The center Y-coordinate of the scaling. /// The created scaling matrix. public static Matrix ScaleAtPrepend(Matrix matrix, double scaleX, double scaleY, double centerX, double centerY) { return ScaleAt(scaleX, scaleY, centerX, centerY) * matrix; } /// /// Creates a translation and scale matrix using the specified offsets and scales along the x-axis and y-axis. /// /// Scaling factor that is applied along the x-axis. /// Scaling factor that is applied along the y-axis. /// X-coordinate offset. /// Y-coordinate offset. /// The created translation and scale matrix. public static Matrix ScaleAndTranslate(double scaleX, double scaleY, double offsetX, double offsetY) { return new Matrix(scaleX, 0.0, 0.0, scaleY, offsetX, offsetY); } /// /// Creates a skew matrix. /// /// Angle of skew along the X-axis in radians. /// Angle of skew along the Y-axis in radians. /// When the method completes, contains the created skew matrix. public static Matrix Skew(float angleX, float angleY) { return new Matrix(1.0, Tan(angleX), Tan(angleY), 1.0, 0.0, 0.0); } /// /// Creates a matrix that rotates. /// /// Angle of rotation in radians. Angles are measured clockwise when looking along the rotation axis. /// The created rotation matrix. public static Matrix Rotation(double radians) { double cos = Cos(radians); double sin = Sin(radians); return new Matrix(cos, sin, -sin, cos, 0, 0); } /// /// Creates a matrix that rotates about a specified center. /// /// Angle of rotation in radians. /// The center X-coordinate of the rotation. /// The center Y-coordinate of the rotation. /// The created rotation matrix. public static Matrix Rotation(double angle, double centerX, double centerY) { return Translate(-centerX, -centerY) * Rotation(angle) * Translate(centerX, centerY); } /// /// Creates a matrix that rotates about a specified center. /// /// Angle of rotation in radians. /// The center of the rotation. /// The created rotation matrix. public static Matrix Rotation(double angle, Vector center) { return Translate(-center.X, -center.Y) * Rotation(angle) * Translate(center.X, center.Y); } /// /// Transforms a point by this matrix. /// /// The matrix to use as a transformation matrix. /// >The original point to apply the transformation. /// The result of the transformation for the input point. public static Point TransformPoint(Matrix matrix, Point point) { return new Point( (point.X * matrix.M11) + (point.Y * matrix.M21) + matrix.M31, (point.X * matrix.M12) + (point.Y * matrix.M22) + matrix.M32); } }