0
我测试了一条线如何可以是1.垂直2.水平3.所有的情况下有正或小于1斜率。该功能的作品,但我想审查它,如果有溢出,丢失测试案例..等等。我刚刚在维基百科上阅读了该算法,并试图从维基百科文章中实现它。DDA线算法的实现
// Draw line using DDA Algorithm
void Graphics::DrawLine(int x1, int y1, int x2, int y2, Color&color)
{
float xdiff = x1-x2;
float ydiff = y1-y2;
int slope = 1;
if (y1 == y2 )
{
slope = 0;
}
else if ( x1 == x2)
{
slope = 2; // vertical lines have no slopes...
}
else
{
slope = (int)xdiff/ydiff;
}
if (slope <= 1)
{
int startx = 0;
int endx = 0;
if (x1 > x2)
{
startx = x2;
endx = x1;
}
else
{
startx = x1;
endx = x2;
}
float y = y1; // initial value
for(int x = startx; x <= endx; x++)
{
y += slope;
DrawPixel(x, (int)abs(y), color);
}
}
else if (slope > 1)
{
float x = x1; // initial value
int starty = 0;
int endy = 0;
if (y1 > y2)
{
starty = y2;
endy = y1;
}
else
{
starty = y1;
endy = y2;
}
for(int y = starty; y <= endy; y++)
{
x += 1/slope;
DrawPixel((int)x, y, color);
}
}
}
对不起,但是,我认为这不是DDA算法。这必须是Brasenham的算法。 – anonymous