C# 在c中以90/180/270旋转图像后查找新坐标#
我在XML文件中有一个图像(w:10,h:15)和一些图形(矩形a(6,4),w:2,h:4)。在C#中图像旋转90/180/270度后,我需要找到A(即:A’(11,6))的新坐标 我尝试了以下代码,但我得到了一个“相对于原始图像”。我需要旋转图像中的坐标C# 在c中以90/180/270旋转图像后查找新坐标#,c#,graphics,rotation,geometry,C#,Graphics,Rotation,Geometry,我在XML文件中有一个图像(w:10,h:15)和一些图形(矩形a(6,4),w:2,h:4)。在C#中图像旋转90/180/270度后,我需要找到A(即:A’(11,6))的新坐标 我尝试了以下代码,但我得到了一个“相对于原始图像”。我需要旋转图像中的坐标 public static PointF RotatePoint(double x, double y, double pageHeight, double pageWidth, int rotateAngle) {
public static PointF RotatePoint(double x, double y, double pageHeight, double pageWidth, int rotateAngle)
{
//Calculate rotate angle in radian
var angle = rotateAngle * Math.PI / 180.0f;
//Find rotate orgin
double rotateOrginX = pageWidth / 2;
double rotateOrginY = pageHeight / 2;
var cosA = Math.Cos(angle);
var sinA = Math.Sin(angle);
var pX = (float)(cosA * (x - rotateOrginX) - sinA * (y - rotateOrginY) + rotateOrginX);
var pY = (float)(sinA * (x - rotateOrginX) + cosA * (y - rotateOrginY) + rotateOrginY);
Console.WriteLine($"Rotate {rotateAngle}\tX: {pX}\tY: {pY}");
return new PointF { X = pX, Y = pY };
}
如果使用
Math.Sin()
和Cos()
仅旋转90°、180°或270°,则过度使用
在90°旋转时,坐标可以很容易地操纵
W : Image width before rotation
H : Image height before rotation
x : X coordinate in image before rotation
x' : X coordinate in image after rotation
y : Y coordinate in image before rotation
y' : Y coordinate in image after rotation
For 0° CCW:
x' = x
y' = y
For 90° CCW:
x' = y
y' = W - x
For 180° CCW/CW:
x' = W - x
y' = H - y
For 270° CCW:
x' = H - y
y' = x
在C#中:
公共静态点f RotatePoint(浮点x、浮点y、int pageWidth、int pageHeight、int度)
{
开关(度)
{
案例0:返回新的点f(x,y);
案例90:返回新的点f(y,pageWidth-x);
案例180:返回新的点f(pageWidth-x,pageHeight-y);
案例270:返回新的点f(页面高度-y,x);
违约:
//抛出ArgumentException或使用Sin(),Cos()实现通用逻辑
}
}
至少您应该自己编写代码,并将“最佳尝试”的代码添加到问题中。请添加一些您尝试过的代码。我喜欢这个答案的简单性。只想留下一个提示,当顺时针旋转时,将90与270互换。