五角星的绘制

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34195441/article/details/79122365

#include <GL/glut.h>
#include <math.h>
const int n = 1000;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;
const GLfloat factor = 0.1f;

void myDisplay(void)
{
 glClearColor(0.0, 0.0, 0.0, 0.0);
 glClear(GL_COLOR_BUFFER_BIT);

 glColor3f (1.0f, 0.1f, 1.0f); 
 glRectf(-0.8f, -0.5f, 0.8f, 0.8f);

 glBegin (GL_TRIANGLES);
 glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (-0.8f, 0.8f);
 glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.8f, 0.8f);
 glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (0.0f, -0.5f);
 glEnd ();

 GLfloat a = 1 / (2-2*cos(72*Pi/180));
 GLfloat bx = a * cos(18 * Pi/180);
 GLfloat by = a * sin(18 * Pi/180);
 GLfloat cy = -a * cos(18 * Pi/180);
 GLfloat
 PointA[2] = { 0, a },
 PointB[2] = { bx, by },
 PointC[2] = { 0.5, cy },
 PointD[2] = { -0.5, cy },
 PointE[2] = { -bx, by };

    glClear(GL_COLOR_BUFFER_BIT);
    // 按照A->C->E->B->D->A的顺序,将五角星画出
  glBegin(GL_LINE_LOOP);//闭合折线
  glVertex2fv(PointA);
  glVertex2fv(PointC);
  glVertex2fv(PointE);
  glVertex2fv(PointB);
  glVertex2fv(PointD);
  glEnd();
  glFlush();

}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Hello Opengl!");
    glutDisplayFunc(&myDisplay);
    glutMainLoop();
    return 0;
}


运行截图:


猜你喜欢

转载自blog.csdn.net/qq_34195441/article/details/79122365