0% found this document useful (0 votes)
578 views

Practical No. 1: Anita

dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,vdda line drawing ,bresen

Uploaded by

anitayadavcs
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
578 views

Practical No. 1: Anita

dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,dda line drawing ,bresenham's line drawing,bresenham's circle drawing ,mid-point circle drawing,flood fill,boundary fill,computer graphics programs and algorithms,line drawing and circle drawing algorithms and programs, reflection,scaling, rotation programs in c++,vdda line drawing ,bresen

Uploaded by

anitayadavcs
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 53

Practical No.

Aim:- To draw line using DDA.


Algorithm:
Step 1: Input (x1,y1) and (x2,y2).
Step 2: dx=x2-x1; dy=y2-y1;
Step 3: if(dx>dy)
then max=dx;
else max=dy;
Step 4: xinc=dx/max; yinc=dy/max;
Step 5: putpixel(x1,y1,WHITE)
Step 6: while(i<=max)
{
x1=x1+xinc;
y1=y1+yinc;
putpixel(x1,y1,WHITE)
}
Step 7: Exit.

ANITA
Practical No. 1

AIM: To draw line using DDA

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," ");

printf("\nEnter starting co-ordinates:\n");

scanf("%f%f",&x1,&y1);

printf("\nEnter ending co-ordinates:\n");

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

AIM: To draw line using Bresenham’s line drawing Algorithm

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;

cout<<"Enter starting co-ordinates:\n";

cin>>x1>>y1;

cout<<"Enter ending co-ordinates:\n";

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;

while(x0<=x2 && y0<=y2)

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;

while(x0<=x2 && y0<=y2)

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

AIM: To draw circle using Bresenham’s Algorithm

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;

cout<<"Enter center of circle:\n";

cin>>xc>>yc;

cout<<"\nEnter radius of circle:\n";

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

AIM: To draw circle using mid-point circle algorithm

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;

cout<<"Enter center of circle:\n";

cin>>xc>>yc;

cout<<"\nEnter radius of circle:\n";

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

AIM: To translate a polygon along x and y-axis.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm," ");

int n,poly[20];

cout<<"Enter number of vertices:\n";

cin>>n;

int j=0;

for(int i=1;i<=n;i++)

cout<<"Enter vertex "<<i<<"\n";

cin>>poly[j++]; //to enter x-co-ordinates

cin>>poly[j++]; //to enter y co-ordinates

poly[j++]=poly[0]; //To close the polygon

poly[j]=poly[1]; // (n+1)th vertex=0th vertex

cout<<"Before translaion:\n";
ANITA
drawpoly(n+1,poly);

int tx,ty;

cout<<"Enter translating factor along x-axis:\n";

cout<<"NOTE:For inverse translation enter negative values of tx and ty\n";

cin>>tx;

cout<<"Enter translating factor along y-axis:\n";

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

AIM: To apply scaling on a polygon along x and y-axis.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

#include<process.h>

int ch;

void inscaling(int n,int poly[])

ANITA
{

int sx,sy;

cout<<"Enter scaling factor along x-axis:\n";

cin>>sx;

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 inverse scaling:\n";

drawpoly(n+1,poly);

void scaling(int n,int poly[])

int sx,sy;

cout<<"Enter scaling factor along x-axis:\n";

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];

cout<<"Enter number of vertices:\n";

cin>>n;

int j=0;

for(int i=1;i<=n;i++)

cout<<"Enter vertex "<<i<<"\n";

ANITA
cin>>poly[j++]; //to enter x-co-ordinates

cin>>poly[j++]; //to enter y co-ordinates

poly[j++]=poly[0]; //To close the polygon

poly[j]=poly[1]; // (n+1)th vertex=0th vertex

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)

cout<<"\nEnter your choice:\n";

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

AIM :To rotate a polygon about origin.

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;

cout<<"Enter no. of vertices of a polygon:\n";

cin>>n;

int j=0;

cout<<"Enter "<<n<<" vertices:\n";

