DDA - Digital Differential Analyzer
• DDA stands for Digital Differential Analyzer.
• getpixel and putpixel are two functions which we will use everytime in our program.
• Declarations:
1) int getpixel(int x, int y);
2) void putpixel(int x, int y, int color);
• Function getpixel returns the integer value of the pixel color present at point(x, y).
• Function putpixel plots a pixel at a point(x, y) of the specified color.
•Steps:
1) slope of line(m): y2-y1/x2-x1
** m= dy/dx = y2-y1/x2-x1
2) dy= m*dx
3) dx= dy/m
• Above three formulas are very important in order to understand DDA Algorithm.
• Algorithm:
1) Start
2) User Input: Coordinate x1, y1 and x2, y2 for line.
3) Find slope m
4) Find dx
5) Find dy with the help of above formula.
6) Check: dx >=dy if yes,
dx = 1
x1= x1 + dx;
y1 = y1 + m1 * dx;
7) else:
dy1 = 1;
x1 = x1 +(dy1/m1);
y1 = y1 + dy1;
8)putpixel(x1,y1,color_name);
9) We will continue the steps from 4 to 8 till x1==x2 and y1==y2
• Example:
If x1 and y1= (1,1)
X2 and y1= (4,3)
Then:
x1=1
y1=1
x2= 4
y2=3
dx = 4 - 1 = 3
dy = 3 - 1= 2
m = dy/dx
= 2/3
= 0.67
1) Check dx>=dy
dx= 1
x1= x1 +dx
= 1 + 1
x1= 2
y1= y1 + dy
= 1 + m*dx
// dy= m*dx
= 1 + 0.67*1
= 1.67
x1= 2 and y1= 1.67
Find
dx= x2-x1= 4-2= 2
dy= y2-y1= 3-1= 2. // 1.67 as int
2) Check dx>=dy yes,
dx= 1
x1= x1 +dx
= 2 + 1.
// x1 = 2 previous value
x1= 3
y1= y1 + dy
= 1.67 + m*dx
= 1.67 + 0.67* 1
y1= 2.34
x1= 3 and y1= 2
Find
dx= x2-x1= 4 -3= 1
dy= y2-y1= 3 -2= 1
3) Check dx>=dy yes,
dx= 1
x1= x1 +dx
=3 + 1.
x1= 4
y1= y1 + dy
= 2.34+ m*dx
= 2.34 + 0.67* 1
y1= 3.01
x1= 4 and y1= 3
• Hence we can stop here though we got the end points.
• Refer the program at this link:
https://github.com/Rinky6767/Computer-Graphics/blob/main/DDA%20line%20drawing%20program
Output:
*You can leave a comment if you have doubt in it.
Comments
Post a Comment