Sei sulla pagina 1di 33
Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 1

Nil DA6210-Computer Graphics Lab Manual Clause: Nil Page: 1 DIT UNIVERSITY DEHRADUN DEPARTMENT OF COMPUTER SCIENCE

DIT UNIVERSITY DEHRADUN

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Lab Manual for the Academic Year 2018-19

Subject Subject code Course coordinator HOD

: Computer Graphics : DA6210 : Kushal Gupta : Dr. Vishal Bharti

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9
S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9

S. No

1

2

S. No 1 2 Experiment No. 1 2 3 4 5 6 7 8 9 10

Experiment

No.

1

2

3

4

5

6

7

8

9

10

DA6210-Computer Graphics Lab Manual

Table of Contents

Issue No.:

Date:

Rev No.: Nil

Clause: Nil

Content

System Requirements

Lab Objectives

Experiment Manual

Title of experiment

Introduction to Graphics functions and construction of

smileys

Line Drawing: General Approach

Line Drawing: DDA Algorithm

Line Drawing: Bresenham’s Algorithm

Circle Drawing: Bresenham’s Algorithm

2-D Transformations: Translation and Scaling using a

HUT

2-D Transformations: Rotation

Polygon Filling: Flood Fill Algorithm

Polygon Filling: Boundary Fill Algorithm

Line Clipping: Cohen Sutherland Algorithm

Page No.

3

4

Line Clipping: Cohen Sutherland Algorithm Page No. 3 4 Page No. 5 9 12 15 18

Page No.

5

9

12

15

18

20

22

24

26

28

Algorithm Page No. 3 4 Page No. 5 9 12 15 18 20 22 24 26

Rev. Date: Nil

Page: 2

3 4 Page No. 5 9 12 15 18 20 22 24 26 28 Rev. Date:
3 4 Page No. 5 9 12 15 18 20 22 24 26 28 Rev. Date:

Reviewed by:

Approved by:

Prepared by:

Kushal Gupta

Prepared by: Kushal Gupta
Prepared by: Kushal Gupta
Prepared by: Kushal Gupta
Prepared by: Kushal Gupta
Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 3

System Requirements

1. Intel based desktop PC of 2GHz or faster processor with at least 256 MB RAM

and 256 MB free disk space.

2. C or C++ compiler.

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 4

Lab Objectives

1. To provide an understanding of basic computer graphics with different color

model like RGB, CMYK etc.

2. To provide an understanding to draw basic objects like line, polygon, curve etc.

and different transformation of objects.

3. To

provide

an

understanding

for

projection, world view etc.

object

clipping,

windowing,

real

object

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 5

Practical 1

Objective

Introduction of basic graphics functions in C language.

1. getmaxx():

It returns the maximum x co-ordinate value(screen-relative) for the current graphics driver and mode.

2. arc(int x, int y, int start_angle, int end_angle, int radius):

It draws a circular arc in the current drawing color with the specified center, start and end angle and radius.

3. setcolor ( int color):

It sets the current drawing color to specified color (which can range from 0 to getmaxcolor ).

4. ellipse(int x, int y, int start_angle, int end_angle, int x_radius, int y_radius) :

Draws elliptical arc in the current drawing color having radius(x,y) and specified radius.

5. line(int x1, int y1, int x2, int y2):

Draws a line from (x1,y1) to (x2,y2) using the current color, line style, and thickness.

6. setcolor(int color) :

Sets the current drawing color to specified color (which can range from 0 to getmaxcolor ).

7. circle(int x, int y, int radius):

Draws a circle in current drawing color having center (x,) and specified radius.

8. setfillstle(int pattern, int color):

Sets the mentioned fill style and fill color.

9. floodfill(int x, int y, int border):

Fills the region with the current drawing color and pattern in which the point (x,y) enclosed with the mentioned border color.

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 6

10.closegraph(void):

It deallocates all memory allocated by the graphics system.It then restores the screen to the mode it was before calling initgraph.

