You are on page 1of 25

1

INDEX

S.No. Name of Practical Signature

1. Write a program to generate line drawing using DDA


algorithm

2. Write a program to generate line using Bresenham line


drawing algorithm.

3. Write a program to generate Circle using Midpoint


Circle Drawing Algorithm.

4. Write a program to generate Circle using Bresenham


circle algorithm

5. Write a program to generate ellipse using mid point


algorithm

6. Write a program to implement the two dimensional


transformation.
(Translation)

7. Write a program to implement the two dimensional


transformation.
(Rotate About Origin)

8. Write a program to implement the two dimensional


transformation.
(Scaling about Origin)

9. Write a program to generate Bezier curve.

10. Write a menu driven program to generate Line, Circle,


Arc and Polygon.
2

PROGRAM #1

Write a program to generate line drawing using DDA algorithm

# include <graphics.h>
# include <math.h>
# include <conio.h>
# include <iostream.h>
void DDALine(int x1,int y1,int x2,int y2,int iColor);
void main()
{
int gDriver=DETECT,gMode;

int x1,x2,y1,y2,iColor;

initgraph(&gDriver,&gMode,"c:\\tc\\bgi");
cleardevice();
cout<<endl<<"Enter x1 : ";
cin>>x1;
cout<<"Enter y1 : ";
cin>>y1;
cout<<endl<<"Enter x2 : ";
cin>>x2;
cout<<"Enter y2 : ";
cin>>y2;
cout<<endl<<"Enter COLOR : ";
cin>>iColor;
cleardevice();
DDALine(320,1,320,480,12);
DDALine(1,240,640,240,12);
circle(320,240,2);
DDALine(320+x1,240-y1,320+x2,240-y2,iColor%16);
getch();
}

void DDALine(int x1,int y1,int x2,int y2,int iColor)


{
float dX,dY,iSteps;
float xInc,yInc,iCount,x,y;

dX = x1 - x2;
dY = y1 - y2;

if (fabs(dX) > fabs(dY))


{
3

iSteps = fabs(dX);
}
else
{
iSteps = fabs(dY);
}

xInc = dX/iSteps;
yInc = dY/iSteps;

x = x1;
y = y1;
circle(x,y,1);

for (iCount=1; iCount<=iSteps; iCount++)


{
putpixel(floor(x),floor(y),iColor);
x -= xInc;
y -= yInc;
}
circle(x,y,1);
return;
}
4

PROGRAM #2

Write a program to generate line using Bresenham line drawing


algorithm.

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
char IncFlag;
void Bresenham(int x1,int x2,int y1,int y2);
void DrawLine(int X,int Y,int End,int PInc,int NInc,int P,int XInc,int YInc);
void main()
{
int gDriver=DETECT, gMode;
int x1,x2,y1,y2;

void Bresenham(int,int,int,int);
initgraph(&gDriver,&gMode,"c:\\tc\\bgi");

cout<<endl<<"x1 : ";
cin>>x1;
cout<<endl<<"y1 : ";
cin>>y1;
cout<<endl<<"x2 : ";
cin>>x2;
cout<<endl<<"y2 : ";
cin>>y2;
line(320,0,320,480);
line(0,240,640,240);
Bresenham(x1,x2,y1,y2);
getch();
}

void Bresenham(int x1,int x2,int y1,int y2)


{
int S,O,End;
int P;
int dx = abs(x1 - x2);
int dy = abs(y1 - y2);
float Slope;
int PInc,NInc,XInc,YInc;
if (dx == 0) //Slope Infinite
{
}
else
5

{
Slope = (float)(y1 - y2) / (x1 - x2);
if (Slope>-1 && Slope<1)
{
IncFlag = 'X';
PInc = 2 * (dy - dx);
NInc = 2 * dy;
P = 2 * dy - dx;
if (x1>x2)
{
S = x2;
O = y2;
End = x1;
}
else
{
S = x1;
O = y1;
End = x2;
}
// DrawLine(x,y,End,PInc,NInc,P,XInc,YInc);
}
else
{
IncFlag = 'Y';
PInc = 2 * (dx - dy);
NInc = 2 * dx;
P = 2 * dx - dy;
if (y1 > y2)
{
O = x2;
S = y2;
End = y1;
}
else
{
O = x1;
S = y1;
End = y2;
}
}
if (IncFlag == 'X')
putpixel(320+S,240-O,12);
else
putpixel(320+O,240-S,12);
while (S <= End)
6

{
S++;
if (P<0)
P = P + NInc;
else
{
P = P + PInc;
if (Slope>0.0)
O++;
else
O--;
}
if (IncFlag == 'X')
putpixel(320+S,240-O,12);
else
putpixel(320+O,240-S,12);
}
}

}
7

