Content deleted Content added
No edit summary |
Commented the sample code to make the algorithm clearer |
||
Line 5:
The DDA method is not very efficient due to the need for division and rounding. [[Bresenham's line algorithm]] is a more efficient method to draw lines because it uses only addition, subtraction and bit shifting.
==
{{citation}}
This is a sample implementation of the DDA
<pre>
void line DDA(int xa, int ya, int xb, int yb) {
int dy=yb-ya; // vertical difference
float xIncrement, yIncrement;
float x=xa, y=ya; // the drawing points x and y are initiated to one endpoint of the line
steps=abs(dx);
steps=abs(dy);
// calculate the increment for x and y in each step
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;
// point out the starting pixel
setPixel(ROUND(x), ROUND(y));
▲ for(k=0; k<steps; k++)
// loop through the line from one end to the other and point out the pixels
▲ {
for(k=0; k<steps;
// the points are
x += xIncrement;
y += yIncrement;
// since the values for x and y are float they are rounded before they are plotted
setPixel(ROUND(x), ROUND(y));
}
}
</pre>
== External links ==
|