Construction of smileys using graphics in C language.

Source Code

#include<conio.h>

#include<stdio.h>

#include<graphics.h>

int main()

{

int gd=DETECT,gm,X,Y;

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

X=getmaxx();

Y=getmaxy();

printf("%d %d",X,Y);

setcolor(WHITE);

//Quadrant Lines

line(X/2,0,X/2,Y);

line(0,Y/2,X,Y/2);

//Quadrant 1 Smiley setcolor(YELLOW); circle(3*X/4,Y/4,X/6); //Main Circle line((3*X/4),(Y/4)-10,(3*X/4),(Y/4)+10); //Nose circle((3*X/4)-50,(Y/4)-50,10); //Eye circle((3*X/4)+50,(Y/4)-50,10); //Eye

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil
Issue No.:
Date:
Rev No.: Nil
Rev. Date: Nil
DA6210-Computer Graphics Lab
Manual
Clause: Nil
Page: 7
ellipse((3*X/4),(Y/4+60),0,360,50,25); //Mouth
setfillstyle(SOLID_FILL,WHITE);
floodfill((3*X/4)-50,(Y/4)-50,YELLOW);//Eye Color
floodfill((3*X/4)+50,(Y/4)-50,YELLOW);//Eye Color
setfillstyle(SLASH_FILL,MAGENTA);
floodfill((3*X/4),(Y/4+60),YELLOW);//Mouth Color
//Quadrant 2 Smiley
setcolor(YELLOW);
circle(X/4,Y/4,X/6); //Main Circle
line(X/4,(Y/4)-10,X/4,(Y/4)+10); //Nose
circle((X/4)-50,(Y/4)-50,10); //Eye
circle((X/4)+50,(Y/4)-50,10); //Eye
arc(X/4,Y/4,232,307,60);//Mouth
ellipse((X/4),(Y/4),215,325,46,80); //Mouth
setfillstyle(XHATCH_FILL,WHITE);
floodfill((X/4)-50,(Y/4)-50,YELLOW);//Eye Color
floodfill((X/4)+50,(Y/4)-50,YELLOW);//Eye Color
setfillstyle(HATCH_FILL,CYAN);
floodfill((X/4),(Y/4+60),YELLOW);//Mouth Color
//Quadrant 3 Smiley
setcolor(YELLOW);
circle(X/4,3*(Y/4),X/6); //Main Circle
line(X/4,3*(Y/4)-10,X/4,3*(Y/4)+10); //Nose
circle((X/4)-50,3*(Y/4)-50,10); //Eye
circle((X/4)+50,3*(Y/4)-50,10); //Eye
line((X/4)-40,3*(Y/4)+40,(X/4)+40,3*(Y/4)+40);//Mouth
ellipse((X/4)+20,3*(Y/4)+40,180,360,20,30);//Mouth
setfillstyle(WIDE_DOT_FILL,WHITE);
floodfill((X/4)-50,3*(Y/4)-50,YELLOW);//Eye Color
floodfill((X/4)+50,3*(Y/4)-50,YELLOW);//Eye Color
setfillstyle(INTERLEAVE_FILL,GREEN);
floodfill((X/4)+20,3*(Y/4)+45,YELLOW);//Mouth Color
//Quadrant 4 Smiley
Prepared by:
Reviewed by:
Approved by:
Kushal Gupta
Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 8

setcolor(YELLOW); circle(3*(X/4),3*(Y/4),X/6); //Main Circle line(3*(X/4),3*(Y/4)-10,3*(X/4),3*(Y/4)+10); //Nose circle(3*(X/4)-50,3*(Y/4)-50,10); //Eye circle(3*(X/4)+50,3*(Y/4)-50,10); //Eye

circle(3*(X/4),3*(Y/4)+50,20);//Mouth

setfillstyle(LINE_FILL,WHITE); floodfill(3*(X/4)-50,3*(Y/4)-50,YELLOW);//Eye Color floodfill(3*(X/4)+50,3*(Y/4)-50,YELLOW);//Eye Color

setfillstyle(LTSLASH_FILL,LIGHTBLUE); floodfill(3*(X/4),3*(Y/4)+50,YELLOW);//Mouth Color

getch();

closegraph();

}