PROGRAM # 3

Write a program to generate Circle using Midpoint Circle Drawing


Algorithm.

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
void Draw(int x,int y,int xC,int yC);
void Circle(int Radius,int xC,int yC);
void main()
{
int gDriver=DETECT, gMode;
initgraph(&gDriver,&gMode,"c:\\tc\\bgi");

int Radius, xC, yC;


cout<< endl << "Enter Center point coordinates...";
cout<<endl<<" Xc : ";
cin>>xC;
cout<<endl<<" Xc : ";
cin>>yC;
cout<<endl<<"Radius : ";
cin>>Radius;
cleardevice();
Circle(Radius,xC,yC);
getch();
return;
}
void Circle(int Radius,int xC,int yC)
{
int P;
int x,y;
void Draw(int x,int y,int xC,int yC);
P = 1 - Radius;
x = 0;
y = Radius;
Draw(x,y,xC,yC);
while (x<=y)
{
x++;
if (P<0)
{
P += 2 * x + 1;
}
else
8

{
P += 2 * (x - y) + 1;
y--;
}
Draw(x,y,xC,yC);
}

}
void Draw(int x,int y,int xC,int yC)
{
putpixel(xC + x,yC + y,12);
putpixel(xC + x,yC - y,12);
putpixel(xC - x,yC + y,12);
putpixel(xC - x,yC - y,12);
putpixel(xC + y,yC + x,12);
putpixel(xC - y,yC + x,12);
putpixel(xC + y,yC - x,12);
putpixel(xC - y,yC - x,12);
}
9

PROGRAM # 4

Write a program to generate Circle using Bresenham circle algorithm.

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void bresenham_circle(const int,const int,const int);
int main( )
{
int driver=VGA;
int mode=VGAHI;

int h=0;
int k=0;
int r=0;

do
{
cout<<"\nCentral Point of the Circle : (h,k) :";
cout<<"\nEnter the value of h = ";
cin>>h;
cout<<"\nEnter the value of k = ";
cin>>k;
cout<<"\nRadius of the Circle : r :";
cout<<"\nEnter the value of r = ";
cin>>r;
initgraph(&driver,&mode,"c:\\tc\\bgi");
setcolor(15);
bresenham_circle(h,k,r);
setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to exit.");

int key=int(getch( ));

if(key!=13)
break;
}
while(1);

return 0;
}

void bresenham_circle(const int h,const int k,const int r)


10

{
int color=getcolor( );

int x=0;
int y=r;
int p=(3-(2*r));

do
{
putpixel((h+x),(k+y),color);
putpixel((h+y),(k+x),color);
putpixel((h+y),(k-x),color);
putpixel((h+x),(k-y),color);
putpixel((h-x),(k-y),color);
putpixel((h-y),(k-x),color);
putpixel((h-y),(k+x),color);
putpixel((h-x),(k+y),color);

x++;

if(p<0)
p+=((4*x)+6);

else
{
y--;
p+=((4*(x-y))+10);
}
}
while(x<=y);
}
11

PROGRAM # 5

