digital differential analyzer DDA算法

DDA算法

虽然是大家不以为然的小算法,但对于我这个大一学生来看还是蛮新鲜的。
我所做的是C++中的openGL实现的,仅仅是换了个环境实现,所以就不多解释了,下面将代码贴出来。 
//digital differential analyzer 
//取整
int round(const float a)
{
	return int(a + 0.5);
};

void DAA (int x0, int y0, int xEnd, int yEnd)
{
	int dx = xEnd - x0;
	int dy = yEnd - y0;
	int steps;
	int rx = 0;
	int ry = 0;
	int k = 0;
	float xIncrement;
	float yIncrement;
	float x = x0;
	float y = y0;

	if(fabs((float)dx) > fabs((float)dy))
	{
		steps = fabs((float)dx);
	}
	else
	{
		steps = fabs((float)dy);
	}
        //取增量
	xIncrement = float(dx) / float(steps);
	yIncrement = float(dy) / float(steps);
        
	glVertex2f(round(x),round(y));
	for(k = 0;k < steps;k++)
	{
		x += xIncrement;
		y += yIncrement;
		rx = round(x);
		ry = round(y);
		glVertex2f( rx, ry );
	}
}

你可能感兴趣的:(C++,OpenGL,图形学,DDA)