用C# 实现 已知旋转矩形左上角点点和宽高以及角度,计算旋转矩形4点坐标 4点坐标为顺时针顺序
时间: 2024-03-16 10:41:18 浏览: 116
可以使用以下代码实现:
```csharp
public static Point[] GetRotatedRectanglePoints(Point leftTop, double width, double height, double angle)
{
Point[] points = new Point[4];
double centerX = leftTop.X + width / 2;
double centerY = leftTop.Y + height / 2;
double radian = angle * Math.PI / 180;
double sin = Math.Sin(radian);
double cos = Math.Cos(radian);
double dx1 = -width / 2;
double dy1 = -height / 2;
double dx2 = width / 2;
double dy2 = -height / 2;
double dx3 = width / 2;
double dy3 = height / 2;
double dx4 = -width / 2;
double dy4 = height / 2;
double x1 = centerX + cos * dx1 - sin * dy1;
double y1 = centerY + sin * dx1 + cos * dy1;
double x2 = centerX + cos * dx2 - sin * dy2;
double y2 = centerY + sin * dx2 + cos * dy2;
double x3 = centerX + cos * dx3 - sin * dy3;
double y3 = centerY + sin * dx3 + cos * dy3;
double x4 = centerX + cos * dx4 - sin * dy4;
double y4 = centerY + sin * dx4 + cos * dy4;
points[0] = new Point(x1, y1);
points[1] = new Point(x2, y2);
points[2] = new Point(x3, y3);
points[3] = new Point(x4, y4);
return points;
}
```
其中,`leftTop` 表示旋转矩形的左上角点,`width` 和 `height` 分别表示矩形的宽度和高度,`angle` 表示矩形的旋转角度(单位为度)。函数返回一个 `Point` 类型的数组,表示旋转矩形的四个顶点坐标,按顺时针顺序排列。
阅读全文