Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views57 pages

CG Lab Manual

The document is a lab manual for Computer Graphics at Yadavrao Tasgaonkar Institute of Engineering & Technology for Semester III (2024-25). It outlines a series of experiments including the implementation of various line drawing algorithms, area filling algorithms, and transformations in computer graphics. Each experiment includes aims, theories, algorithms, and sample code for practical implementation.

Uploaded by

gf1166679
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views57 pages

CG Lab Manual

The document is a lab manual for Computer Graphics at Yadavrao Tasgaonkar Institute of Engineering & Technology for Semester III (2024-25). It outlines a series of experiments including the implementation of various line drawing algorithms, area filling algorithms, and transformations in computer graphics. Each experiment includes aims, theories, algorithms, and sample code for practical implementation.

Uploaded by

gf1166679
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Lab Manual
For
Computer Graphics
Semester III
(2024-25)
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

List of Experiment

1. Implement DDA line Drawing Algorithm (dotted/dashed/thick).


2. Implement Bresenham’s Line Algorithm( dotted/dashed/thick).
3. Implement Midpoint Circle Algorithm.
4. Implement Area Filling Algorithm: Boundary Fill, Flood Fill.
5. Implement Scan line Polygon Filling Algorithm.
6. Implement 2D Transformations: Translation, Scaling, Rotation, Reflection, Shear.
7. Implement Line Clipping Algorithm : Cohen Sutherland.
8. Program to perform 3D Transformation.
9. Character Generation: Bitmap method.
10. Implement Polygon Clipping algorithm.
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Experiment No 1

Aim : Implement DDA line Drawing Algorithm (dotted/dashed/thick)


Theory :
In computer graphics, a digital differential analyzer (DDA) is hardware or software used for
linear interpolation of variables over an interval between start and end point. DDAs are used
for rasterization of lines, triangles and polygons. In its simplest implementation, the DDA
algorithm interpolates values in interval by computing for each xi the equations xi = xi−1+1/m,
yi = yi−1 + m, where Δx = x2− x1 and Δy = y2 – y1 and m = Δy/Δx.

Algorithm

Step1: Start Algorithm

Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.

Step3: Enter value of x1,y1,x2,y2.

Step4: Calculate dx = x2-x1 Step5:

Calculate dy = y2-y1

Step6: If ABS (dx) > ABS (dy)


Then step = abs (dx) Else
Step7: xinc=dx/step
yinc=dy/step assign
x = x1 assign y =
y1

Step8: Set pixel (x, y)

Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2

Step11: End Algorithm


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Program:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>

float x1,x2,y1,y2,dx,dy,len,ix,iy,x,y,s1,s2; int


i,j,ch;

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

if(dy > dx)


{
len = dy;
}
else if(dx > dy)
{
len = dy;
}
else
{
len = dy;
}

printf("\nThe length is: %f", len); ix =

(x2-x1)/len;
iy = (y2-y1)/len;

printf("\nThe increment in X: %f.", ix);


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

printf("\nThe increment in Y: %f.", iy);


if((x2-x1) < 0)
{
x = x1 - 0.5;
}
if((x2-x1) > 0)
{
x = x1 + 0.5;
}
else if((x2-x1) == 0)
{
x = x1;
}

printf("\nThe value of init X: %f", x);

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("\nThe value of init Y: %f", y);

printf("\ni\tPlot\tX\tY\n");

printf("Press any key to continue to the graph...");


getch();
clrscr();

s1 = floor(x); s2 =
floor(y);

po:

printf("\nChoose from options below..."); printf("\n1


= Straight line");
printf("\n2 = Dotted Line");
printf("\n3 = Dashed Line");
printf("\n>>Enter you choice: ");
scanf("%d", &ch);

switch(ch)
{
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Output :
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Conclusion : Thus we have studied about DDA line Drawing Algorithm.


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Experiment No 2

Aim : Implement Bresenham’s Line algorithm( dotted/dashed/thick)


Theory :
The big advantage of this algorithm is that, it uses only integer calculations. Moving across the x
axis in unit intervals and at each step choose between two different y coordinates.
Algorithm:

Step1: Start Algorithm

Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy

Step3: Enter value of x1,y1,x2,y2


Where x1,y1are coordinates of starting point And
x2,y2 are coordinates of Ending point
Step4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy Calculate
i2=2*(dy-dx) Calculate
d=i1-dx
Step5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
If dx < 0
Then x = x2
If dx > 0
Then x = x1
If dx > 0
Then x = x1
Step6: Generate point at (x,y)coordinates.
Step7: Check if whole line is generated.
If x > = xend Stop.
Step8: Calculate co-ordinates of the next pixel.
If d < 0
Then d = d + i1
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

If d ≥ 0
Then d = d + i2
Increment y = y + 1

Step9: Increment x = x + 1

Step10: Draw a point of latest (x, y) coordinates

Step11: Go to step 7

Step12: End of Algorithm

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

}
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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

}
y=y+yinc*a;
k++;
for(j=0;j<n;j++)
{
putpixel((int)(x-j),(int)(y+j),WHITE);
delay(100);
}
}
}
getch();
closegraph();
restorecrtmode();
}

