如何将一个点旋转任意度数?
How can I rotate one point in any degrees?
我正在使用 C# 程序,我一直在尝试将一个点 (x,y) 旋转到任何角度,但我找不到更好的解决方案,我得到了这个功能:
private Point RotateCoordinates(int degrees, double x, double y)
{
Point coordinate = new Point();
if (degrees == 0 || degrees == 360)
{
coordinate.X = x;
coordinate.Y = y;
}
else if (degrees == 90)
{
coordinate.X = y.SetNegativeValues();
coordinate.Y = x;
}
else if (degrees == 180)
{
coordinate.X = x.SetNegativeValues();
coordinate.Y = y.SetNegativeValues();
}
else if (degrees == 270)
{
coordinate.X = y;
coordinate.Y = x.SetNegativeValues();
}
return coordinate;
}
如您所见,此功能适用于 90、180 和 270 度。但问题是当我必须将它旋转 55 度、80 度或任何其他度数时。
谁能告诉我如何实现任何旋转?
如果您想知道精确的数学运算,那么您应该搜索二维旋转矩阵示例。不过,您真的不需要了解数学,因为 .Net 框架中内置了简单的旋转。
首先,添加对 WindowsBase 程序集的引用(如果您还没有的话)。要执行二维旋转,您需要 System.Windows.Vector 和 System.Windows.Media.Matrix.
示例:
using System.Windows;
using System.Windows.Media;
...
var originalPoint = new Vector(10, 0);
var transform = Matrix.Identity;
transform.Rotate(45.0); // 45 degree rotation
var rotatedPoint = originalPoint * transform;
2D 旋转的数学运算实际上非常简单,因此使用两种新的对象类型可能看起来有些矫枉过正。但矩阵变换的优点是,如果需要,您可以将多个变换组合成一个矩阵。
已经有一个可接受的答案,但如果您想在没有外部库的情况下这样做:
/// <summary>
/// Rotates the specified point around another center.
/// </summary>
/// <param name="center">Center point to rotate around.</param>
/// <param name="pt">Point to rotate.</param>
/// <param name="degree">Rotation degree. A value between 1 to 360.</param>
public static Point RotatePoint(Point center, Point pt, float degree)
{
double x1, x2, y1, y2;
x1 = center.X;
y1 = center.Y;
x2 = pt.X;
y2 = pt.Y;
double distance = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
degree *= (float)(Math.PI / 180);
double x3, y3;
x3 = distance * Math.Cos(degree) + x1;
y3 = distance * Math.Sin(degree) + y1;
return new Point((int)x3, (int)y3);
}
Point
结构是从程序集导入的:System.Drawing
;所以如果你也不想引用它,你可以把它写下来:
public struct Point
{
public Point(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
}
我正在使用 C# 程序,我一直在尝试将一个点 (x,y) 旋转到任何角度,但我找不到更好的解决方案,我得到了这个功能:
private Point RotateCoordinates(int degrees, double x, double y)
{
Point coordinate = new Point();
if (degrees == 0 || degrees == 360)
{
coordinate.X = x;
coordinate.Y = y;
}
else if (degrees == 90)
{
coordinate.X = y.SetNegativeValues();
coordinate.Y = x;
}
else if (degrees == 180)
{
coordinate.X = x.SetNegativeValues();
coordinate.Y = y.SetNegativeValues();
}
else if (degrees == 270)
{
coordinate.X = y;
coordinate.Y = x.SetNegativeValues();
}
return coordinate;
}
如您所见,此功能适用于 90、180 和 270 度。但问题是当我必须将它旋转 55 度、80 度或任何其他度数时。 谁能告诉我如何实现任何旋转?
如果您想知道精确的数学运算,那么您应该搜索二维旋转矩阵示例。不过,您真的不需要了解数学,因为 .Net 框架中内置了简单的旋转。
首先,添加对 WindowsBase 程序集的引用(如果您还没有的话)。要执行二维旋转,您需要 System.Windows.Vector 和 System.Windows.Media.Matrix.
示例:
using System.Windows;
using System.Windows.Media;
...
var originalPoint = new Vector(10, 0);
var transform = Matrix.Identity;
transform.Rotate(45.0); // 45 degree rotation
var rotatedPoint = originalPoint * transform;
2D 旋转的数学运算实际上非常简单,因此使用两种新的对象类型可能看起来有些矫枉过正。但矩阵变换的优点是,如果需要,您可以将多个变换组合成一个矩阵。
已经有一个可接受的答案,但如果您想在没有外部库的情况下这样做:
/// <summary>
/// Rotates the specified point around another center.
/// </summary>
/// <param name="center">Center point to rotate around.</param>
/// <param name="pt">Point to rotate.</param>
/// <param name="degree">Rotation degree. A value between 1 to 360.</param>
public static Point RotatePoint(Point center, Point pt, float degree)
{
double x1, x2, y1, y2;
x1 = center.X;
y1 = center.Y;
x2 = pt.X;
y2 = pt.Y;
double distance = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
degree *= (float)(Math.PI / 180);
double x3, y3;
x3 = distance * Math.Cos(degree) + x1;
y3 = distance * Math.Sin(degree) + y1;
return new Point((int)x3, (int)y3);
}
Point
结构是从程序集导入的:System.Drawing
;所以如果你也不想引用它,你可以把它写下来:
public struct Point
{
public Point(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
}