return 0;

Output

Color getch(); closegraph(); } return 0; Output Prepared by: Reviewed by: Approved by: Kushal Gupta

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 9

Practical 2

Objective

Construction of a line using the equation y=mx+c.

Source Code

#include<conio.h>

#include<stdio.h>

#include<graphics.h>

int max(int a,int b){if(a>b){return a;}else{return b;}} int min(int a,int b){if(a<b){return a;}else{return b;}}

int main()

{

int x1,y1,x2,y2,x,y,i; float m,c; int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"");

printf("Program to construct a line using equation y=mx+c.\n"); printf("---------------------------------------------------------\n"); printf("Enter the coordinates of the the line.\n"); printf("X1 : ");

scanf("%d",&x1);

printf("Y1 : ");

scanf("%d",&y1);

printf("X2 : ");

scanf("%d",&x2);

printf("Y2 : ");

scanf("%d",&y2);

m=(float)(y2-y1)/(x2-x1);

c=(float)y1-(m*x1);

if(m>1)

{

for(i=min(y1,y2);i<=max(y1,y2);i++ )

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil
Issue No.:
Date:
Rev No.: Nil
Rev. Date: Nil
DA6210-Computer Graphics Lab
Manual
Clause: Nil
Page: 10
{
x=(int)((i-c)/m) ;
putpixel(x,i,YELLOW);
}
}
else
{
for(i=min(x1,x2);i<=max(x1,x2);i++)
{
y=(int)((m*i)+c);
putpixel(i,y,CYAN);
}
}
line(x1+10,y1,x2+10,y2);
getch();
closegraph();
return 0;
}

Output

getch(); closegraph(); return 0; } Output Prepared by: Reviewed by: Approved by: Kushal Gupta

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 11

Nil DA6210-Computer Graphics Lab Manual Clause: Nil Page: 11 Prepared by: Reviewed by: Approved by: Kushal

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 12

Practical 3

Objective

Construction of lines using Digital Differential Analyzer algorithm.

Source Code

#include<conio.h> #include<stdio.h> #include<graphics.h> void lineDDA(float x1,float y1,float
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void lineDDA(float x1,float y1,float x2,float y2)
{
float i,m,dx,dy;
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
if(dx<0)
{
i=y1;
y1=y2;
y2=i;
i=x1;
x1=x2;x2=i;
dy=dy;
dx=-dx;
}
if(dx>0.0)
{
if(m<=1 && m>=-1)
{
for(i=x1;i<=x2;i++)
{
putpixel(i,(int)y1,RED);
y1=y1+m;
}
}
else

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil
Issue No.:
Date:
Rev No.: Nil
Rev. Date: Nil
DA6210-Computer Graphics Lab
Manual
Clause: Nil
Page: 13
{
if(m<-1)
{
x1=x2;
i=y1;
y1=y2;
y2=i;
}
for(i=y1;i<=y2;i++)
{
putpixel((int)x1,i,BLUE);
x1=x1+(1/m);
}
}
}
}
int main()
{
int x1,y1,x2,y2,i;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
printf("Program to construct lines using digital differential analyzer algorithm.\n");
printf("---------------------------------------------------------\n");
for(i=0;i<4;i++)
{
printf("Enter the coordinates of a line \n");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
line(x1+5,y1,x2+5,y2);
lineDDA(x1,y1,x2,y2);
}
getch();
closegraph();
return 0;
}

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 14

Output

Graphics Lab Manual Clause: Nil Page: 14 Output Prepared by: Reviewed by: Approved by: Kushal Gupta
Graphics Lab Manual Clause: Nil Page: 14 Output Prepared by: Reviewed by: Approved by: Kushal Gupta

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 15

