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

Drawing Lines with DDA and Bresenham

The document discusses drawing lines in C++ using three different algorithms: 1) The DDA (Digital Differential Analyzer) algorithm is used to draw a line with a negative slope. It takes x and y coordinates of start and end points as input and uses incremental steps of xinc and yinc to reach the end point. 2) The symmetrical DDA algorithm is also used to draw a line with negative slope. It works similarly but uses powers of 2 to determine incremental steps for improved efficiency. 3) Bresenham's line algorithm is also demonstrated, which produces integer coordinates for pixel positions on the line to avoid fractions and is more efficient than DDA for negative slopes. It uses decision parameters to determine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views6 pages

Drawing Lines with DDA and Bresenham

The document discusses drawing lines in C++ using three different algorithms: 1) The DDA (Digital Differential Analyzer) algorithm is used to draw a line with a negative slope. It takes x and y coordinates of start and end points as input and uses incremental steps of xinc and yinc to reach the end point. 2) The symmetrical DDA algorithm is also used to draw a line with negative slope. It works similarly but uses powers of 2 to determine incremental steps for improved efficiency. 3) Bresenham's line algorithm is also demonstrated, which produces integer coordinates for pixel positions on the line to avoid fractions and is more efficient than DDA for negative slopes. It uses decision parameters to determine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical No-2(a)

Aim: -To draw a line using Simple DDA Algorithm for negative line slope.
#include<iostream.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
void dda_line(int x1,int y1,int x2,int y2)
{
int dx=(x2-x1);
int dy=(y2-y1);
int length;
if(abs(dy)>abs(dx))
length=abs(dy);
else
length=abs(dx);
float xinc,yinc,x=x1,y=y1;
xinc=dx/(float)length;
yinc=dy/(float)length;

putpixel(round(x),round(y),15);
for(int k=1;k<=length;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),15);
delay(100);
}
}
void main()
{
clrscr();
int x1,x2,y1,y2;
int gd=DETECT,gm;
cout<<"Enter the x-coordinate of starting point : ";
cin>>x1;
cout<<"Enter the y-coordinate of ending point : ";
cin>>y1;

[Link] 1
cout<<endl;
cout<<"Enter the x-coordinate of starting point : ";
cin>>x2;
cout<<"Enter the y-coordinate of ending point : ";
cin>>y2;
getch();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
dda_line(x1,y1,x2,y2);
setcolor(4);
getch();
closegraph();
}

Output:-

[Link] 2
Practical No-2(b)

Aim: - To draw a line using Symmetrical DDA for negative line slope.

#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#define ROUND(a)((int)(a+0.5))
void symDDA(int xa,int ya,int xb,int yb)
{
int dx=xb-xa,dy=yb-ya;float length;
float xinc,yinc,x=xa,y=ya;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dy);
float n=log10(length)/log10(2);
xinc=dx/(pow(2,n));
yinc=dy/(pow(2,n));
putpixel(ROUND(x),ROUND(y),15);
delay(50);

for(int i=0;i<length;i++)
{
x=x+xinc;
y=y+yinc;
putpixel(ROUND(x),ROUND(y),15);
delay(50);
}
}
void main()
{

[Link] 3
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int xa,xb,ya,yb;
cout<<"enter the points";
cin>>xa>>xb>>ya>>yb;
cleardevice();
symDDA(xa,xb,ya,yb);
getch();
closegraph();
}

Output:-

[Link] 4
Practical No-2(c)

Aim: - To draw a line using Bresenham’s Algorithm for negative line slope.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
int sign(int x)
{ if(x<0)
return(-1);
if(x>0)
return(1);
else
return(0);
}
void lineBres(int xa,int ya,int xb,int yb)
{ int sx,sy,t,length,flag;
int x=xa;
int y=ya;
int dx=abs(xa-xb),dy=abs(ya-yb);
sx=sign(xb-xa);
sy=sign(yb-ya);
if(dy>dx)
{ t=dx;
dx=dy;
dy=t;
length=dy;
flag=1;}
else
{ length=dx;
flag=0;}
int p=(2*dy)-dx;
int twoDx=2*dx,twoDy=2*dy;
putpixel(x,y,15);
delay(50);
for(int i=0;i<length;i++)
{while(p>0)
{if(flag==1)
x=x+sx;

[Link] 5
else
y=y+sy;
p=p-twoDx;}
if(flag==1)
y=y+sy;
else
{x=x+sx;
p=p+twoDy;
putpixel(x,y,15);
delay(50);
}}}
void main()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
int xa,ya,xb,yb;
cout<<"Enter the starting point of x :";
cin>>xa;
cout<<"Enter the starting point of y :";
cin>>ya;
cout<<"Enter the ending point of x :";
cin>>xb;
cout<<"Enter the ending point of x :";
cin>>yb;
cleardevice();
lineBres(xa,ya,xb,yb);
getch();
closegraph();}

[Link] 6

You might also like