openGL— 中点Bresenham画圆

// aa.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include  

void Init(void)    
{
     glClearColor(0.0,0.0,0.0,0.0);    //设置背景颜色为黑色    
     glMatrixMode(GL_PROJECTION);       //对投影相关进行操作 
     gluOrtho2D(0.0, 30.0, 0.0, 30.0);
 }
 
 void CirclePoint(int x,int y)
 {
	 glColor3f(1.0,0.0,0.0);    //设置颜色为红色
     glPointSize(4);            //设置点的大小
     glBegin(GL_POINTS);
 
     glVertex2f(15+x,15+y);     // 修正点的坐标
     glVertex2f(15+y,15+x);
     glVertex2f(15-y,15+x);
     glVertex2f(15-x,y+15);
     glVertex2f(15-x,15-y);
     glVertex2f(15-y,15-x);
     glVertex2f(15+y,15-x);
     glVertex2f(15+x,15-y);
 
     glEnd();
	 glFlush(); 
 }

 void MidBresenhamCircle(int r)
 {
     int x,y,d;
     x=0;
	 y=r;
	 d=1-r;
     while(x<=y)
	 {
         CirclePoint(x,y);    
         if(d<0)
             d+=2*x+3;
         else
		 {
             d+=2*(x-y)+5;
             y--;
         }
         x++;
     }
 }

 void display()
 {
     glClear(GL_COLOR_BUFFER_BIT);       
     MidBresenhamCircle(8);    //半径为8
 }
 
 int main(int argc,char** argv)
 {

     glutInit(&argc,argv);
     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
     glutInitWindowSize(500,500);   
     glutInitWindowPosition(400,100);    
     glutCreateWindow("中点Bresenham画圆");   
     glutDisplayFunc(display);    
     Init();       
     glutMainLoop();   
 
     return 0;
 }

运行结果:

openGL— 中点Bresenham画圆_第1张图片


第二种方法:

// aa.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include  

void init(void)    
{
     glClearColor(0.0,0.0,0.0,0.0);    //设置背景颜色为黑色    
     glMatrixMode(GL_PROJECTION);       //对投影相关进行操作 
     gluOrtho2D(0.0, 30.0, 0.0, 30.0);
 }

 void putpixel(int x, int y)  
{  
    glColor3f(1.0, 0.0, 0.0);        //设置颜色为红色
    glPointSize(2);    //设置点的大小
    glBegin(GL_POINTS);  
        glVertex2f(15+x, 15+y);  
    glEnd();  
    glFlush();  
} 

 void CirclePoint(int x,int y)
 {
     putpixel(x,y);
	 putpixel(y,x);
	 putpixel(-y,x);
	 putpixel(-x,y);
	 putpixel(-x,-y);
	 putpixel(-y,-x);
	 putpixel(y,-x);
	 putpixel(x,-y);
 }

 void MidBresenhamCircle(int r)
 {
     int x,y,d;
     x=0;
	 y=r;
	 d=1-r;
     while(x<=y)
	 {
         CirclePoint(x,y);    
         if(d<0)
             d+=2*x+3;
         else
		 {
             d+=2*(x-y)+5;
             y--;
         }
         x++;
     }
 }

 void display()
 {
     glClear(GL_COLOR_BUFFER_BIT);       
     MidBresenhamCircle(8);    //半径为8
 }
 
 int main(int argc,char** argv)
 {

     glutInit(&argc,argv);
     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
     glutInitWindowSize(400,400);   
     glutInitWindowPosition(0,0);    
     glutCreateWindow("中点Bresenham画圆");   
     glutDisplayFunc(display);    
     init();       
     glutMainLoop();   
 
     return 0;
 }

运行结果:
openGL— 中点Bresenham画圆_第2张图片

你可能感兴趣的:(计算机图形学)