用C++语言画一朵玫瑰花
时间: 2023-11-12 20:08:43 浏览: 134
这里给出一个用C++和OpenGL画玫瑰花的例子:
```c++
#include <GL/glut.h>
#include <math.h>
#define PI 3.1415926535897932384626433832795
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
const int N = 1000; // 点的数量
const double R = 0.5; // 玫瑰花的半径
const double k = 3.0; // 玫瑰花的形状参数
const double a = 1.0; // 玫瑰花的高度参数
const double delta = 2 * PI / N; // 每个点之间的角度差
glBegin(GL_POINTS);
for (int i = 0; i < N; i++)
{
double theta = delta * i;
double r = R * sin(k * theta);
double x = r * cos(theta);
double y = r * sin(theta);
double z = a * cos(k * theta);
glVertex3d(x, y, z);
}
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Rose");
glutDisplayFunc(display);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glutMainLoop();
return 0;
}
```
运行结果如下图所示:
![rose](https://cdn.luogu.com.cn/upload/image_hosting/ma7a9blq.png)
阅读全文