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

Conversation

rabbitism
Copy link
Contributor

What does the pull request do?

Add new overload for Matrix.CreateRotation(double radians, Point center).
This is a valid and commonly used transformation in SVG see: https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform#rotate
Also part of Systme.Numeric api.

Notice: whether to use Point or Vector is not finalized yet, pending on team descision.

What is the current behavior?

What is the updated/expected behavior with this PR?

How was the solution implemented (if it's not obvious)?

Checklist

Breaking changes

Obsoletions / Deprecations

Fixed issues

Closes #17307

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.3.999-cibuild0053637-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@robloo
Copy link
Contributor

robloo commented Dec 2, 2024

Please double check Radians vs. Degrees for API uniformity. Higher levels usually use degrees. It is very important we keep APIs consistent in terms of units.

@rabbitism
Copy link
Contributor Author

I think the other rotation api uses radians.

@robloo
Copy link
Contributor

robloo commented Dec 2, 2024

@rabbitism All our shape API uses degrees. API in WPF almost always uses degrees.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.rotatetransform.-ctor?view=windowsdesktop-9.0#system-windows-media-rotatetransform-ctor(system-double-system-double-system-double)

I don't know the Matrix API off the top of my head but we need to 1) Use degrees or 2) Use radians if that is what WPF does in this case.

This is something that has to be very carefully reviewed in API because both degree and radian are double. So it's a very dangerous silently breaking changing if ever it needs to be fixed.

@rabbitism
Copy link
Contributor Author

@rabbitism All our shape API uses degrees. API in WPF almost always uses degrees.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.rotatetransform.-ctor?view=windowsdesktop-9.0#system-windows-media-rotatetransform-ctor(system-double-system-double-system-double)

I don't know the Matrix API off the top of my head but we need to 1) Use degrees or 2) Use radians if that is what WPF does in this case.

This is something that has to be very carefully reviewed in API because both degree and radian are double. So it's a very dangerous silently breaking changing if ever it needs to be fixed.

I don't see any reason this API should use a different unit with the other.

public static Matrix CreateRotation(double radians)
{
double cos = Math.Cos(radians);
double sin = Math.Sin(radians);
return new Matrix(cos, sin, -sin, cos, 0, 0);
}

@robloo
Copy link
Contributor

robloo commented Dec 2, 2024

Well, seems we are already different from WPF...

https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.matrix.rotateat?view=windowsdesktop-9.0#system-windows-media-matrix-rotateat(system-double-system-double-system-double)

I wonder if this was intentional or an oversight. I guess the one benefit is radians makes the math easier. Still, we really shouldn't have both degrees and radians in the APIs. The convention has always been degrees as well. If we wanted to switch to radians it should have been done everywhere.

But it is worse to have different units in the same class. So now we are stuck.

API review is extremely important.

There should NOT be a public degrees to radians conversion method on this class either.

@maxkatz6
Copy link
Member

maxkatz6 commented Dec 2, 2024

@maxkatz6
Copy link
Member

maxkatz6 commented Dec 2, 2024

There should NOT be a public degrees to radians conversion method on this class either.

This method is 10 years old... Shouldn't be there indeed.

@robloo
Copy link
Contributor

robloo commented Dec 2, 2024

@maxkatz6 OK, if they already use radians in the BCL I won't argue. My point was WPF seems to be different using degrees. Perhaps Microsoft changed their thinking for .NET after WPF was initially released.

I guess the distinction is:

Low-level functionality uses Radians for mathematical reasons.

High-level API (XAML/Shapes) uses degrees for compatibility and simplicity (degrees are easier for people to visualize).

@maxkatz6 maxkatz6 added this pull request to the merge queue Dec 3, 2024
Merged via the queue into AvaloniaUI:master with commit 9ef11b0 Dec 3, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Matrix.Rotate(angle, x, y) overload
5 participants