Write a program to generate ellipse using mid point algorithm.

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void midpoint_ellipse(const int,const int,const int,const int);
int main( )
{
int driver=VGA;
int mode=VGAHI;
int h=0;
int k=0;
int rx=0;
int ry=0;
do
{
cout<<"Central Point of the Ellipse : (h,k) :";
cout<<"Enter the value of h = ";
cin>>h;
cout<<"Enter the value of k = ";
cin>>k;
cout<<"Radius of the Ellipse : (rx,ry) :";
cout<<"Enter the radius along x-axis : rx = ";
cin>>rx;
cout<<"Enter the radius along y-axis : ry = ";
cin>>ry;

initgraph(&driver,&mode,"..\\Bgi");

setcolor(15);
midpoint_ellipse(h,k,rx,ry);

setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to exit.");

int key=int(getch( ));

if(key!=13)
break;
}
while(1);

return 0;
12

}
void midpoint_ellipse(const int h,const int k,const int a,const int b)
{
int color=getcolor( );

float aa=(a*a);
float bb=(b*b);
float aa2=(aa*2);
float bb2=(bb*2);

float x=0;
float y=b;

float fx=0;
float fy=(aa2*b);

float p=(int)(bb-(aa*b)+(0.25*aa)+0.5);

putpixel((h+x),(k+y),color);
putpixel((h+x),(k-y),color);
putpixel((h-x),(k-y),color);
putpixel((h-x),(k+y),color);

while(fx<fy)
{
x++;
fx+=bb2;

if(p<0)
p+=(fx+bb);

else
{
y--;
fy-=aa2;
p+=(fx+bb-fy);
}

putpixel((h+x),(k+y),color);
putpixel((h+x),(k-y),color);
putpixel((h-x),(k-y),color);
putpixel((h-x),(k+y),color);
}

p=(int)((bb*(x+0.5)*(x+0.5))+(aa*(y-1)*(y-1))-(aa*bb)+0.5);
13

while(y>0)
{
y--;
fy-=aa2;

if(p>=0)
p+=(aa-fy);

else
{
x++;
fx+=bb2;
p+=(fx+aa-fy);
}

putpixel((h+x),(k+y),color);
putpixel((h+x),(k-y),color);
putpixel((h-x),(k-y),color);
putpixel((h-x),(k+y),color);
}
}
14

PROGRAM # 6

Write a program to implement the two dimensional transformation.


(Translation)

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
char IncFlag;
int PolygonPoints[4][2] =
{{10,10},{10,100},{100,100},{100,10}};
float Tx=10;
float Ty=10;
void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
for (iCnt=0; iCnt<4; iCnt++)
{
line(320+PolygonPoints[iCnt][0],
240-PolygonPoints[iCnt][1],
320+PolygonPoints[(iCnt+1)%4][0],
240-PolygonPoints[(iCnt+1)%4][1]);
}}
void Translate()
{
int iCnt;
cout<<endl;
for (iCnt=0; iCnt<4; iCnt++)
{
PolygonPoints[iCnt][0] += Tx;
PolygonPoints[iCnt][1] += Ty;
}}
void main()
{
int gDriver = DETECT, gMode;
int iCnt;
initgraph(&gDriver, &gMode, "C:\\TC\\BGI");
PolyLine();
getch();
Translate();
PolyLine();
getch(); }
15

PROGRAM # 7

Write a program to implement the two dimensional transformation.


(Rotate About Origin)

# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>

char IncFlag;
int PolygonPoints[4][2] =
{{10,100},{110,100},{110,200},{10,200}};

void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
for (iCnt=0; iCnt<4; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%4][0],PolygonPoints[(iCnt+1)%4][1]);
}
}
void Rotate()
{
float Angle;
int iCnt;
int Tx,Ty;
cout<<endl;
Angle = 30.0*(22.0/7.0)/180.0;
for (iCnt=0; iCnt<4; iCnt++)
{
Tx = PolygonPoints[iCnt][0];
Ty = PolygonPoints[iCnt][1];
PolygonPoints[iCnt][0] = (Tx - 320)*cos(Angle) -
(Ty - 240)*sin(Angle) + 320;
PolygonPoints[iCnt][1] = (Tx - 320)*sin(Angle) +
(Ty - 240)*cos(Angle) + 240;
}
}

