//绘制正六边形,余弦和圆
#include<GL/glut.h>
#include<math.h>
const GLfloat pi = 3.1415926f;
int n = 1000;
const GLfloat factor = 0.1f;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
for (int i = 0; i < n; i++)
{
glVertex2f(0.4f*cos(2 * (pi / n)*i) - 0.5f, 0.4f*sin(2 * (pi / n)*i)+0.5f);
}
glEnd();
glFlush();
glBegin(GL_LINE_LOOP);
glColor3f(0.0f,0.0f,0.0f);
for (int i = 1; i <= 6; i++)
{
glVertex2f(0.4f*cos(2 * (pi / 6)*i)+0.5f, 0.4f*sin(2 * (pi / 6)*i) + 0.5f);
}
glEnd();
glFlush();
glBegin(GL_LINE_STRIP);
glColor3f(0.0f, 0.0f, 0.0f);
for (double x = -1.0f / factor; x<1.0f / factor; x += 0.01f)
{
glVertex2f(x*factor, sin(x)*factor - 0.50);
}
glEnd();
glFlush();
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-1.0f,0.0f);
glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f,-1.0f);
glVertex2f(0.0f,1.0f);
glEnd();
glFlush();
}
void init()
{
glClearColor(0.0,1.0,1.0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0f,1.0f,-1.0f,1.0f);
}
int main(int argc,char*argv[])
{
glutInit(&argc,argv); //初始化
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); //选择rgb,单缓冲
glutInitWindowPosition(300,100); //窗体位置
glutInitWindowSize(600,600); //窗体大小
glutCreateWindow("六边形和圆和余弦函数");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
效果: