Digital differential analyzer (graphics algorithm): Difference between revisions

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.
 
== SampleThe Codealgorithm in code ==
{{citation}}
This is a sample implementation of the DDA implementationalgorithm in the [[C programming language]]:
 
<pre>
void/* Draw a straight line DDA(intbetween the points (xa, int ya,) intand (xb, int yb) */
void line DDA(int xa, int ya, int xb, int yb) {
{
int dx=xb-xa, dy=yb-ya, steps, k;
floatint xIncrement, yIncrement, xdx=xb-xa,; y=ya;// horisontal difference
int dy=yb-ya; // vertical difference
for(k=0;int k<steps;, k++);
float xIncrement, yIncrement;
float x=xa, y=ya; // the drawing points x and y are initiated to one endpoint of the line
 
if(abs(dx)>abs(dy))// calculate the number of steps=abs used to draw the line (dxnumber of pixels);
else steps=if(abs(dx)>abs(dy); )
steps=abs(dx);
{else
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; x k+=+) xIncrement;{
y += yIncrement;
// the points are setPixel(ROUND(x),incremented ROUND(y));an equal amount for every step until the whole line has been drawn
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 ==