void main()
{
16

int gDriver = DETECT, gMode;


int iCnt;
initgraph(&gDriver, &gMode, "C:\\TC\\BGI");
for (iCnt=0; iCnt<4; iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine();
getch();
Rotate();
PolyLine();
getch();
}
17

PROGRAM #8

Write a program to implement the two dimensional transformation.


(Scaling about Origin)
# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <math.h>
char IncFlag;
int PolygonPoints[4][2] =
{{10,10},{10,100},{100,100},{100,10}};
float Sx=0.5;
float Sy=2.0;
void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
for (iCnt=0; iCnt<4; iCnt++)
{
line(320+PolygonPoints[iCnt][0],240-PolygonPoints[iCnt][1],
320+PolygonPoints[(iCnt+1)%4][0],240-PolygonPoints[(iCnt+1)%4]
[1]);
}}
void Scale()
{
int iCnt;
int Tx,Ty;
cout<<endl;
for (iCnt=0; iCnt<4; iCnt++)
{
PolygonPoints[iCnt][0] *= Sx;
PolygonPoints[iCnt][1] *= Sy;
}}
void main()
{
int gDriver = DETECT, gMode;
int iCnt;
initgraph(&gDriver, &gMode, "C:\\TC\\BGI");
PolyLine();
getch();
Scale();
PolyLine();
getch();
}
18

PROGRAM # 9
Write a program to generate Bezier curve.

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void Bezier_curve(const int [8]);
double nCr(int,int);
double factorial(int);
void Dashed_line(const int,const int,const int,const int,const int=0);
int main( )
{
int driver=VGA;
int mode=VGAHI;
int control_points[8]={0};
do
{
for(int count=0;count<=3;count++)
{
cout<<"Coordinates of Point-"<<count<<"
(x"<<count<<",y"<<count<<") :";
cout<<"Enter the value of x"<<count<<" = ";
cin>>control_points[(count*2)];
cout<<"Enter the value of y"<<count<<" = ";
cin>>control_points[((count*2)+1)];
cout<<" ";
cout<<" ";
cout<<" ";
}

initgraph(&driver,&mode,"..\\Bgi");
setcolor(15);
Bezier_curve(control_points);

setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to exit.");

int key=int(getch( ));

if(key!=13)
break;
}
while(1);

return 0;
19

}
void Bezier_curve(const int cp[8])
{
int color=getcolor( );

setcolor(7);

for(int count=0;count<3;count++)
Dashed_line(cp[(count*2)],cp[((count*2)+1)],
cp[((count+1)*2)],cp[(((count+1)*2)+1)]);

float x;
float y;

for(float u=0.0005;u<=1;u+=0.0005)
{
x=0;
y=0;

for(int k=0;k<=3;k++)
{
x+=(cp[(k*2)]*nCr(3,k)*pow(u,k)*powl((1-u),(3-k)));
y+=(cp[((k*2)+1)]*nCr(3,k)*pow(u,k)*powl((1-u),(3-k)));
}

putpixel((int)(x+0.5),(int)(y+0.5),color);
}
}
double nCr(int n,int r)
{
double nf;
double rf;
double nrf;
double ncr;

nf=factorial(n);
rf=factorial(r);
nrf=factorial((n-r));

ncr=(nf/(rf*nrf));

return ncr;
}

double factorial(int number)


{
20

double factorial=1;

if(number==0 || number==1);

else
{
for(int count=1;count<=number;count++)
factorial=factorial*count;
}

return factorial;
}

void Dashed_line(const int x_1,const int y_1,const int x_2,


const int y_2,const int line_type)
{
int count=0;
int color=getcolor( );

int x1=x_1;
int y1=y_1;

int x2=x_2;
int y2=y_2;

if(x_1>x_2)
{
x1=x_2;
y1=y_2;

x2=x_1;
y2=y_1;
}

int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);

if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);

int x=x1;
int y=y1;
21

putpixel(x,y,color);

while(x<x2)
{
x++;

if(p<0)
p+=two_dy;

else
{
y+=inc_dec;
p+=two_dy_dx;
}

if((count%2)!=0 && line_type==0)


putpixel(x,y,color);

else if((count%5)!=4 && line_type==1)


putpixel(x,y,color);

else if((count%10)!=8 && (count%10)!=9 && line_type==2)


putpixel(x,y,color);

else if((count%20)!=18 && (count%20)!=19 && line_type==3)


putpixel(x,y,color);

else if((count%12)!=7 && (count%12)!=8 &&


(count%12)!=10 && (count%12)!=11 && line_type==4)
putpixel(x,y,color);

count++;
}
}

else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);

int x=x1;
int y=y1;

putpixel(x,y,color);
22

