DDA Algorithm:
Step1: Start Algorithm
Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.
Step3: Enter value of x1,y1,x2,y2.
Step4: Calculate dx = x2-x1
Step5: Calculate dy = y2-y1
Step6: If ABS (dx) > ABS (dy)
Then step = abs (dx)
Else
Then step = abs (dx)
Else
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
yinc=dy/step
assign x = x1
assign y = y1
Step8: Set pixel (x, y)
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
Step11: End Algorithm
Program to implement DDA Line Drawing Algorithm:
- #include<graphics.h>
- #include<conio.h>
- #include<stdio.h>
- void main()
- {
- intgd = DETECT ,gm, i;
- float x, y,dx,dy,steps;
- int x0, x1, y0, y1;
- initgraph(&gd, &gm, "C:\\TC\\BGI");
- setbkcolor(WHITE);
- x0 = 100 , y0 = 200, x1 = 500, y1 = 300;
- dx = (float)(x1 - x0);
- dy = (float)(y1 - y0);
- if(dx>=dy)
- {
- steps = dx;
- }
- else
- {
- steps = dy;
- }
- dx = dx/steps;
- dy = dy/steps;
- x = x0;
- y = y0;
- i = 1;
- while(i<= steps)
- {
- putpixel(x, y, RED);
- x += dx;
- y += dy;
- i=i+1;
- }
- getch();
- closegraph();
- }
Output:
Example: If a line is drawn from (2, 3) to (6, 15) with use of DDA. How many points will needed to generate such line?
Solution: P1 (2,3) P11 (6,15)
x1=2
y1=3
x2= 6
y2=15
dx = 6 - 2 = 4
dy = 15 - 3 = 12
m =
y1=3
x2= 6
y2=15
dx = 6 - 2 = 4
dy = 15 - 3 = 12
m =
For calculating next value of x takes x = x +
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें