利用画圆的方法来画正N边形
当N越大图形越趋近于圆
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
const GLfloat R=0.5f;
const GLfloat Pi=3.1415926536f;
void myDisplay(void)
{
int n=4;
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 1.0f);
glBegin(GL_POLYGON);//画填充图
//glBegin(GL_LINE_LOOP);//画非填充的图形
//glBegin(GL_POINTS);//画点
for(int i=0; i<n; ++i)
glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
glEnd();
glFlush();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(400,400);
glutCreateWindow("画正N边形");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
代码二:
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
void myDisplay(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 1.0f);
glBegin (GL_POLYGON); //顶点指定需要按逆时针方向
glVertex2f (0.0f,-0.5f);//下点
glVertex2f (0.5f,0.0f);//右点
glVertex2f (0.0f, 0.5f);//上点
glVertex2f (-0.5f,0.0f);//左点
glEnd ( );
glFlush();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(400,400);
glutCreateWindow("画正N 边形");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
n=4 结果: