中点Bresenham算法扫描转换圆心在原点, 半径为8的圆;

#include 
#include "stdio.h"
#include 
#include 
#include 
using namespace std;
void CirclePoint(int x,int y,int color)
{

	 glColor3f(0.0f,0.0f,1.0f);
     glPointSize(2.0f);
     glBegin(GL_POINTS);
     glVertex2i(x+80,y+80);glVertex2i(y+80,x+80); 
     glVertex2i(-y+80,x+80); glVertex2i(-x+80,y+80);
     glVertex2i(-x+80,-y+80); glVertex2i(-y+80,-x+80);
     glVertex2i(y+80,-x+80); glVertex2i(x+80,-y+80);
     glEnd();
     glFlush();
}
void MidBresenhamCircle(int r,int color)
{
	int x,y,d;
	x=0;y=r;d=1-r;
	while(x<=y){
      CirclePoint(x,y,color);
	  if(d<0)d+=2*x+3;
	  else{
   d+=2*(x-y)+5;
   y--;
	  }
	  x++;
	}
}

void Init(void)
{
	glClearColor(1.0f,1.0f,1.0f,0.0f);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0,200.0,0.0,150.0);
}

void Display(void)
{
	glClear(GL_COLOR_BUFFER_BIT); // 用当前背景色填充窗口,如果不写这句会残留之前的图像
    MidBresenhamCircle(8,20.0f);
}
int main(int argc,char* argv[])
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(400,300);
	glutInitWindowPosition(100,100);
	glutCreateWindow("Circle");
	Init();
	glutDisplayFunc(Display);	
	glutMainLoop();
	return 0;
}


你可能感兴趣的:(C++)