C# 旋转矩形的点位置
我有一个要旋转的矩形 我只有矩形的一个点(鼠标位置)、矩形的长度和宽度以及旋转角度。现在我需要矩形的其他三个角的位置,这样我就可以用Graphics.DrawPolygon()轻松地绘制矩形 我不知道,因为我已经好几年没学数学了,需要你的帮助C# 旋转矩形的点位置,c#,math,rotation,points,rectangles,C#,Math,Rotation,Points,Rectangles,我有一个要旋转的矩形 我只有矩形的一个点(鼠标位置)、矩形的长度和宽度以及旋转角度。现在我需要矩形的其他三个角的位置,这样我就可以用Graphics.DrawPolygon()轻松地绘制矩形 我不知道,因为我已经好几年没学数学了,需要你的帮助 我已经尝试过许多来自互联网的解决方案,但没有什么能真正适合我的问题。下面的课程做了你正在寻找的。请看一下旋转是如何工作的,以供参考: 旋转和绘制的矩形之间有什么关系?围绕哪个中心点旋转?有一个矩形和一个文本框或类似的文本框,用户必须在其中写入旋转角度。。矩
我已经尝试过许多来自互联网的解决方案,但没有什么能真正适合我的问题。下面的课程做了你正在寻找的。请看一下旋转是如何工作的,以供参考:
旋转和绘制的矩形之间有什么关系?围绕哪个中心点旋转?有一个矩形和一个文本框或类似的文本框,用户必须在其中写入旋转角度。。矩形用新的位置重新绘制。。中心点是鼠标位置(矩形的中心或矩形的左上角(尚未确定))。。为此,我需要角的位置。。所以你创建的3或4X和ySo总是与某个特定的轴/面对齐,比如说与x轴对齐。有一次,它被绘制出来,在指定的点周围应用所需的旋转。是的,我总是创建一个矩形,但此时没有角度
public class MyRectangle
{
public double Length { get; set; }
public double Width { get; set; }
public double Rotation { get; private set; }
public Coord Center { get; private set; }
public Coord TopLeft { get; private set; }
public Coord TopRight { get; private set; }
public Coord BottomLeft { get; private set; }
public Coord BottomRight { get; private set; }
public MyRectangle(Coord origin, double length, double width)
{
Length = length;
Width = width;
Center = origin;
BottomLeft = new Coord(Center.X - Width / 2, Center.Y - Length / 2);
BottomRight = new Coord(Center.X + Width / 2, Center.Y - Length / 2);
TopLeft = new Coord(Center.X - Width / 2, Center.Y + Length / 2);
TopRight = new Coord(Center.X + Width / 2, Center.Y + Length / 2);
}
private void Move(Coord c)
{
InitCorners(new Coord((c.X - Center.X), (c.Y - Center.Y)));
Center.X = Center.X + (c.X - Center.X);
Center.Y = Center.Y + (c.Y - Center.Y);
}
private void InitCorners(Coord c)
{
BottomRight.X = (BottomRight.X + c.X );
BottomRight.Y = (BottomRight.Y + c.Y);
BottomLeft.X = (BottomLeft.X + c.X);
BottomLeft.Y = (BottomLeft.Y + c.Y);
TopRight.X = (TopRight.X + c.X);
TopRight.Y = (TopRight.Y + c.Y);
TopLeft.X = (TopLeft.X + c.X);
TopLeft.Y = (TopLeft.Y + c.Y);
}
public void Rotate(double qtyRadians)
{
//Move center to origin
Coord temp_orig = new Coord(Center.X, Center.Y);
Move(new Coord(0, 0));
BottomRight = RotatePoint(BottomRight, qtyRadians);
TopRight = RotatePoint(TopRight, qtyRadians);
BottomLeft = RotatePoint(BottomLeft, qtyRadians);
TopLeft = RotatePoint(TopLeft, qtyRadians);
//Move center back
Move(temp_orig);
}
Coord RotatePoint(Coord p, double qtyRadians)
{
Coord temb_br = new Coord(p.X, p.Y);
p.X = temb_br.X * Math.Cos(qtyRadians) - temb_br.Y * Math.Sin(qtyRadians);
p.Y = temb_br.Y * Math.Cos(qtyRadians) + temb_br.X * Math.Sin(qtyRadians);
return p;
}
}
[DebuggerDisplay("({X},{Y})")]
public class Coord
{
public double X { get; set; }
public double Y { get; set; }
public Coord(double x, double y)
{
X = x;
Y = y;
}
}