旋转图像数学(C#) [英] Rotate image math (C#)
问题描述
我有两个点的图像,排列是这样的:
I have an image with two points, aligned something like this:
|----------------|
| |
| . |
| |
| . |
| |
|----------------|
我同时拥有X,Y为坐标的,我需要将图像旋转X度,所以它看起来是这样,而不是:
I have both X, Y coordinates for both points and I need to rotate the image X degrees so it looks like this instead:
|----------------|
| |
| |
| . . |
| |
| |
|----------------|
基本上使他们对齐旁边的海誓山盟,什么是数学这个? (在C#A code例子会更好,但不是必需的)
Basically so they align next to eachother, what's the math for this? (A code example in C# would be even better but not required)
推荐答案
这取决于这一点要作为一个中心为循环使用。让我们把点到了,离开点A和一个右方和下方pointB。如果你想在A点周围旋转,使B点与它保持一致,计算弧度转角会是这样的:
It depends on which point you want to use as a "center" for your rotation. Let's call the point to the up and left pointA and the one to the right and below pointB. If you want to rotate around the point A so that point B aligns with it, calculating the rotation angle in radians would go like this:
double angle = Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);
我不知道你是如何处理你的形象,因此,只有当你使用System.Drawing.Graphics适用:
I don't how you're handling your image, so the following applies only if you're using System.Drawing.Graphics:
myImage.TranslateTransform(-pointA.X, -pointA.Y);
myImage.RotateTransform((float) angle, MatrixOrder.Append);
myImage.TranslateTransform(pointA.X, pointA.Y, MatrixOrder.Append);
希望它帮助。
这篇关于旋转图像数学(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!