0% found this document useful (0 votes)
21 views6 pages

CG 1

The document discusses three algorithms for drawing lines on a graph: 1) Simple DDA which calculates the slope and increments x or y by 1 each iteration 2) Symmetric DDA which calculates the slope and increments both x and y by a fraction of the difference between endpoints each iteration 3) Bresenham's algorithm which more accurately calculates the pixel points of a line for any slope It provides pseudocode and a C++ program for implementing Simple DDA and Symmetric DDA.

Uploaded by

Rahul Premani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

CG 1

The document discusses three algorithms for drawing lines on a graph: 1) Simple DDA which calculates the slope and increments x or y by 1 each iteration 2) Symmetric DDA which calculates the slope and increments both x and y by a fraction of the difference between endpoints each iteration 3) Bresenham's algorithm which more accurately calculates the pixel points of a line for any slope It provides pseudocode and a C++ program for implementing Simple DDA and Symmetric DDA.

Uploaded by

Rahul Premani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Chandigarh University

Practical 3
Aim-To draw a line with positive line slope using
a)Simple DDA
b)Symmetric DDA
c) Bresenham algorithm

Objective:- a)Drawn a line using Simple DDA.


Flow chart:

Start

Read Starting and


Ending Points

dx = X1 - X0
dy = Y1 - Y0
m=dy-dx

M<1
True

False X i+1= X I + X I +1
Y i+1= Y I + m

M<1
True

False
Yi+1 = Y I+ YI +1
X i+1= X I + X I +1
Xi+1 = X I+ X+1/m
Yi+1 = Y I+ Y I +1

Stop
Chandigarh University

Algorithm:
Step 1: Read the Starting and Ending Points of the Line.
Step 2: Calculate the difference between two end points.
dx = X1 - X0
dy = Y1 - Y0
Step 3: Calculate m= dy/dx
Step 4: if (m<1)
Then:
X i+1= X I + X I +1
Y i+1= Y I + m
Elseif(m>1)
Then:
Yi+1 = Y I+ YI +1
Xi+1 = X I+ X+1/m
Else:
X i+1= X I + X I +1
Yi+1 = Y I+ Y I +1
Step 5: Stop

Program Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void drawline(float,float,float,float,float);
void main ()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"C://TURBOC3//BGI");
float x1,y1,x2,y2,m;
cout<<"\nEnter starting coordinates :";
cin>>x1>>y1;
cout<<"\nEnter ending coordinates :";
cin>>x2>>y2;
m=(y2-y1)/(x2-x1);
cout<<"\nSlope :"<<m;
drawline(x1,y1,x2,y2,m);
getch ();
}
voiddrawline(float x1,float y1,float x2,float y2,float m)
{ floatx,y;
if(m<1&&m>=0)
{y=y1;
for(x=x1;x<=x2;x++)
{ putpixel(x,y,YELLOW);
Chandigarh University

y=y+m;
abs(y);
}
}
else if(m>1)
{
x=x1;
for(y=y1;y<=y2;y++)
{
putpixel(x,y,YELLOW);
x=x+(1/m);
abs(x);
}
}
else if(m==1)
{
y=y1;
for(x=x1;x<=x2;x++)
{
putpixel(x,y,YELLOW);
y=y+1;
}
}
else
cout<<"\nNegative slope Line out of bound...";
}
OUTPUT
Chandigarh University

Objective:- b) Drawn a line using Symmetric DDA.


Flowchart:

Start

Read Starting and


Ending Points

dx = X1 - X0
dy = Y1 - Y0

(abs(dx)>abs(dy))

length =abs(dx); length= abs(dy);

log10(length)/log10(2)

xin=dx/n2
yin=dy/n2

X=X0

False X<=X1 x=x+yin;

putpixel(x,y,
TRUE RED);
y=y+yin;
Stop putpixel(x,y,
RED); putpixel(x,y,
RED);

putpixel(x,y,
Chandigarh University

Algorithm:
Step 1: Read the Starting and Ending Points of the Line.
Step 2: Calculate the difference between two end points.
dx = X1 - X0
dy = Y1 - Y0
Step 3:
if (abs(dx)>abs(dy))
length =abs(dx);
else
length= abs(dy);

Step 4: n= log10(length)/log10(2) ;

Step 5: xin=dx/pow(2,n);
yin=dy/pow(2,n);

Step 6:for(x=x1;x<=x2;x+=xin)
{ y=y+yin;
putpixel(x,y,RED);
}
Step 7: Stop

Program Code:
#include<conio.h>
#include<iostream.h>
#include<math.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"c://TURBOC3//BGI");
float x1,x2,y1,y2,dx,dy,n;
floatxin,yin,x,y,length;
cout<<"\n\n\n\nEnter the starting points";
cin>>x1>>y1;
cout<<"Enter the ending points" ;
cin>>x2>>y2;
x=x1;
y=y1;
putpixel(x,y,RED);
dx=x2-x1;
dy=y2-y1;

if (abs(dx)>abs(dy))
length =abs(dx);
else
Chandigarh University

length= abs(dy);

n= log10(length)/log10(2) ;

xin=dx/pow(2,n);
yin=dy/pow(2,n);

for(x=x1;x<=x2;x+=xin)
{

y=y+yin;

putpixel(x,y,RED);

}
getch();
}

OUTPUT

You might also like