C语言实现图形的旋转变换
时间: 2024-05-02 16:20:30 浏览: 7
以下是C语言实现图形旋转变换的示例代码:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
// 定义二维坐标结构体
typedef struct {
int x;
int y;
} Point;
// 旋转变换函数
Point rotate(Point p, double angle) {
Point q;
double rad = angle * PI / 180.0;
q.x = round(p.x * cos(rad) - p.y * sin(rad));
q.y = round(p.x * sin(rad) + p.y * cos(rad));
return q;
}
int main() {
// 定义要旋转的点和旋转角度
Point p = {100, 100};
double angle = 30;
// 进行旋转变换
Point q = rotate(p, angle);
// 输出旋转前后的点坐标
printf("p(%d,%d) -> q(%d,%d)\n", p.x, p.y, q.x, q.y);
return 0;
}
```
在上述代码中:
- `rotate` 函数实现了二维坐标的旋转变换,其中 `p` 是要旋转的点,`angle` 是旋转角度,函数返回旋转后的点坐标 `q`。
- `main` 函数中定义了一个要旋转的点 `p` 和旋转角度 `angle`,然后调用 `rotate` 函数进行旋转变换,并输出旋转前后的点坐标。
需要注意的是,上述代码中使用了 `math.h` 中的 `cos` 和 `sin` 函数进行角度的计算。同时,为了避免精度误差,旋转后的点坐标使用了 `round` 函数进行四舍五入处理。