用OPenGL实现 Bresenham画线算法

#include
#include
#include
int xs,ys,xe,ye;
void BresenhamLine(int x0,int y0,int x1,int y1)
{
	if((x0!=x1)&&(y0!=y1))
	{
		int x,y,dx,dy,e,i;
		dx=x1-x0;
		dy=y1-y0;
		e=-dx;
		x=x0;
		y=y0;
		for(i=0;i<=dx;i++)
		{
			glVertex2i(x,y);
			x=x+1;
			e=e+dy+dy;
			if(e>=0)
			{
				y=y+1;
				e=e-dx-dx;
			}
		}
	}
	else
	{
		int min,d;
		if(x0==x1)
		{
			int x=x0,y;
			y=(y0<=y1)?y0:y1;
			d=fabs((double)(y0-y1));
			while(d>=0)
			{
				glVertex2i(x,y+d);
				d--;
			}
		}
		if(y0==y1)
		{
			int x,y=y0;
			x=(x0<=x1)?x0:x1;
			d=fabs((double)(x0-x1));
			while(d>=0)
			{
				glVertex2i(x+d,y);
				d--;
			}
		}
	}
}
void lineSegment()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0,0.0,0.0);
	glBegin(GL_POINTS);
	BresenhamLine(xs,ys,xe,ye);
	glEnd();
	glFlush();
}

void main(int argc,char * argv[])
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	printf("输入起始坐标和终点坐标(范围为0-500  0-500)");
	scanf("%d,%d,%d,%d",&xs,&ys,&xe,&ye);
	glutInitWindowPosition(50,100);
	glutInitWindowSize(500,500);
	glutCreateWindow("Bresenham画线算法");
	glClearColor(1.0,1.0,1.0,0.0);
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0,500.0,0.0,500.0);
	glutDisplayFunc(lineSegment);
	glutMainLoop();
}

你可能感兴趣的:(OpenGL,opengl,c++,算法)