Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

DDA Algorithm:

Consider one point of the line as (X0, Y0) and the second point of the line as (X1,
Y1).
// calculate dx , dy
dx = X1 – X0;
dy = Y1 – Y0;
// Depending upon absolute value of dx & dy
// choose number of steps to put pixel as
// steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy)
steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
// calculate increment in x & y for each steps
Xinc = dx / (float) steps;
Yinc = dy / (float) steps;
// Put pixel for each step
X = X0;
Y = Y0;
for (int i = 0; i <= steps; i++)
{
putpixel (round(X),round(Y),WHITE);
X += Xinc;
Y += Yinc;
}

// Java Code for DDA line generation


public class Solution {

// function for rounding off the pixels


public static int round(float n) {
if (n - (int) n < 0.5)
return (int) n;
return (int) (n + 1);
}

// Function for line generation


public static void DDALine(int x0, int y0, int x1, int y1) {

// Calculate dx and dy
int dx = x1 - x0;
int dy = y1 - y0;

int step;
// If dx > dy we will take step as dx
// else we will take step as dy to draw the complete
// line
if (Math.abs(dx) > Math.abs(dy))
step = Math.abs(dx);
else
step = Math.abs(dy);

// Calculate x-increment and y-increment for each step


float x_incr = (float) dx / step;
float y_incr = (float) dy / step;

// Take the initial points as x and y


float x = x0;
float y = y0;

for (int i = 0; i < step; i++) {

// putpixel(round(x), round(y), WHITE);


System.out.println(round(x) + " " + round(y));
x += x_incr;
y += y_incr;
// delay(10);
}
}

// Driver code
public static void main(String[] args) {

int x0 = 200, y0 = 180, x1 = 180, y1 = 160;

// Function call
DDALine(x0, y0, x1, y1);

}
}

You might also like