用C++写一个点(x,y)绕旋转中心P 旋转θ角的函数
时间: 2024-06-10 20:04:55 浏览: 108
好的,您要求的是一个C语言编写的点(x,y)绕旋转中心P旋转θ角的函数,下面是示例代码:
```
#include <math.h>
// 定义点类型
typedef struct {
double x;
double y;
} Point;
// 旋转函数
Point rotate(Point point, Point center, double angle) {
Point result;
double cosTheta = cos(angle);
double sinTheta = sin(angle);
double deltaX = point.x - center.x;
double deltaY = point.y - center.y;
result.x = deltaX * cosTheta - deltaY * sinTheta + center.x;
result.y = deltaX * sinTheta + deltaY * cosTheta + center.y;
return result;
}
// 示例代码
int main() {
Point center = {0, 0}; // 旋转中心点
Point point = {1, 1}; // 需要旋转的点
double angle = M_PI / 4; // 旋转角度,以弧度为单位
Point result = rotate(point, center, angle);
printf("(%f, %f)", result.x, result.y);
return 0;
}
```
其中,rotate函数的实现方法参考了2D图形的旋转变换公式。该函数接收三个参数:需要旋转的点,旋转中心点以及旋转角度,返回旋转后的点。示例代码中,我们声明了一个Point结构体代表点的坐标,定义了旋转中心center和需要旋转的点point,以及旋转角度angle(这里为了方便,我们将旋转角度设为45度,即π/4弧度)。使用rotate函数进行旋转并返回结果,最后打印结果即可。
阅读全文
相关推荐
















