首页 » 技术分享 » 逐点比较法画圆(opengl)

逐点比较法画圆(opengl)

 

需要opengl库文件的支持,源码如下供大家参考

#include <GL/glut.h>
#include<Windows.h>

GLfloat cd=0.001f;
void drawy(GLfloat X,GLfloat Y,GLfloat R)
{
     int i=10000;
     GLfloat s;
     GLfloat x=R;
     GLfloat y=0.0f;
     GLfloat r=R*R;
      while(i>0)
     {   s=x*x+y*y;
         if(s>r)
         {
            if(x>0.0f&&y>0.0f)
            {
               x=x-cd;
            }
            else if(x<=0.0f&&y>0.0f)
               y=y-cd;
            else if(x<=0.0f&&y<=0.0f)
               x=x+cd;
            else if(x>0.0f&&y<=0.0f)
              y=y+cd;
               
         } 
         else
         {
               if(x>0.0f&&y>0.0f)
                   y=y+cd;
                else if(x<=0.0f&&y>0.0f)
                   x=x-cd;
                else if(x<=0.0f&&y<=0.0f)
                   y=y-cd;
                else if(x>0.0f&&y<=0.0f)
                  x=x+cd;
         } 
         glBegin(GL_POINTS);
            glVertex2f(x+X,y+Y);
         glEnd();
              
         i--;
     }
}

void display()
{
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);//设置当前颜色状态为红色
     drawy(0.5,-0.5,0.5);
     glFlush();//发送缓冲区
    
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("圆);
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

转载自原文链接, 如需删除请联系管理员。

原文链接:逐点比较法画圆(opengl),转载请注明来源!

0