while(y!=y2)
{
y+=inc_dec;

if(p<0)
p+=two_dx;

else
{
x++;
p+=two_dx_dy;
}

if((count%2)!=0 && line_type==0)


putpixel(x,y,color);

else if((count%5)!=4 && line_type==1)


putpixel(x,y,color);

else if((count%10)!=8 && (count%10)!=9 && line_type==2)


putpixel(x,y,color);

else if((count%20)!=18 && (count%20)!=19 && line_type==3)


putpixel(x,y,color);

else if((count%12)!=7 && (count%12)!=8 &&


(count%12)!=10 && (count%12)!=11 && line_type==4)
putpixel(x,y,color);

count++;
}
}
}
23

PROGRAM # 10
Write a menu driven program to generate Line, Circle, Arc and
Polygon.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void circle1()
{ int d,x,y,r;
printf("Enter the radius of the circle: ");
scanf("%d",&r);
x=0;
y=r;
putpixel(x+320,240-y,2);
putpixel(y+320,240-x,2);
putpixel((-y)+320,240-x,2);
putpixel(x+329,240+y,2);
d=3-(2*r);
while(x<=y)
{
x++;
if(d<0)
d=d+(4*x)+6;
else
{ d=d+(4*(x-y))+10;
y--;
}
putpixel(x+320,240-y,2);
putpixel(y+320,240-x,2);
putpixel(-y+320,240-x,2);
putpixel(-x+320,240-y,2);
putpixel(-x+320,240+y,2);
putpixel(-y+320,240+x,2);
putpixel(y+320,240+x,2);
putpixel(x+320,240+y,2);
}
}

void arc1()
{ int d, x,y,r;
printf("\nEnter the radius of the circle: ");
scanf("%d",&r);
x=0;
y=r;
putpixel(x+320,240-y,2);
putpixel(y+320,240-x,2);
d=3-(2*r);
24

while(x<=y)
{ x++;
if(d<0)
d=d+(4*x)+6;
else
{ d=d+(4*(x-y))+10;
y--;
}
putpixel(x+320,240-y,2);
putpixel(y+320,240-x,2);
}
}

void line1(int x1, int y1, int x2, int y2)


{ int dx, dy, steps, k;
float x, y,xinc, yinc;
dx=x2-x1;
dy=y2-y1;
if (abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;
y=y1;
putpixel(x,y,2);
for(k=0;k<=steps;k++)
{ x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
}

void polygon1()
{ int i, j, k,n, pts[50][2],x1,y1,x2,y2;
printf("\nEnter the no. of coordinates of N: ");
scanf("%d",&n);
printf("\nEnter the coordinates in cyclic order.");
cleardevice();
for(i=0;i<n;i++)
{ printf("\nEnter the value of x coordinate: ");
scanf("%d",&pts[i][0]);
printf("\nEnter the value of y coordinate: ");
scanf("%d",&pts[i][1]);
}
cleardevice();
for(i=0;i<n;i++)
{
if(i==n-1)
{
x1=pts[i][0];
25

y1=pts[i][1];
x2=pts[0][0];
y2=pts[0][1];
line(x1,y1,x2,y2);
break;
}
x1=pts[i][0];
y1=pts[i][1];
i++;
x2=pts[i][0];
y2=pts[i][1];
line1(x1,y1,x2,y2);
i--;
}
}
void main()
{ int i,j,k,x1,x2,y1,y2;
int gdriver=DETECT,gmode;
char ch;
clrscr();
printf("\nMENU:");
printf("\n1.Draw a line");
printf("\n2.Draw a circle");
printf("\n3.Draw an arc");
printf("\n4.Draw a polygon");
printf("\n5.Exit");
printf("\nEnter your choice(1-5):");
scanf("%c",&ch);
initgraph(&gdriver,&gmode,"D:\\softwa~1\\tc\\bgi");
switch(ch)
{ case'1':printf("\nEnter the value of x1:");
scanf("%d",&x1);
printf("\nEnter the value of y1:");
scanf("%d",&y1);
printf("\nEnetr the value of x2:");
scanf("%d",&x2);
printf("\nEnter the value of y2:");
scanf("%d",&y2);
line1(x1,y1,x2,y2);
break;
case'2':circle1();
break;
case'3':arc1();
break;
case'4':polygon1();
break;
case'5':exit(0);
break;
default:exit(0);
}
getch();
}

You might also like