Conclusion : Thus we have studied about Bresenham’s Line algorithm.


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Experiment NO 3

Aim : Implement Midpoint Circle Algorithm


Theory :
The mid-point circle drawing algorithm is an algorithm used to determine the points needed for
rasterizing a circle.

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

printf("*********** MID POINT Circle drawing algorithm ********\n\n");

printf("\nenter the coordinates= ");

scanf("%d %d",&x_mid,&y_mid);

printf("\n now enter the radius =");

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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();

Conclusion : Thus we have studied about Midpoint circle algorithm.


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Experiment No 4

Aim : Implement Area Filling Algorithm: Boundary Fill, Flood Fill.


Theory:
Boundary-fill Algorithm

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.

In this, generally two methods are given that are:

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).

Algorithm for boundary fill (4-connected)


Boundary fill (x, y, fill, boundary)

1) Initialize boundary of the region, and variable fill with color.


2) Let the interior pixel(x,y)
(Now take an integer called current and assign it to (x,y))
current=getpixel(x,y)
3) If current is not equal to boundary and current is not equal
to fill then set pixel (x, y, fill)
boundary fill 4(x+1,y,fill,boundary)
boundary fill 4(x-1,y,fill,boundary)
boundary fill 4(x,y+1,fill,boundary)
boundary fill 4(x,y-1,fill,boundary)

4) End.
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Program

#include<stdio.h>

#include<graphics.h>

#include<dos.h>

Void boundaryfill(int x,int y, f_color,int b_color)

If(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)

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;

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

Boundaryfill(x,y,4,15);
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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.

Algorithm for Flood fill algorithm


floodfill4 (x, y, fillcolor, oldcolor: integer)

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Program

#include<stdio.h>
#include<graphics.h>
#include<dos.h>

void floodFill(int x,int y,int oldcolor,int newcolor)


{
if(getpixel(x,y) == oldcolor)
{
putpixel(x,y,newcolor);
floodFill(x+1,y,oldcolor,newcolor);
floodFill(x,y+1,oldcolor,newcolor);
floodFill(x-1,y,oldcolor,newcolor);
floodFill(x,y-1,oldcolor,newcolor);
}
}
//getpixel(x,y) gives the color of specified pixel

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 :

Conclusion : Thus we have studied about Area filling algorithm.


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Experiment No 5

Aim : Implement Scan line Polygon Filling Algorithm

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Program
#include<stdio.h> #include<conio.h>

#include<math.h>

#include<dos.h>

#include<graphics.h>

void main()

int n,i,j,k,gd,gm,dy,dx; int

x,y,temp;

int a[20][2],xi[20]; float

slope[20]; clrscr();

printf(“\n\Enter no.of edges of polygon”);

scanf(“%d”,&n);

printf(“\n\Enter co-ordinates of polygon”);

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Output :

Conclusion : Thus we have studied about Scan line Polygon filling algorithm.
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Experiment No 6

Aim : Implement 2D Transformations: Translation, Scaling, Rotation, Reflection, Shear.


Theory :

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

printf("Enter the fixed point\n");


