Practical No. 1: Anita
Practical No. 1: Anita
ANITA
Practical No. 1
Program:
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
float x1,y1,x2,y2,max,dx,dy,xinc,yinc;
int gd=DETECT,gm,i=0;
initgraph(&gd,&gm," ");
scanf("%f%f",&x1,&y1);
scanf("%f%f",&x2,&y2);
cleardevice();
dx=x2-x1;
dy=y2-y1;
if(dx>=dy)
max=dx;
else
max=dy;
xinc=dx/max;
ANITA
yinc=dy/max;
putpixel(x1,y1,WHITE);
while(i<=max)
i++;
x1=x1+xinc;
y1=y1+yinc;
putpixel(x1,y1,WHITE);
getch();
ANITA
Practical No. 2
Aim:To draw a line using Bresenham’s line drawing algorithm.
Algorithm:
Step1:Input starting and ending co-ordinates i.e. (x1,y1) and (x2,y2).
Step2:x0=x1; y0=y1;
Step3:Plot x0,y0;
Step 3:dx=x2-x1; dy=y2-y1;
Step 4:if(dy<dx)
{
p0=(2*dy)-dx;
while(x0<=x2 && y0<=y2)
{
if(pk<0)
{
Plot(xk+1,yk);
pk+1=pk+(2*dy);
}
else
{
Plot(xk+1,yk+1)
pk+1=pk+(2*dy)-(2*dx);
ANITA
}
}
}
Step 5:else
{
p0=(2*dx)-dy;
while(x0<=x2 && y0<=y2)
{
if(pk<0)
{
Plot(xk,yk+1);
pk+1=pk+(2*dx);
}
else
{
Plot(xk+1,yk+1);
pk+1=pk+(2*dx)-(2*dy);
}
}
}
Step 7:Exit
ANITA
Practical No. 2
Program:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
double x1,y1,x2,y2,dx,dy,p0;
cin>>x1>>y1;
cin>>x2>>y2;
cleardevice();
dx=x2-x1;
dy=y2-y1;
double x0=x1;
double y0=y1;
putpixel(x0,y0,WHITE);
if(dy<dx)
{
ANITA
p0=(2*dy)-dx;
x0++;
if(p0<0)
p0=p0+(2*dy);
else
y0++;
p0=p0+(2*dy)-(2*dx);
putpixel(x0,y0,WHITE);
else
p0=(2*dx)-dy;
y0++;
ANITA
if(p0<0)
p0=p0+(2*dx);
else
x0++;
p0=p0+(2*dx)-(2*dy);
putpixel(x0,y0,WHITE);
getch();
ANITA
Practical No. 3
Aim : To draw circle using Bresenham’s Algorithm
Algorithm:
Step 1: Input centre of circle i.e. xC ,yC and radius of circle i.e. r;
Step2: x=0; y=r;
Step 3: p0=3-(2*r);
Step 4: while(x<=y)
Step 5: {
if(p0<0)
{
p0=p0+(4*x)+6;
x=x+1;
Plot(xC+x,yC+y);
Plot(xC+y,yC+x);
Plot(xC+x,yC-y);
Plot(xC+y,yC-x);
Plot(xC-y,yC-x);
Plot(xC-x,yC-y);
Plot(xC-x,yC+y);
Plot(xC-y,yC+x);
}
ANITA
Step6: else
{
p0=p0+(4*(x-y))+10;
x=x+1;
y=y-1;
Plot(xC+x,yC+y);
Plot(xC+y,yC+x);
Plot(xC+x,yC-y);
Plot(xC+y,yC-x);
Plot(xC-y,yC-x);
Plot(xC-x,yC-y);
Plot(xC-x,yC+y);
Plot(xC-y,yC+x);
}
}
Step 7: Exit
ANITA
Practical No. 3
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
double xc,yc,r,p0,x,y;
cin>>xc>>yc;
cin>>r;
x=0; y=r;
p0=3-(2*r);
while(x<=y) {
if(p0<0)
{ p0=p0+(4*x)+6;
x++;
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+y,yc+x,WHITE);
ANITA
putpixel(xc+x,yc-y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-y,yc+x,WHITE);
else
{ p0=p0+(4*(x-y))+10;
x++; y--;
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-y,yc+x,WHITE);
getch();
ANITA
OUTPUT 03
ANITA
Practical No. 4
Aim : To draw circle using Mid point Circle Algorithm
Algorithm:
Step 1: Input centre of circle i.e. xc ,yc and radius of circle i.e. r;
Step2: x=0; y=r;
Step 3: p0=(5/4)-r;
Step 4: while(x<=y)
Step 5: {
If(p0<0)
{
p0=p0+(2*x)+3;
x=x+1;
Plot(xc+x,yc+y);
Plot(xc+y,yc+x);
Plot(xc+x,yc-y);
Plot(xc+y,yc-x);
Plot(xc-y,yc-x);
Plot(xc-x,yc-y);
Plot(xc-x,yc+y);
Plot(xc-y,yc+x);
}
ANITA
Step6: else
{
p0=p0+(2*(x-y))+5;
x=x+1;
y=y-1;
Plot (xc+x,yc+y);
Plot (xc+y,yc+x);
Plot (xc+x,yc-y);
Plot (xc+y,yc-x);
Plot (xc-y,yc-x);
Plot (xc-x,yc-y);
Plot (xc-x,yc+y);
Plot ( xc-y , yc+x);
}
}
Step 7: Exit
ANITA
Practical No. 4
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
double xc,yc,r,p0,x,y;
cin>>xc>>yc;
cin>>r;
x=0; y=r;
p0=(5/4)-r;
while(x<=y)
{ if(p0<0)
{ p0=p0+(2*x)+3;
x++;
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+y,yc+x,WHITE);
ANITA
putpixel(xc+x,yc-y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-y,yc+x,WHITE);
else
p0=p0+(2*(x-y))+5;
x++; y--;
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-y,yc+x,WHITE);
getch();
}
ANITA
OUTPUT 04
ANITA
PRACTICAL NO. 5
AIM : To translate a polygon along x and y-axis.
ALGORITHM:
Step 1:Input no. of vertices ‘n’ of polygon.
Step 2:Input the co-ordinates (x , y) of all the vertices and store in an
array poly[], which contains 2*n elements. i.e.
int j=0;
for(int i=1;i<=n;i++)
{
Enter poly[j]; //Input x-co-ordinate of ith vertex
Enter poly[j++]; //Input y co-ordinate of ith vertex
}
Step 3:To close the polygon, the last vertex should be equal to the first
vertex.
i.e. poly[n]=poly[0]; and poly[n+1]=poly[1];
and then call drawpoly(n+1,poly);
Step 4: Enter translating value tx along x-axis and ty along y-axis.
For inverse translation enter negative values for tx and ty;
Step 5: For each original vertex, the corresponding new vertex is
x1=x+tx; and y1=y+ty;
where x1 and y1 are the new co-ordinates.
ANITA
i.e. j=0;
for(i=1;i<=n;i++)
{
poly[j++]+=tx;
poly[j++]+=ty;
}
poly[j++]=poly[0];
poly[j]=poly[1];
drawpoly(n+1,poly);
Step 6:Exit;
ANITA
PRATICAL NO. 5
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int n,poly[20];
cin>>n;
int j=0;
for(int i=1;i<=n;i++)
cout<<"Before translaion:\n";
ANITA
drawpoly(n+1,poly);
int tx,ty;
cin>>tx;
cin>>ty;
j=0;
for(i=1;i<=n;i++)
poly[j++]+=tx;
poly[j++]+=ty;
poly[j++]=poly[0];
poly[j]=poly[1];
cout<<"After translation:";
drawpoly(n+1,poly);
getch();
ANITA
OUTPUT05
ANITA
PRACTICAL NO.6
AIM : To apply scaling on a polygon along x and y-axis.
ALGORITHM:
Step 1:Input no. of vertices ‘n’ of polygon.
Step 2:Input the co-ordinates (x , y) of all the vertices and store in an
array poly[], which contains 2*n elements. i.e.
int j=0;
for(int i=1;i<=n;i++)
{
ANITA
Enter poly[j]; //Input x-co-ordinate of ith vertex
Enter poly[j++]; //Input y co-ordinate of ith vertex
}
Step 3:To close the polygon, the last vertex should be equal to the first
vertex.
i.e. poly[n]=poly[0]; and poly[n+1]=poly[1];
and then call drawpoly(n+1,poly);
Step 4: Enter scaling factor sx along x-axis and sy along y-axis.
Step 5:Case 1:Scaling:
For each original vertex, the corresponding new vertex is
x1=x*sx; and y1=y*sy;
where x1 and y1 are the new co-ordinates.
i.e. j=0;
for(i=1;i<=n;i++)
{
poly[j++]*=sx;
poly[j++]*=sy;
}
poly[j++]=poly[0];
poly[j]=poly[1];
drawpoly(n+1,poly);
ANITA
Step 6:Case 2: Inverse Scaling:
Co-ordinates of new vertices are:-
x1=x/sx; y1=y/sy;
i.e. j=0;
for(i=1;i<=n;i++)
{ poly[j++]=poly[j++]/sx;
poly[j++]=poly[j++]/sy; }
poly[j++]=poly[0];
poly[j]=poly[1];
drawpoly(n+1,poly);
Step 7:Exit;
PRACTICAL NO. 6
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
int ch;
ANITA
{
int sx,sy;
cin>>sx;
cin>>sy;
int j=0;
for(int i=1;i<=n;i++)
poly[j++]/=sx;
poly[j++]/=sy;
poly[j++]=poly[0];
poly[j]=poly[1];
drawpoly(n+1,poly);
int sx,sy;
cin>>sx;
ANITA
cout<<"Enter scaling factor along y-axis:\n";
cin>>sy;
int j=0;
for(int i=1;i<=n;i++)
poly[j++]*=sx;
poly[j++]*=sy;
poly[j++]=poly[0];
poly[j]=poly[1];
cout<<"\nAfter scaling:\n";
drawpoly(n+1,poly);
void polygon()
int n,poly[20];
cin>>n;
int j=0;
for(int i=1;i<=n;i++)
ANITA
cin>>poly[j++]; //to enter x-co-ordinates
cout<<"Before scaling:\n";
drawpoly(n+1,poly);
if(ch==1)
scaling(n,poly);
else
inscaling(n,poly);
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"1.>Scaling\n";
cout<<"2.>Inverse scaling\n";
cout<<"3.>Exit\n";
while(1)
ANITA
cin>>ch;
switch(ch)
case 1:
case 2: polygon();
break;
case 3: exit(0);
OUTPUT 06
Scaling:-
ANITA
Inverse Scaling:-
ANITA
Practical No.-07
AIM : To rotate a polygon about origin.
ANITA
ALGORITHM:
Step 1: Input n vertices of a polygon and draw it.
Step 2: Input the angle of rotation Q of the polygon in radians.
Step 3: After rotating vertex (x,y) by Q radians the new co-ordinates of
this vertex are: x1=x*CosQ – y*SinQ
y1=x*SinQ + y*CosQ
Step 4: We apply rotation on all the vertices as follows:
j=0;
for(i=1;i<=n;i++)
{
poly1[j++]=((poly[k]*cos(Q))-(poly[k+1]*sin(Q)));
poly1[j++]=((poly[k]*sin(Q))+(poly[k+1]*cos(Q)));
k=k+2;
}
poly1[j++]=poly1[0];
poly1[j]=poly1[1];
Step 4: Apply the rotation on each and every vertex of the polygon and
draw the new polygon after rotation.
Step 5:Exit;
Practical No. 7
PROGRAM:
ANITA
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int n,poly[20],poly1[20],k=0;
double Q,x;
cin>>n;
int j=0;
for(int i=1;i<=n;i++)
cin>>poly[j++];
cin>>poly[j++];
poly[j++]=poly[0];
poly[j]=poly[1];
drawpoly(n+1,poly);
ANITA
cout<<"Enter angle of rotation in degrees:\n";
cin>>Q;
Q=(3.14*Q)/180;
j=0;
for(i=1;i<=n;i++)
poly1[j++]=(poly[k]*cos(Q))-(poly[k+1]*sin(Q));
poly1[j++]=(poly[k]*sin(Q))+(poly[k+1]*cos(Q));
k=k+2; }
poly[j++]=poly[0];
poly[j]=poly[1];
drawpoly(n+1,poly);
getch();
OUTPUT 07
ANITA
Practical No.08
ANITA
AIM:To rotate a polygon about a fixed point.
Step 1: Input vertices (x,y) of a polygon and draw it.
Step 2: Input fixed point P(tx,ty) about which rotation is to be performed.
Step 3: Input angle of rotation Q.
Step 4: Then vertices(x1,y1) of the rotated polygon are calculated as:
x1 1 0 tx CosQ -SinQ 0 1 0 -tx x
y1 = 0 1 ty SinQ CosQ 0 0 1 -ty y
1 0 0 1 0 1 1 0 0 1 1
Step 5:We calculate the vertices of new polygon as shown above and
draw it.
Step 6: Exit.
Practical No.08
ANITA
AIM:To rotate a polygon about a fixed point.
PROGRAM:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
int gd=DETECT,gm,n,poly[20];
initgraph(&gd,&gm," ");
cin>>n;
int tx,ty,j=0;
double Q;
for(int i=0;i<n;i++)
cin>>poly[j++];
cin>>poly[j++];
poly[j++]=poly[0];
poly[j]=poly[1];
drawpoly(n+1,poly);
ANITA
cout<<"Enter a fixed point of rotation:\n";
cin>>tx>>ty;
cin>>Q;
Q=(3.14*Q)/180;
double a[3][3],b[3][3],c[3][3],d[3][3],f[3][10];
//translation matrix
//rotation matrix
//polygon matrix
int k=0;
for(i=0;i<n;i++)
for(j=0;j<2;j++)
f[j][i]=poly[k++];
if(j==2)
ANITA
f[j][i]=1;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
d[i][j]=0;
for(k=0;k<3;k++)
d[i][j]+=a[i][k]*b[k][j];
double e[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
e[i][j]=0;
for(k=0;k<3;k++)
e[i][j]+=d[i][k]*c[k][j];
double r[3][10];
for(i=0;i<3;i++)
for(j=0;j<n;j++)
r[i][j]=0;
ANITA
for(k=0;k<3;k++)
r[i][j]+=e[i][k]*f[k][j]; }
k=0;
for(i=0;i<n;i++)
for(j=0;j<2;j++)
poly[k++]=r[j][i];
poly[k++]=poly[0];
poly[k]=poly[1];
drawpoly(n+1,poly);
getch();
OUTPUT 08
ANITA
Practical No.09
ANITA
AIM:To apply scaling on a polygon about a fixed point.
ALGORITHM:
Step 1: Input vertices (x,y) of a polygon and draw it.
Step 2: Input fixed point P(tx,ty) about which scaling is to be performed.
Step 3: Input scaling factor along x-axis i.e. Sx and along y-axis i.e. Sy.
Step 4: Then vertices(x1,y1) of the scaled polygon are calculated as:
x1 1 0 tx Sx 0 0 1 0 -tx x
y1 = 0 1 ty 0 Sy 0 0 1 -ty y
1 0 0 1 0 0 1 0 0 1 1
Step 5:We calculate the vertices of new polygon as shown above and
draw it.
Step 6: Exit.
ANITA
Practical No.09
PROGRAM:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
int gd=DETECT,gm,n,poly[20];
initgraph(&gd,&gm," ");
cin>>n;
int tx,ty,j=0;
double sx,sy;
for(int i=0;i<n;i++)
cin>>poly[j++];
cin>>poly[j++];
poly[j++]=poly[0];
ANITA
poly[j]=poly[1];
drawpoly(n+1,poly);
cin>>tx>>ty;
cin>>sx;
cin>>sy;
double a[3][3],b[3][3],c[3][3],d[3][3],f[3][10];
//translation matrix
//scaling matrix
//polygon matrix
int k=0;
for(i=0;i<n;i++)
{
ANITA
for(j=0;j<2;j++)
f[j][i]=poly[k++];
if(j==2)
f[j][i]=1;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
d[i][j]=0;
for(k=0;k<3;k++)
d[i][j]+=a[i][k]*b[k][j];
double e[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
e[i][j]=0;
for(k=0;k<3;k++)
e[i][j]+=d[i][k]*c[k][j];
double r[3][10];
for(i=0;i<3;i++)
ANITA
for(j=0;j<n;j++)
r[i][j]=0;
for(k=0;k<3;k++)
r[i][j]+=e[i][k]*f[k][j];
k=0;
for(i=0;i<n;i++)
for(j=0;j<2;j++)
poly[k++]=r[j][i];
poly[k++]=poly[0];
poly[k]=poly[1];
drawpoly(n+1,poly);
getch();
ANITA
OUTPUT09
ANITA
Practical No. 10
Aim :To fill a polygon using Boundary fill Algorithm.
Algorithm:
Step 1: Draw a polygon with boundary color i.e. bcolor
e.g. rectangle(l,t,r,b);
Step 2: Input a color i.e fcolor ,other than bcolor ,which you want to fill
in the polygon.
Step 3: Select a point inside the region of the polygon.i.e. (x,y).
Step 4: Fill the polygon with fill color by recursion using 4-connected or
8-connected method.
ANITA
i.e. void boundaryfil( int x, int y, int bcolor, int fcolor)
{
int current=getpixel(x,y);
if((current!=fcolor)&&(current!=bcolor))
{ putpixel(x,y,fcolor);
boundaryfil(x,y-1,bcolor,fcolor);
boundaryfil(x-1,y,bcolor,fcolor);
boundaryfil(x,y+1,bcolor,fcolor);
boundaryfil(x+1,y,bcolor,fcolor);
}
Step 5: Exit.
Practical No.10
Program:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
delay(2);
int current=getpixel(x,y);
ANITA
if((current!=fcolor)&&(current!=bcolor))
putpixel(x,y,fcolor);
boundaryfil(x,y-1,bcolor,fcolor);
boundaryfil(x-1,y,bcolor,fcolor);
boundaryfil(x,y+1,bcolor,fcolor);
boundaryfil(x+1,y,bcolor,fcolor);
void main()
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int bcolor,fcolor;
cin>>bcolor;
cin>>fcolor;
setcolor(bcolor);
rectangle(300,100,360,150);
int x=301,y=101;
boundaryfil(x,y,bcolor,fcolor);
ANITA
getch();
OUTPUT 10
ANITA
ANITA