对称变换包括以x轴对称,y轴对称和原点o对称三种。(1)将原点平移到(300,200)处。绘制一个三角形,要求利用矩阵相乘在第一象限画出三角形,然后分别求出三个对称图形。利用openGL写代码
时间: 2024-02-11 08:03:48 浏览: 24
好的,以下是利用OpenGL和GLUT库实现的代码:
```
#include <GL/glut.h>
#include <math.h>
// 定义三角形的三个顶点,按逆时针顺序连接
GLfloat vertices[3][2] = {{-0.5, -0.5}, {0.5, -0.5}, {0.0, 0.5}};
void display() {
// 清除颜色和深度缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 绘制原始三角形
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_TRIANGLES);
for (int i = 0; i < 3; i++) {
glVertex2fv(vertices[i]);
}
glEnd();
// 进行对称变换
GLfloat sym_x[3][3] = {{1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}}; // x轴对称变换矩阵
GLfloat sym_y[3][3] = {{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}}; // y轴对称变换矩阵
GLfloat sym_o[3][3] = {{-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}}; // 原点对称变换矩阵
// x轴对称变换
GLfloat new_vertices_x[3][2];
for (int i = 0; i < 3; i++) {
GLfloat x = vertices[i][0] * sym_x[0][0] + vertices[i][1] * sym_x[1][0] + sym_x[2][0];
GLfloat y = vertices[i][0] * sym_x[0][1] + vertices[i][1] * sym_x[1][1] + sym_x[2][1];
new_vertices_x[i][0] = x + 300;
new_vertices_x[i][1] = y + 200;
}
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
for (int i = 0; i < 3; i++) {
glVertex2fv(new_vertices_x[i]);
}
glEnd();
// y轴对称变换
GLfloat new_vertices_y[3][2];
for (int i = 0; i < 3; i++) {
GLfloat x = vertices[i][0] * sym_y[0][0] + vertices[i][1] * sym_y[1][0] + sym_y[2][0];
GLfloat y = vertices[i][0] * sym_y[0][1] + vertices[i][1] * sym_y[1][1] + sym_y[2][1];
new_vertices_y[i][0] = x + 300;
new_vertices_y[i][1] = y + 200;
}
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
for (int i = 0; i < 3; i++) {
glVertex2fv(new_vertices_y[i]);
}
glEnd();
// 原点对称变换
GLfloat new_vertices_o[3][2];
for (int i = 0; i < 3; i++) {
GLfloat x = vertices[i][0] * sym_o[0][0] + vertices[i][1] * sym_o[1][0] + sym_o[2][0];
GLfloat y = vertices[i][0] * sym_o[0][1] + vertices[i][1] * sym_o[1][1] + sym_o[2][1];
new_vertices_o[i][0] = x + 300;
new_vertices_o[i][1] = y + 200;
}
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
for (int i = 0; i < 3; i++) {
glVertex2fv(new_vertices_o[i]);
}
glEnd();
// 刷新屏幕
glutSwapBuffers();
}
void init() {
// 设置窗口大小和标题
glutInitWindowSize(600, 400);
glutCreateWindow("Symmetric Transformations");
// 设置背景颜色
glClearColor(0.0, 0.0, 0.0, 0.0);
// 设置投影矩阵为正交投影
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 600.0, 0.0, 400.0, -1.0, 1.0);
// 进入模型视图矩阵
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv) {
// 初始化OpenGL和GLUT
glutInit(&argc, argv);
init();
// 注册显示函数
glutDisplayFunc(display);
// 进入主循环
glutMainLoop();
return 0;
}
```
运行后可以得到一个窗口,在第一象限绘制出原始的三角形,并在窗口中画出三个对称变换后的三角形,分别为以x轴对称、y轴对称和原点对称。其中,原点对称后的三角形颜色为蓝色,x轴对称为红色,y轴对称为绿色。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)