scanf("%d%d",&xa,&ya);
theta=(float)(90*(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 2:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(270*(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:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(180*(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
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Output :

Enter the upper left corner of the rectangle: 100


100
Enter the lower right corner of the rectangle: 200
200
******2DTransformations*******
1.Translation
2.Rotation 3.Scaling
4.Reflection
5.Shearing 6.Exit

Enter your choice: 1

*******Translation*******
Enter the value of shift vector: 150
150
******2DTransformations*******
1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit

Enter your choice: 2

*******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

Enter your choice: 3

********Scaling*******
Enter the value of scaling factor: 2
2
******2DTransformations*******
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

1.Translation
2.Rotation
3.Scaling
4.Reflection
5.Shearing
6.Exit

Enter your choice: 4

*******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

Enter your choice: 5


*******Shearing*********
1.x-direction shear
2.y-direction shear Enter
your choice: 1
Enter the value of shear : 2 Enter
your choice: 2
Enter the value of shear : 2

Conclusion : Thus we have studied about 2D Transformation.


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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 x,y,x1,y1,i, xc,yc;

int gr=DETECT,gm;

initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");

printf("\n****** Cohen Sutherlsnd Line Clipping algorithm ***********");

printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);

printf("\n First enter XMax, YMax =");

scanf("%d %d",&W_xmax,&W_ymax);
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

printf("\n Please enter intial point x and y= ");

scanf("%d %d",&x,&y);

printf("\n Now, enter final point x1 and y1= ");

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

//end point of Line

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)

printf("No need of clipping as it is already in window");

flag=1;

for(i=0;i<4;i++){

region_code[i]= rcode_begin[i] && rcode_end[i] ;


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

if(region_code[i]==1)

flag=0;

if(flag==0)

printf("\n Line is completely outside the window");

else{

slope=(float)(y1-y)/(x1-x);

if(rcode_begin[2]==0 && rcode_begin[3]==1) //left

y=y+(float) (W_xmin-x)*slope ;

x=W_xmin;

if(rcode_begin[2]==1 && rcode_begin[3]==0) // right

y=y+(float) (W_xmax-x)*slope ;

x=W_xmax;

if(rcode_begin[0]==1 && rcode_begin[1]==0) // top

x=x+(float) (W_ymax-y)/slope ;

y=W_ymax;
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

if(rcode_begin[0]==0 && rcode_begin[1]==1) // bottom

x=x+(float) (W_ymin-y)/slope ;

y=W_ymin;

// end points

if(rcode_end[2]==0 && rcode_end[3]==1) //left

y1=y1+(float) (W_xmin-x1)*slope ;

x1=W_xmin;

if(rcode_end[2]==1 && rcode_end[3]==0) // right

y1=y1+(float) (W_xmax-x1)*slope ;

x1=W_xmax;

if(rcode_end[0]==1 && rcode_end[1]==0) // top

x1=x1+(float) (W_ymax-y1)/slope ;

y1=W_ymax;
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

if(rcode_end[0]==0 && rcode_end[1]==1) // bottom

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();

Conclusion : Thus we have studied about line clipping algorithm.


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Experiment No 8

Aim : Program to perform 3D Transformation

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

int gd=DETECT,gm;

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"e:\\tc\\bgi");

printf("\n 1.Translation \n2.Scaling\n 3.Rotation \n 4.exit"); printf("enter

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

case 4 : break;

printf("enter your choice");

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

printf("Enter translation factor");

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

midx=maxx/2; midy=maxy/2;

//axis();

bar3d(midx+50,midy-100,midx+60,midy-90,5,1);

printf("before translation\n");

printf("Enter scaling factors\n");

scanf("%d %d %d", &x,&y,&z);

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

printf("Enter rotating angle");

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

// axis();

// printf("After rotation about z axis");

// bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);

//axis();

printf("After rotation about x axis");

bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);

//axis();

printf("After rotation about yaxis");

bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);

Output:

Translation

Scaling
SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Rotation

Conclusion : Thus we have studied about 3D Transformation


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Experiment No 9

Aim : Character Generation: Bitmap method

Theory :

Character generation methods


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Program

Program for character generation using bitMap

#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

Conclusion : Thus we have studied about Character Generation


SARASW ATI EDUCATION SOCIETY

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Experiment No 10

Aim : Implement Polygon Clipping algorithm

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

Output

After entering number of vertices you’ll be asked to enter coordinates of vertices.

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

YADAVRAO TASGAONKAR INSTITUTE OF ENGINEERING &TECHNOLOGY


DR.N.Y.TASGAONKAR TECHNICAL EDUCATIONAL COMPUS, CHANDHAI AT.POST: -NASARPUR, TAL:-KARJAT, DIST:-RAIGAD

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.

Conclusion: Thus we have studied about Polygon clipping algorithm.

You might also like