You are on page 1of 5

Computer Graphics Lab

(CSP-305)
Worksheet-2

Submitted to: Submitted By:


Er. Udit Jindal Samridh Garg
CSE-5(C)
19BCS1815
Q1. To draw a line using DDA line drawing Algorithm for positive and negative line
slope.
Answer:
Aim: To draw a line using DDA line drawing Algorithm for positive and negative line
slope.
DDA stands for Digital Differential Analyzer. It is an incremental method of scan
conversion of line. In this method calculation is performed at each step but by using
results of previous steps
Algorithm:
Suppose at step i, the pixels is (xi,yi)
The line of equation for step i
yi=mxi+b......................equation 1
Next value will be
yi+1=mxi+1+b.................equation 2

m=
yi+1-yi=∆y.......................equation 3
yi+1-xi=∆x......................equation 4
yi+1=yi+∆y
∆y=m∆x
yi+1=yi+m∆x
∆x=∆y/m
xi+1=xi+∆x
xi+1=xi+∆y/m
Case1: When |M|<1 then (assume that x12)
x= x1,y=y1 set ∆x=1
yi+1=y1+m, x=x+1
Until x = x2

Case2: When |M|<1 then (assume that y12)


x= x1,y=y1 set ∆y=1
xi+1=DDA Algorithm, y=y+1
Until y → y2
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
Step7: xinc=dx/step
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))
Step10: Repeat step 9 until x = x2
Step11: End Algorithm
Source code:
#include<graphics.h>
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
int gd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm,NULL);
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, WHITE);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}
Output:

For positive slope

(b) For negative slope

You might also like