Skip to content

Implement Matrix.CreateRotation with center point. #17657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading