Skip to content

Commit

Permalink
feat: implement Matrix.CreateRotation with center point. (#17657)
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitism authored Dec 3, 2024
1 parent 7210eff commit 9ef11b0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Avalonia.Base/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ public static Matrix CreateRotation(double radians)
return new Matrix(cos, sin, -sin, cos, 0, 0);
}

/// <summary>
/// Creates a rotation matrix using the given rotation in radians around center point.
/// </summary>
/// <param name="radians">The amount of rotation, in radians. </param>
/// <param name="center">The location of center point. </param>
/// <returns></returns>
public static Matrix CreateRotation(double radians, Point center)
{
var cos = Math.Cos(radians);
var sin = Math.Sin(radians);
var x = center.X;
var y = center.Y;
return new Matrix(cos, sin, -sin, cos, x * (1.0 - cos) + y * sin, y * (1.0 - cos) - x * sin);
}

/// <summary>
/// Creates a skew matrix from the given axis skew angles in radians.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions tests/Avalonia.Base.UnitTests/MatrixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ public void Transform_Point_Should_Return_Correct_Value_For_Rotated_Matrix()

AssertCoordinatesEqualWithReducedPrecision(expected, actual);
}

[Fact]
public void Transform_Point_Should_Return_Correct_Value_For_Rotate_Matrix_With_Center_Point()
{
var expected = Vector2.Transform(
new Vector2(0, 10),
Matrix3x2.CreateRotation((float)Matrix.ToRadians(30), new Vector2(3, 5)));

var matrix = Matrix.CreateRotation(Matrix.ToRadians(30), new Point(3, 5));
var point = new Point(0, 10);
var actual = matrix.Transform(point);

AssertCoordinatesEqualWithReducedPrecision(expected, actual);
}

[Fact]
public void Transform_Point_Should_Return_Correct_Value_For_Scaled_Matrix()
Expand Down

0 comments on commit 9ef11b0

Please sign in to comment.