CG Lab Manual
CG Lab Manual
Lab Manual
For
Computer Graphics
Semester III
(2024-25)
SARASW ATI EDUCATION SOCIETY
List of Experiment
Experiment No 1
Algorithm
Calculate dy = y2-y1
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
Program:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
clrscr();
initgraph(&gd,&gm," ");
printf("Enter the first coordinates.");
printf("\nx1: ");
scanf("%f", &x1);
printf("y1: ");
scanf("%f", &y1);
printf("\nEnter the second coordinates.");
printf("\nx2: ");
scanf("%f", &x2);
printf("y2: ");
scanf("%f", &y2);
dx = abs(x2-x1); dy
= abs(y2-y1);
printf("\nThe value of DeltaX: %f.", dx);
printf("\nThe value of DeltaY: %f.", dy);
(x2-x1)/len;
iy = (y2-y1)/len;
if((y2-y1) < 0)
{
y = y1 + (0.5*(-1));
}
else if((y2-y1) > 0)
{
y = y1 + 0.5;
}
else if((y2-y1) == 0)
{
y = y1;
}
printf("\ni\tPlot\tX\tY\n");
s1 = floor(x); s2 =
floor(y);
po:
switch(ch)
{
SARASW ATI EDUCATION SOCIETY
case 1: clrscr();
for(i=0;i<len;i++)
{
putpixel(s1,s2,4); x =
x+ix;
y = y+iy;
s1 = floor(x); s2 =
floor(y);
//printf("%d\t(%f,%f)\t%f\t%f",i,s1,s2,x,y);
}
break; case
2: clrscr();
for(i=0;i<len;i++)
{
if(i%2==0)
{
putpixel(s1,s2,4);
}
x = x+ix; y =
y+iy;
s1 = floor(x); s2 =
floor(y);
//printf("%d\t(%f,%f)\t%f\t%f",i,s1,s2,x,y);
}
break; case
3: clrscr();
for(i=0;i<len;i++)
{
if(i%4!=0)
{
putpixel(s1,s2,4);
}
x = x+ix; y =
y+iy;
s1 = floor(x); s2 =
floor(y);
//printf("%d\t(%f,%f)\t%f\t%f",i,s1,s2,x,y);
}
break; default:
printf("Enter a proper value from 1 to 3."); goto
po;
}
getch();
closegraph(); return
0;
}
SARASW ATI EDUCATION SOCIETY
Output :
SARASW ATI EDUCATION SOCIETY
Experiment No 2
If d ≥ 0
Then d = d + i2
Increment y = y + 1
Step9: Increment x = x + 1
Step11: Go to step 7
Program DOTTED
Line
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,x,y,x1,y1,x2,y2,dx,dy,i,e;
float xinc,yinc;
initgraph(&gd,&gm,"");
cleardevice();
printf("Enter x1,y1,x2,y2:\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(x1<x2)
xinc=1;
else
xinc=-1;
if(y1<y2)
yinc=1;
else
yinc=-1;
x=x1;
y=y1;
if(dx>=dy)
{
e=(2*dy)-dx;
while(x!=x2)
{
if(e<0)
SARASW ATI EDUCATION SOCIETY
e=e+(2*dy);
else
{
e=e+(2*(dy-dx));
y=y+yinc;
y=y+yinc;
}
x=x+xinc;
x=x+xinc;
putpixel(x,y,WHITE);
}
}
else
{
e=(2*dx)-dy;
while(y!=y2)
{
if(e<0)
e=e+(2*dx);
else
{
e=e+(2*(dx-dy));
x=x+xinc;
x=x+xinc;
}
y=y+yinc;
y=y+yinc;
putpixel(x,y,WHITE);
}
}
getch();
closegraph();
restorecrtmode();
}
SARASW ATI EDUCATION SOCIETY
Thick Line
include<stdio.h>
#include<graphics.h>
#include<math.h>
void bline(float x1,float y1,float x2,float y2);
void main()
{
int gd=DETECT,gm;
float wy,wx,x1,y1,x2,y2;
int i,thickness;
initgraph(&gd,&gm,"");
printf("Enter x1,y1,x2,y2:\n");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("\nEnter thickness of line: ");
scanf("%d",&thickness);
bline(x1,y1,x2,y2);
if((y2-y1)/(x2-x1)<1)
{
SARASW ATI EDUCATION SOCIETY
wy=(thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(x2-
x1));
for(i=0;i<wy;i++)
{
bline(x1,y1-i,x2,y2-i);
bline(x1,y1+i,x2,y2+i);
}
}
else
{
wx=(thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(y2-
y1));
for(i=0;i<wx;i++)
{
bline(x1-i,y1,x2-i,y2);
bline(x1+i,y1,x2+i,y2);
}
}
getch();
closegraph();
restorecrtmode();
}
void bline(float x1,float y1,float x2,float y2)
{
float xinc,yinc,x,y;
float dx,dy,e;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(x1<x2)
xinc=1;
else
xinc=-1;
if(y1<y2)
yinc=1;
else
yinc=-1;
x=x1;
y=y1;
putpixel(x,y,WHITE);
if(dx>=dy)
{
e=(2*dy)-dx;
while(x!=x2)
{
if(e<0)
{
e+=(2*dy);
SARASW ATI EDUCATION SOCIETY
}
else
{
e+=(2*(dy-dx));
y+=yinc;
}
x+=xinc;
putpixel(x,y,WHITE);
}
}
else
{
e=(2*dx)-dy;
while(y!=y2)
{
if(e<0)
{
e+=(2*dx);
}
else
{
e+=(2*(dx-dy));
x+=xinc;
}
y+=yinc;
putpixel(x,y,WHITE);
}
}
}
SARASW ATI EDUCATION SOCIETY
Dashed Line
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,x,y,x1,y1,x2,y2,dx,dy,i,e,k,j;
int d=1,a=2,n=1;
float xinc,yinc;
initgraph(&gd,&gm,"");
cleardevice();
printf("Enter x1,y1,x2,y2:\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(x1<x2)
xinc=1;
else
xinc=-1;
if(y1<y2)
SARASW ATI EDUCATION SOCIETY
yinc=1;
else
yinc=-1;
x=x1;
y=y1;
if(dx>=dy)
{
e=(2*dy)-dx;
k=0;
while(x1!=x2)
{
if(d==1)
a=1;
if(k%10==0 && d==1)
a=5;
if(e<0)
e=e+2*dy;
else
{
e=e+2*(dy-dx);
y=y+yinc*a;
}
x=x+xinc*a;
k++;
for(j=0;j<n;j++)
{
putpixel((int)(x+j),(int)(y-j),WHITE);
delay(100);
}
}
}
else
{
k=0;
e=(2*dx)-dy;
while(y1!=y2)
{
if(d==1)
a=1;
if(k%10==0 && d==1)
a=4;
if(e<0)
e=e+2*dx;
else
{
e=e+2*(dx-dy);
x=x+xinc*a;
SARASW ATI EDUCATION SOCIETY
}
y=y+yinc*a;
k++;
for(j=0;j<n;j++)
{
putpixel((int)(x-j),(int)(y+j),WHITE);
delay(100);
}
}
}
getch();
closegraph();
restorecrtmode();
}
Experiment NO 3
We use the mid-point algorithm to calculate all the perimeter points of the circle in the first
octant and then print them along with their mirror points in the other octants. This will work
because a circle is symmetric about its centre.
SARASW ATI EDUCATION SOCIETY
Program
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
int x,y,x_mid,y_mid,radius,dp;
int g_mode,g_driver=DETECT;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
scanf("%d %d",&x_mid,&y_mid);
scanf("%d",&radius);
x=0;
y=radius;
dp=1-radius;
do
putpixel(x_mid+x,y_mid+y,YELLOW);
putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);
putpixel(x_mid-x,y_mid+y,YELLOW);
putpixel(x_mid-x,y_mid-y,YELLOW);
SARASW ATI EDUCATION SOCIETY
putpixel(x_mid-y,y_mid-x,YELLOW);
putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);
if(dp<0) {
dp+=(2*x)+1;
else{
y=y-1;
dp+=(2*x)-(2*y)+1;
x=x+1;
}while(y>x);
getch();
Experiment No 4
This is an area filling algorithm. This is used where we have to do an interactive painting in
computer graphics, where interior points are easily selected. If we have a specified boundary in a
single color, then the fill algorithm proceeds pixel by pixel until the boundary color is
encountered. This method is called the boundary-fill algorithm.
1. 4-connected:
In this firstly there is a selection of the interior pixel which is inside the boundary then in reference
to that pixel, the adjacent pixel will be filled up that is top-bottom and left-right.
2. 8-connected:
This is the best way of filling the color correctly in the interior of the area defined. This is used
to fill in more complex figures. In this four diagonal pixel are also included with a reference
interior pixel (including top-bottom and left right pixels).
4) End.
SARASW ATI EDUCATION SOCIETY
Program
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
Putpixel(x,y,f_color);
Boundaryfill(x+1,y,f_color,b_color);
Boundaryfill(x,y+1,f_color,b_color);
Boundaryfill(x-1,y,f_color,b_color);
Boundaryfill(x,y-1,f_color,b_color);
}}
int main()
int gm,gd,=DETECT,radius;
int x,y;
scanf(“%d%d”,&x,&y);
scanf(“%d”,&radius);
initgraph(&gd,&gm,”c:\\turboc3\\bgi”);
circle(x,y,radius);
Boundaryfill(x,y,4,15);
SARASW ATI EDUCATION SOCIETY
Delay(10000);
Closegraph();
return 0;
Output :
Flood-fill Algorithm
By this algorithm, we can recolor an area that is not defined within a single color boundary. In
this, we can paint such areas by replacing a color instead of searching for a boundary color value.
This whole approach is termed as flood fill algorithm. This procedure can also be used to reduce
the storage requirement of the stack by filling pixel spans.
begin
if getpixel (x, y) = old color then
begin
setpixel (x ,y, fillcolor)
floodfill4 (x+1, y, fillcolor, oldcolor)
floodfill4 (x-1, y, fillcolor, oldcolor)
floodfill4 (x, y+1, fillcolor, oldcolor)
floodfill4 (x, y-1, fillcolor, oldcolor)
end.
SARASW ATI EDUCATION SOCIETY
Program
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
int main()
{
int gm,gd=DETECT,radius; int
x,y;
printf("Enter x and y positions for circle\n");
scanf("%d%d",&x,&y);
printf("Enter radius of circle\n");
scanf("%d",&radius);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
circle(x,y,radius);
floodFill(x,y,0,15);
delay(5000);
closegraph(); return 0;
}
Output :
Experiment No 5
Theory :
Scan line filling is basically filling up of polygons using horizontal lines or scan lines. The
purpose of the SLPF algorithm is to fill (color) the interior pixels of a polygon given only the
vertices of the figure. To understand Scan line, think of the image being drawn by a single pen
starting from bottom left, continuing to the right, plotting only points where there is a point
present in the image, and when the line is complete, start from the next line and continue.
This algorithm works by intersecting scanline with polygon edges and fills the polygon
between pairs of intersections.
SARASW ATI EDUCATION SOCIETY
Program
#include<stdio.h> #include<conio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
void main()
x,y,temp;
slope[20]; clrscr();
scanf(“%d”,&n);
for(i=0;i<n;i++)
printf(“\tX%dY%d:”,i,i);
scanf(“%d%d”,&a[i][0],&a[i][1]);
} a[n][0]=a[0][0];
a[n][1]=a[0][1];
SARASW ATI EDUCATION SOCIETY
detectgraph(&gd,&gm);
initgraph(&gd,&gm,”c:\\tc\\bgi”);
for(i=0;i<n;i++)
{ line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
getch(); for(i=0;i<n;i++)
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];
if(dy==0) slope[i]=1.0;
if(dx==0) slope[i]=0.0;
if((dy!=0)&&(dx!=0))
slope[i]=(float)dx/dy;
for(y=0;y<480;y++)
{
SARASW ATI EDUCATION SOCIETY
k=0;for(i=0;i<n;i++)
if((a[i][1]<=y)&&(a[i+1][1]>y)){||((a[i][1]>y)&&(a[i+1][1]<=y))xi[k]=(int)(a[i][0]+slope[i]* (y-
a[i]
[1]));
k++;
for(j=0;j<k-1;j++) for(i=0;i<k;i++)
if(xi[i]>xi[i+1])
temp=xi[i]; xi[i]=xi[i+1];
xi[i+1]=temp;
setcolor(35);
for(i=0;i<k;i+=2)
line(xi[i],y,xi[i+1]+1,y);
getch();
SARASW ATI EDUCATION SOCIETY
Output :
Conclusion : Thus we have studied about Scan line Polygon filling algorithm.
SARASW ATI EDUCATION SOCIETY
Experiment No 6
Translation
It is the straight line movement of an object from one position to another is called Translation. Here
the object is positioned from one coordinate location to another.
Scaling
It is used to alter or change the size of objects. The change is done using scaling factors. There are
two scaling factors, i.e. Sx in x direction Sy in y-direction. If the original position is x and
y. Scaling factors are Sx and Sy then the value of coordinates after scaling will be x1 and y1.
SARASW ATI EDUCATION SOCIETY
Rotation:
It is a process of changing the angle of the object. Rotation can be clockwise or anticlockwise. For
rotation, we have to specify the angle of rotation and rotation point. Rotation point is also called a
pivot point. It is print about which object is rotated.
SARASW ATI EDUCATION SOCIETY
Reflection:
It is a transformation which produces a mirror image of an object. The mirror image can be either
about x-axis or y-axis. The object is rotated by180°.
SARASW ATI EDUCATION SOCIETY
Shearing:
It is transformation which changes the shape of object. The sliding of layers of object occur. The
shear can be in one direction or in two directions.
Shearing in the X-direction: In this horizontal shearing sliding of layers occur. The
homogeneous matrix for shearing in the x-direction is shown below:
SARASW ATI EDUCATION SOCIETY
Program :
#include<stdio.h> #include<conio.h>
#include<math.h>
#include<graphics.h>
int ch,x,y,az,i,w,ch1,ch2,xa,ya,ra,a[10],b[10],da,db;
float x1,y1,az1,w1,dx,dy,theta,x1s,y1s,sx,sy,a1[10],b1[10];
void main()
{
int gm ,gr; clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Enter the upper left corner of the rectangle:\n");
scanf("%d%d",&x,&y);
printf("Enter the lower right corner of the rectangle:\n");
scanf("%d%d",&az,&w);
rectangle(x,y,az,w);
da=az-x;
db=w-y;
a[0]=x;
b[0]=y;
a[1]=x+da; b[1]=y;
a[2]=x+da; b[2]=y+db;
a[3]=x;b[3]=y+db;
while(1)
{
printf("******2D Transformations*******\n");
printf("1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing\n6.Exit\nEnter your
choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI"); rectangle(x,y,az,w);
printf("*******Translation*******\n\n"); printf("Enter
the value of shift vector:\n"); scanf("%f%f",&dx,&dy);
x1=x+dx; y1=y+dy;
az1=az+dx;
w1=w+dy;
SARASW ATI EDUCATION SOCIETY
rectangle(x1,y1,az1,w1); break;
case 2:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Rotation*******\n\n");
printf("Enter the value of fixed point and angle of rotation:Enter the value of fixed point and angle
of rotation:\n");
scanf("%d%d%d",&xa,&ya,&ra);
theta=(float)(ra*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 3:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("********Scaling*******\n\n");
printf("Enter the value of scaling factor:\n");
scanf("%f%f",&sx,&sy);
x1=x*sx; y1=y*sy;
az1=az*sx;
w1=w*sy;
rectangle(x1,y1,az1,w1); break;
case 4:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Reflection*********\n");
printf("1.About x-axis\n2.About y-axis\n3.About both axis\nEnter your choice:\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
SARASW ATI EDUCATION SOCIETY
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
}
break;
case 5:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Shearing******\n\n");
printf("1.x-direction shear\n2.y-direction shear\nEnter your choice:\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
printf("Enter the value of shear:\n");
scanf("%f",&x1s);
x1=x+(y*x1s); y1=y;
az1=az+(w*x1s);
w1=w;
rectangle(x1,y1,az1,w1); break;
case 2:
printf("Enter the value of shear:\n");
scanf("%f",&y1s);
x1=x; y1=y+(x*y1s);
az1=az;
w1=w+(az*y1s);
rectangle(x1,y1,az1,w1); break;
}
break; case
6: exit(0);
}
}
getch();
}
SARASW ATI EDUCATION SOCIETY
Output :
*******Translation*******
Enter the value of shift vector: 150
150
******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
*******Rotation*******
Enter the value of fixed point and angle of rotation: 300
300
70
******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
********Scaling*******
Enter the value of scaling factor: 2
2
******2DTransformations*******
SARASW ATI EDUCATION SOCIETY
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
*******Reflection*********
1. About x-axis
2. About y-axis
3. About both axis Enter
your choice:1 Enter the
fixed point 150
150
Enter your choice: 2
Enter the fixed point
150
150
Enter your choice: 3
Enter the fixed point
150
150
******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit
Experiment No 7
Aim : Implement Line Clipping Algorithm : Cohen Sutherland.
Theory :
Clipping is a process of removing a portion of a line or an object that falls outside of the
specified region, Cohen Sutherland is a line clipping algorithm which is used to clip out the extra
portion of the line from view Plane.
Program :
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];
int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
scanf("%d %d",&W_xmin,&W_ymin);
scanf("%d %d",&W_xmax,&W_ymax);
SARASW ATI EDUCATION SOCIETY
scanf("%d %d",&x,&y);
scanf("%d %d",&x1,&y1);
cleardevice();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
line(0,0,600,0);
line(0,0,0,600);
if(y>W_ymax) {
rcode_begin[0]=1; // Top
flag=1 ;
if(y<W_ymin) {
rcode_begin[1]=1; // Bottom
flag=1;
if(x>W_xmax) {
rcode_begin[2]=1; // Right
flag=1;
if(x<W_xmin) {
rcode_begin[3]=1; //Left
flag=1;
}
SARASW ATI EDUCATION SOCIETY
if(y1>W_ymax){
rcode_end[0]=1; // Top
flag=1;
if(y1<W_ymin) {
rcode_end[1]=1; // Bottom
flag=1;
if(x1>W_xmax){
rcode_end[2]=1; // Right
flag=1;
if(x1<W_xmin){
rcode_end[3]=1; //Left
flag=1;
if(flag==0)
flag=1;
for(i=0;i<4;i++){
if(region_code[i]==1)
flag=0;
if(flag==0)
else{
slope=(float)(y1-y)/(x1-x);
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;
SARASW ATI EDUCATION SOCIETY
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;
// end points
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;
SARASW ATI EDUCATION SOCIETY
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;
delay(1000);
clearviewport();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(0,0,600,0);
line(0,0,0,600);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
Experiment No 8
Theory :
3-D Transformation is the process of manipulating the view of a three-D object with respect to
its original position by modifying its physical attributes through various methods of
transformation like Translation, Scaling, Rotation, Shear, etc.
Program :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h> void
trans();
//void axis();
void scale();
void rotate();
int maxx,maxy,midx,midy;
/*void axis()
// getch();
// cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}*/
void main()
int ch;
SARASW ATI EDUCATION SOCIETY
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
your choice");
scanf("%d",&ch); do
switch(ch)
case 1 : trans();
getch();
// closegraph();
break;
case 2 : scale();
getch();
// closegraph();
break;
case 3 : rotate();
getch();
// closegraph();
break;
SARASW ATI EDUCATION SOCIETY
case 4 : break;
scanf("%d",&ch);
} while(ch<4);
void trans()
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
//axis();
bar3d(midx+50,midy-100,midx+60,midy-90,10,1);
scanf("%d%d",&x,&y);
printf("After translation:");
bar3d(midx+x+50,midy-(y+100),midx+x+60,midy-(y+90),10,1);
void scale()
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
SARASW ATI EDUCATION SOCIETY
midx=maxx/2; midy=maxy/2;
//axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("before translation\n");
printf("After scaling\n");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
void rotate()
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
//axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
SARASW ATI EDUCATION SOCIETY
// axis();
// bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
//axis();
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
//axis();
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
Output:
Translation
Scaling
SARASW ATI EDUCATION SOCIETY
Rotation
Experiment No 9
Theory :
Program
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd= DETECT, gm,i,j,m;
char c[8][8],s[1]; // needed to delcare 's' as string inorder to use it with outtextxy()
initgraph(&gd,&gm,"..\\bgi");
printf("Enter a Character:");
scanf("%s",s); clearviewport();
outtextxy(1,1,s);
for(i=0;i<textwidth("S");i++) // any capital character can be given instead of S
{
for(j=0;j<textheight("S");j++)
{
c[i][j]=getpixel(i,j);
}
}
printf("Enter the size to enlarge the inputted character \n");
scanf("%d",&m);
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
setfillstyle(SOLID_FILL,c[i][j]);
bar(100+(i*m),100+(j*m),100+(i+1)*m,100+(j+1)*m);
}
}
getch(); closegraph();
}
Sample input :
Enter a Character: A
Enter the size to enlarge the inputted character: 30
Experiment No 10
Theory :
Algorithm:-
Step 1- Read coordinates of all the vertices of polygon
Step 2- Read coordinates of the clipping window
Step 3- Consider the left edge of window
Step 4- Compare the vertices of each edge of the polygon , individually with clipping plane.
Step 5-Save the resulting intersections and vertices in the new list of vertices according to four
possible relationships between the edge and the clipping boundary discussed earlier.
Step 6- Repeat the steps 4 and 5 for remaining edges of the clipping window. Each time the
resultant list of the vertices of is successively passed to process the next edge of the clipping
window.
Step 7- Stop
Program
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int gd,gm,n,*x,i,k=0;
//window coordinates int
wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,wy4=340;
int w[]={220,140,420,140,420,340,220,340,220,140};//array for drawing window
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //initializing graphics
printf("Window:-");
setcolor(RED); //red colored window
drawpoly(5,w); //window drawn
printf("Enter the no. of vertices of polygon: ");
scanf("%d",&n);
x = malloc(n*2+1);
printf("Enter the coordinates of points:\n");
k=0;
for(i=0;i<n*2;i+=2) //reading vertices of polygon
{
SARASW ATI EDUCATION SOCIETY
printf("(x%d,y%d): ",k,k);
scanf("%d,%d",&x[i],&x[i+1]);
k++;
}
x[n*2]=x[0]; //assigning the coordinates of first vertex to last additional vertex for
drawpoly method.
x[n*2+1]=x[1];
setcolor(WHITE);
drawpoly(n+1,x);
printf("\nPress a button to clip a polygon..");
getch();
setcolor(RED);
drawpoly(5,w);
setfillstyle(SOLID_FILL,BLACK);
floodfill(2,2,RED);
gotoxy(1,1); //bringing cursor at starting position
printf("\nThis is the clipped polygon..");
getch();
cleardevice();
closegraph();
return 0;
}
SARASW ATI EDUCATION SOCIETY
Output
After entering vertices' coordinates just press a button a polygon will be drawn with white color.
Now press a button to clip the polygon and you'll simply get the clipped polygon in the output.
SARASW ATI EDUCATION SOCIETY
So, this is the simplest program to clip a polygon. If you want to change the window
coordinates you can change them according to you.