Practical 4

Objective

Construction of a line using Bresenham’s algorithm.

Source Code

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void Draw_Line(int X1,int Y1,int X2,int Y2)

{

 

int X,Y,D,DX,DY,DE,DNE;

X=X1;

Y=Y1;

DY=Y2-Y1;

DX=X2-X1;

D=2*DY-DX;

DE=2*DY;

DNE=2*(DY-DX);

putpixel(X,Y,WHITE);

while(X<X2)

{

if(D<=0)

{

 

D=D+DE;

 

}

else

{

 

D=D+DNE;

Y=Y+1;

 

}

X=X+1;

putpixel(X,Y,WHITE);

}

}

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

  Issue No.: Date:
 

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 16

 

int main()

 

{

int gdriver=DETECT,gmode,X,Y,X1,Y1,X2,Y2;

 

initgraph(&gdriver,&gmode,"");

printf("Program to construct a line using Bresenham's line drawing algorithm.\n"); printf("---------------------------------------------------------\n");

 

printf("Enter X1 : ");

 

scanf("%d",&X1);

printf("Enter Y1 : ");

scanf("%d",&Y1);

printf("Enter X2 : ");

scanf("%d",&X2);

printf("Enter Y2 : ");

scanf("%d",&Y2);

Draw_Line(X1,Y1,X2,Y2);

getch();

closegraph();

 

return 0;

}

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 17

Output

Graphics Lab Manual Clause: Nil Page: 17 Output Prepared by: Reviewed by: Approved by: Kushal Gupta
Graphics Lab Manual Clause: Nil Page: 17 Output Prepared by: Reviewed by: Approved by: Kushal Gupta

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 18

Practical 5

Objective

To Construct a circle using Bresenham's algorithm (Version-2).

Source Code

#include<conio.h>

#include<stdio.h>

#include<graphics.h>

void dcircle(int x,int y,int h,int k)

{

putpixel(x+h,y+k,1); putpixel (y+h,x+k,5); putpixel(-x+h,y+k,2); putpixel (-y+h,x+k,6);

putpixel(x+h,-y+k,3); putpixel (y+h,-x+k,7); putpixel(-x+h,-y+k,4); putpixel (-y+h,-x+k,8);

}

void main() { int d,de,dse,h,k,x,y,r,gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,""); printf("Enter the radius\n"); scanf("%d",&r); printf("Enter the co ordinates of center\n"); scanf("%d%d",&h,&k); x=0; y=r; d=1-r; de=3; dse=5-(2*r); dcircle(x,y,h,k); while(y>x) { if(d<0)

 

{d=d+de;

de=de+2;

dse=dse+2;}

else

 

{d=d+dse;

de=de+2;

dse=dse+4;

y--;

}

x++;

dcircle(x,y,h,k);

}

settextstyle(3,0,3);

outtextxy(100,300,"Program Executed By: Gaurav Soni"); getch();

closegraph();

}

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 19

Output

Graphics Lab Manual Clause: Nil Page: 19 Output Prepared by: Reviewed by: Approved by: Kushal Gupta
Graphics Lab Manual Clause: Nil Page: 19 Output Prepared by: Reviewed by: Approved by: Kushal Gupta

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 20

Practical 6

Objective

To draw an image of a hut and perform scaling and translation on it.

Source Code

