Bresenham画线算法简单实线

#include<freeglut.h>
#include<cmath>

typedef GLint Vertex3[3];
GLint winwidth = 300, winheight = 300;
void reshapeFcn(GLint newwidth,GLint newheight)
{
	glViewport(0, 0, newwidth, newheight);

	winwidth = newwidth;
	winheight = newheight;
}

void init()
{
	glClearColor(1.0, 1.0, 1.0, 0.0);

	gluOrtho2D(0, winwidth, 0, winheight);

}

void test()
{
	int x0 = 10, y0 = 15, xE = 100, yE = 145; //(x0,y0)  (xE,yE);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,0,0);
	int dx = fabs(xE - x0), dy = fabs(yE - y0);
	int twody = 2 * dy;
	int twod = twody - 2 * dx;
	int p = twody - dx;
	int x, y;

	if (x0 > xE)
	{
		x = xE;
		y = yE;
		xE = x0;
	}
	else
	{
		x = x0; y = y0;
	}
	glBegin(GL_POINTS);
	glVertex2i(x, y);
	while (x < xE)
	{
		x++;
		if (p < 0)
		{
			p = p + twody;
		}
		else
		{
			p = p + twod;
			y++;
		}
		glVertex2i(x, y);
	}
	glEnd();
	glFlush();
}


void main(int argc, char **argv)
{
	
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(winwidth, winheight);
	glutCreateWindow("nbb");

	init();
	glutReshapeFunc(reshapeFcn);
	glutDisplayFunc(test);

	glutMainLoop();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值