用C++写一个点(x,y)绕旋转中心(p,q)旋转θ角的函数
时间: 2024-02-06 21:02:06 浏览: 60
好的,以下是用C语言实现一个点(x,y)绕旋转中心(p,q)旋转θ角的函数:
```c
#include <math.h>
// 定义 PI 常量
const double PI = 3.14159265358979323846;
// 定义点和中心的结构体类型
typedef struct {
double x;
double y;
} point;
typedef struct {
double x;
double y;
} center;
// 定义旋转函数
point rotate(point p, center q, double theta) {
// 将角度转换为弧度
double radian = theta * PI / 180.0;
// 计算出 cos 和 sin 值
double cosVal = cos(radian);
double sinVal = sin(radian);
// 将 point 转换为相对于中心点的坐标系
double x0 = p.x - q.x;
double y0 = p.y - q.y;
// 计算旋转后的坐标
double x1 = x0 * cosVal - y0 * sinVal;
double y1 = x0 * sinVal + y0 * cosVal;
// 将坐标系还原回绝对坐标系
point rotatedPoint;
rotatedPoint.x = x1 + q.x;
rotatedPoint.y = y1 + q.y;
return rotatedPoint;
}
```
上述代码首先定义了点和中心的结构体类型,然后通过旋转函数 `rotate` 实现了点的旋转操作。该函数接受三个参数:点 `p`、中心点 `q`、旋转角度 `theta`。通过先将角度转换为弧度,然后计算出 cos 和 sin 值,最后通过坐标系变换计算旋转后的坐标,最终将坐标系还原为绝对坐标系返回结果。需要注意的是,上述代码中的旋转角度是以度数作为单位的,如果需要使用弧度作为单位,可以将第三个参数修改为 double 类型的 radian。
阅读全文
相关推荐


