for(int i=1;i<=n;i++)

cin>>poly[j++];

cin>>poly[j++];

poly[j++]=poly[0];

poly[j]=poly[1];

cout<<"\nPolygon before rotation is:\n";

drawpoly(n+1,poly);
ANITA
cout<<"Enter angle of rotation in degrees:\n";

cout<<"NOTE:Enter negative values of angle for clockwise rotation.\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];

cout<<"After rotation :\n";

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," ");

cout<<"Enter no. of co-ordinates of the polygon:\n";

cin>>n;

int tx,ty,j=0;

double Q;

cout<<"Enter "<<n<<" co-ordinates(x,y) of polygon:\n";

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;

cout<<"Enter angle of rotation in degrees:\n";

cout<<"NOTE:Enter negative values of angle for clockwise rotation.\n";

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

a[0][0]=1; a[0][1]=0; a[0][2]=tx; a[1][0]=0;

a[1][1]=1; a[1][2]=ty; a[2][0]=0; a[2][1]=0; a[2][2]=1;

//rotation matrix

b[0][0]=cos(Q); b[0][1]=(-sin(Q)); b[0][2]=0; b[1][0]=sin(Q);

b[1][1]=cos(Q); b[1][2]=0; b[2][0]=0; b[2][1]=0; b[2][2]=1;

//inverse translation matrix

c[0][0]=1; c[0][1]=0; c[0][2]=(-tx); c[1][0]=0; c[1][1]=1;

c[1][2]=(-ty); c[2][0]=0; c[2][1]=0; c[2][2]=1;

//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;

//multiply arrays a and b

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]; }

//polygon After rotation

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];

cout<<"Polygon after rotation:\n";

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:For inverse scaling:


x1 1 0 tx 1/Sx 0 0 1 0 -tx x
y1 = 0 1 ty 0 1/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

AIM:To apply scaling on a polygon about a fixed point.

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," ");

cout<<"Enter no. of co-ordinates of the polygon:\n";

cin>>n;

int tx,ty,j=0;

double sx,sy;

cout<<"Enter "<<n<<" co-ordinates(x,y) of polygon:\n";

for(int i=0;i<n;i++)

cin>>poly[j++];

cin>>poly[j++];

poly[j++]=poly[0];
ANITA
poly[j]=poly[1];

cout<<"Polygon before scaling:\n";

drawpoly(n+1,poly);

cout<<"Enter a fixed point for scaling:\n";

cin>>tx>>ty;

cout<<"Enter scaling factor along x-axis:\n";

cin>>sx;

cout<<"Enter scaling factor along y-axis:\n";

cin>>sy;

double a[3][3],b[3][3],c[3][3],d[3][3],f[3][10];

//translation matrix

a[0][0]=1; a[0][1]=0; a[0][2]=tx; a[1][0]=0; a[1][1]=1;

a[1][2]=ty; a[2][0]=0; a[2][1]=0; a[2][2]=1;

//scaling matrix

b[0][0]=1/sx; b[0][1]=0; b[0][2]=0; b[1][0]=0;

b[1][1]=1/sy; b[1][2]=0; b[2][0]=0; b[2][1]=0; b[2][2]=1;

//inverse translation matrix

c[0][0]=1; c[0][1]=0; c[0][2]=(-tx); c[1][0]=0; c[1][1]=1;

c[1][2]=(-ty); c[2][0]=0; c[2][1]=0; c[2][2]=1;

//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;

//multiply arrays a and b

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];

//polygon After rotation

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];

cout<<"Polygon after scaling:\n";

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

Aim :To fill a polygon using Boundary fill Algorithm.

Program:

#include<iostream.h>

#include<graphics.h>

#include<conio.h>

#include<dos.h>

void boundaryfil(int x,int y,int bcolor,int fcolor)

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;

cout<<"Enter the color of boundary(0 to 255):\n";

cin>>bcolor;

cout<<"Enter the color to be filled(0 to 255):\n";

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

You might also like