I have tested all the cases of how a line could be
- vertically
- horizontally
- has a slope that's positive or less than 1
The function works, but I would like it reviewed, such as if there are overflows, etc.
// 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
for(int y = y1;y <= y2; y++)
{
x += 1/slope;
DrawPixel((int)x, y, color);
}
}
}