//#include<conio.h> #include<graphics.h> #include<stdio.h> #include<math.h> int main()
//#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
int main()
{
int i,tx,ty;
float sx,sy;
// Defining Hut Coordinates
int h[11][4]={
{100,50,50,200},
{100,50,150,200},
{100,50,250,50},
{250,50,250,200},
{50,200,150,200},
{50,200,50,400},
{150,200,250,200},
{150,200,150,400},
{250,200,250,400},
{50,400,150,400},
{150,400,250,400}
};
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
printf("Program to implement translation and scaling on an image.\n\n");
printf("Enter Translation Factor (X & Y)\t: "); scanf("%d%d",&tx,&ty);
printf("Enter Scaling Factor (X & Y)\t\t: "); scanf("%f%f",&sx,&sy);
settextstyle(3,0,3);
outtextxy(100,420,"Program executed by Gaurav Soni");

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

  Issue No.: Date:
 

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

 

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 21

 

for(i=0;i<11;i++)

   

{

setcolor(WHITE);

// Drawing Hut

line(h[i][0],h[i][1],h[i][2],h[i][3]);

 

setcolor(YELLOW);

// Hut Translation

line(h[i][0]+tx,h[i][1]+ty,h[i][2]+tx,h[i][3]+ty);

 

setcolor(GREEN);

// Hut Scaling

line(h[i][0]*sx+75,h[i][1]*sy+240,h[i][2]*sx+75,h[i][3]*sy+240);

 

}

getch();

closegraph();

}

Output

  } getch(); closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta
  } getch(); closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 22

Practical 7

Objective

To perform rotation of an image.

Source Code

#include<conio.h> #include<graphics.h> #include<stdio.h> #include<math.h> int main()

{

int i;float r;

// Defining Hut Coordinates int h[4][13]={ {100,100,150,350,400,130,200,130,170,200,200,150,100}, {100,100,50 ,25 ,75 ,200,200,150,150,100,100,50 ,200},

{100,150,350,400,400,130,400,170,170,200,400,200,200},

{200, 50,25 ,75 ,175,150,175,150,200,200,75 ,100,200} }; int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,""); printf("Program to rotate an image.\n\n"); printf("Enter Rotation Angle : "); scanf("%f",&r);

settextstyle(3,0,3);

outtextxy(250,400,"Program executed by : Gaurav Soni");

r=(3.14159265/180.0)*r;

for(i=0;i<13;i++)

{

setcolor(WHITE);

// Drawing Hut

line(h[0][i],h[1][i],h[2][i],h[3][i]);

// Hut Rotation h[0][i]=h[0][i]* cos (r) -h[1][i]*sin(r) ; h[1][i]=h[0][i]*sin(r) +h[1][i]*cos(r) ; h[2][i]=h[2][i]* cos(r) -h[3][i]*sin(r) ; h[3][i]=h[2][i]*sin(r) +h[3][i]*cos(r) ;

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

  Issue No.: Date:
 

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

 

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 23

 

setcolor(YELLOW);

// Drawing Rotated Hut

 

line(h[0][i],h[1][i],h[2][i],h[3][i]);

 

}

getch();

closegraph();

}

Output

  } getch(); closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta
  } getch(); closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 24

Practical 8

Objective

To implement Boundary Filling algorithm.

Source Code

#include<conio.h>

#include<stdio.h>

#include<graphics.h>

void bound(int x,int y,int n,int o)

{

if(getpixel(x,y)!=o&&getpixel(x,y)!=n)

{

putpixel(x,y,n);

 

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

// Providing Delay

{

for(int j=0;j<1500;j++)

{

}

 

}

bound(x+1,y,n,o); bound(x,y+1,n,o); bound(x-1,y,n,o); bound(x,y-1,n,o);

}

}

int main()

{

int x,y,gd=DETECT,gm; initgraph(&gd,&gm,""); printf("Program to implement Boundary Filling algorithm.\n\n"); printf("Enter seed point [Range (150,150) -> (200,200)]\t: ");

settextstyle(3,0,3);

outtextxy(100,200,"Program executed by : Gaurav Soni"); scanf("%d %d",&x,&y);

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

  Issue No.: Date:
 

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 25

 

setbkcolor(8);

 

setcolor(14); rectangle(150,150,200,200);

 

setcolor(3);line(155,155,155,175);

setcolor(2); line(175,155,175,175);

bound(x,y,1,14);

getch();

closegraph();

}

Output

bound(x,y,1,14); getch(); closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta
bound(x,y,1,14); getch(); closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta
bound(x,y,1,14); getch(); closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 26

