用C# 实现 已知旋转矩形中心点和宽高以及角度,计算旋转矩形4点坐标
时间: 2024-03-15 20:46:59 浏览: 52
可以使用以下代码实现已知旋转矩形中心点和宽高以及角度,计算旋转矩形4点坐标:
```csharp
public static PointF[] GetRotatedRectanglePoints(PointF center, float width, float height, float angle)
{
float halfWidth = width / 2;
float halfHeight = height / 2;
PointF[] points = new PointF[4];
// 计算旋转后的四个顶点坐标
points[0] = new PointF(center.X - halfWidth, center.Y - halfHeight);
points[1] = new PointF(center.X + halfWidth, center.Y - halfHeight);
points[2] = new PointF(center.X + halfWidth, center.Y + halfHeight);
points[3] = new PointF(center.X - halfWidth, center.Y + halfHeight);
// 计算旋转角度的正弦和余弦值
float sinAngle = (float)Math.Sin(angle);
float cosAngle = (float)Math.Cos(angle);
// 对每个顶点进行旋转
for (int i = 0; i < 4; i++)
{
float x = points[i].X - center.X;
float y = points[i].Y - center.Y;
points[i].X = center.X + (x * cosAngle - y * sinAngle);
points[i].Y = center.Y + (x * sinAngle + y * cosAngle);
}
return points;
}
```
其中,`center` 表示旋转矩形的中心点坐标,`width` 和 `height` 分别表示旋转矩形的宽度和高度,`angle` 表示旋转角度。函数返回一个长度为 4 的 `PointF` 数组,表示旋转矩形的四个顶点坐标。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)