用C# 实现 已知旋转矩形左上角点点和宽高以及角度,计算旋转矩形4点坐标
时间: 2024-03-15 18:47:25 浏览: 131
以下是用C#实现已知旋转矩形左上角点、宽高和角度,计算旋转矩形4点坐标的代码:
```csharp
public static PointF[] GetRotatedRectPoints(PointF leftTop, float width, float height, float angle)
{
PointF[] points = new PointF[4];
// 计算中心点
float centerX = leftTop.X + width / 2;
float centerY = leftTop.Y + height / 2;
// 计算旋转后的4个点坐标
float sin = (float)Math.Sin(angle);
float cos = (float)Math.Cos(angle);
float x1 = -width / 2;
float y1 = -height / 2;
float x2 = width / 2;
float y2 = -height / 2;
float x3 = width / 2;
float y3 = height / 2;
float x4 = -width / 2;
float y4 = height / 2;
points[0] = new PointF(centerX + x1 * cos - y1 * sin, centerY + x1 * sin + y1 * cos);
points[1] = new PointF(centerX + x2 * cos - y2 * sin, centerY + x2 * sin + y2 * cos);
points[2] = new PointF(centerX + x3 * cos - y3 * sin, centerY + x3 * sin + y3 * cos);
points[3] = new PointF(centerX + x4 * cos - y4 * sin, centerY + x4 * sin + y4 * cos);
return points;
}
```
其中,leftTop为左上角点坐标,width和height为宽和高,angle为旋转角度,函数返回值为旋转后的4个点坐标。
阅读全文