Practical 9

Objective

To implement the Flood Fill algorithm.

Source Code

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void flood(int x,int y,int old)

{

if(getpixel(x,y)==old)

{

putpixel(x,y,YELLOW);

delay(9);

flood(x+1,y,old);

flood(x,y+1,old);

flood(x-1,y,old);

flood(x,y-1,old);

}

}

int main()

{

int x,y,gd=DETECT,gmode; initgraph(&gd,&gmode,""); printf("Program to implement Flood Filling algorithm.\n\n"); printf("Enter seed point [Range (100,100) -> (160,160)]\t: "); scanf("%d %d",&x,&y);

settextstyle(3,0,3);

outtextxy(100,200,"Program executed by : Gaurav Soni");

setbkcolor(8);

setcolor(RED); rectangle(100,100,160,160); setcolor(GREEN); line(115,115,115,155); setcolor(BLUE); line(155,115,155,155); setcolor(YELLOW); flood(x,y,0);

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

  Issue No.: Date:
 

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 27

 

getch();

 

closegraph();

}

Output

27   getch();   closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta
27   getch();   closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta
27   getch();   closegraph(); } Output Prepared by: Reviewed by: Approved by: Kushal Gupta

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil
Issue No.:
Date:
Rev No.: Nil
Rev. Date: Nil
DA6210-Computer Graphics Lab Manual
Clause: Nil
Page: 28
Practical 10
Objective
Line Clipping: Cohen Sutherland Algorithm.
Source Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"");
printf("Please enter the bottom left co-ordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("Please enter the top right co-ordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("Please enter the co-ordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("Please enter the co-ordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)
Prepared by:
Reviewed by:
Approved by:
Kushal Gupta
  Issue No.: Date:
 

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 29

 

{

start[i]=0;

end[i]=0;

}

m=(y2-y1)/(x2-x1);

 

if(x1 <xmin) start[0]=1;

if(x1 >xmax) start[1]=1;

if(y1 >ymax) start[2]=1;

if(y1 <ymin) start[3]=1;

if(x2 <xmin) end[0]=1;

if(x2 >xmax) end[1]=1;

if(y2 >ymax) end[2]=1;

if(y2 <ymin) end[3]=1;

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

code[i]=start[i]&&end[i];

 

if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))

 

{

if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]==0)&&(end

[2]==0)&&(end[3]==0))

 

{

cleardevice();

 

printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");

 

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta

Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil
Issue No.:
Date:
Rev No.: Nil
Rev. Date: Nil
DA6210-Computer Graphics Lab Manual
Clause: Nil
Page: 30
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
else
{
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
Prepared by:
Reviewed by:
Approved by:
Kushal Gupta
Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil
Issue No.:
Date:
Rev No.: Nil
Rev. Date: Nil
DA6210-Computer Graphics Lab Manual
Clause: Nil
Page: 31
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
Prepared by:
Reviewed by:
Approved by:
Kushal Gupta
Issue No.: Date: Rev No.: Nil Rev. Date: Nil DA6210-Computer Graphics Lab Manual Clause: Nil
Issue No.:
Date:
Rev No.: Nil
Rev. Date: Nil
DA6210-Computer Graphics Lab Manual
Clause: Nil
Page: 32
x2=xmax;
}
clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}
Prepared by:
Reviewed by:
Approved by:
Kushal Gupta
  Issue No.: Date:
 

Issue No.:

Date:

Rev No.: Nil

Rev. Date: Nil

DA6210-Computer Graphics Lab Manual

Clause: Nil

Page: 33

Output

 
 
Graphics Lab Manual Clause: Nil Page: 33 Output   Prepared by: Reviewed by: Approved by: Kushal
Graphics Lab Manual Clause: Nil Page: 33 Output   Prepared by: Reviewed by: Approved by: Kushal

Prepared by:

Reviewed by:

Approved by:

Kushal Gupta