S.No. Name of Practical Signature: Index
S.No. Name of Practical Signature: Index
INDEX
PROGRAM #1
# 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();
}
dX = x1 - x2;
dY = y1 - y2;
iSteps = fabs(dX);
}
else
{
iSteps = fabs(dY);
}
xInc = dX/iSteps;
yInc = dY/iSteps;
x = x1;
y = y1;
circle(x,y,1);
PROGRAM #2
# 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();
}
{
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
# 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");
{
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
# 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.");
if(key!=13)
break;
}
while(1);
return 0;
}
{
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
# 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.");
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
# 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
# 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
PROGRAM #8
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.");
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=1;
if(number==0 || number==1);
else
{
for(int count=1;count<=number;count++)
factorial=factorial*count;
}
return factorial;
}
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;
}
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;
}
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 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();
}