Download - Sem 4: CG Practical
-
8/2/2019 Sem 4: CG Practical
1/57
COMPUTER
GRAPHICS
LAB
NEIL MATHEW 4CS4 Y3305 - A2324710002
-
8/2/2019 Sem 4: CG Practical
2/57
NEIL MATHEW 4CS4 Y3305 - A2324710002
CONTENTS:
Sno: Program Sign1 WA P u s i n g 1 0 g r a p h i c s f u n c t i o n s .2 WA P t o d r a w l i n e u s i n g DDA A l g o r i t h m
3 WA P t o d r a w s c e n e r y u s i n g 2 0 g r a p h i cf u n c t i o n s .4 WA P t o d r a w a l i n e u s i n g B r e s e n h a m' s A l g o5 WA P t o d r a w a c i r c l e u s i n g Br e s e n h a m' s A l g o6 WA P t o d r a w a c i r c l e u s i n g Mi d - P o i n t A l g o7 WA P t o d r a w a n e l l i p s e u s i n g Mi d - P o i n t A l g o8 WA P t o p e r f o r m L i n e Cl i p p i n g9 WA P t o p e r f o r m s c a l i n g o f a t r i a n g l e .
10 WA P t o p e r f o r m t r a n s l a t i o n11 WA P t o p e r f o r m S c a l i n g w. r . t a Po i n t .12 WA P t o p e r f o r m r o t a t i o n w. r . t t o a p o i n t .13 WA P t o p e r f o r m r e f l e c t i o n w. r . t t o a l i n e14 P r o j e c t
-
8/2/2019 Sem 4: CG Practical
3/57
Q1: Wap using 10 graphics functions.
/*Functions Used:
1 getmaxx()
2 getmaxy()
3 Line()
4 Rectangle()
5 Drawpoly()
6 Circle()
7 Arc()
8 Pieslice()
9 Setcolor()
10 Setbkcolor()
11 Setfillstyle()
*/
#include
#include
#include
int main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "C:/TC/BGI/");
setbkcolor(0);
setcolor(8);
//Asteroid Backgroumd
int i,j;
for(i=0; i
-
8/2/2019 Sem 4: CG Practical
4/57
setcolor(15);
//Polygons : Triangles
int points_up[]={getmaxx()*0.90, getmaxy()*0.85-60,
getmaxx()*0.90-20,getmaxy()*0.85-30,
getmaxx()*0.90+20, getmaxy()*0.85-30,
getmaxx()*0.90, getmaxy()*0.85-60
};
int points_down[]={getmaxx()*0.90, getmaxy()*0.85+60,
getmaxx()*0.90-20,getmaxy()*0.85+30,
getmaxx()*0.90+20, getmaxy()*0.85+30,
getmaxx()*0.90, getmaxy()*0.85+60
};
int points_left[]={getmaxx()*0.90-20-10, getmaxy()*0.85+20,
getmaxx()*0.90-20-10,getmaxy()*0.85-20,
getmaxx()*0.90-20-40, getmaxy()*0.85,
getmaxx()*0.90-20-10, getmaxy()*0.85+20
};
int points_right[]={getmaxx()*0.90+20+10, getmaxy()*0.85+20,
getmaxx()*0.90+20+10,getmaxy()*0.85-20,
getmaxx()*0.90+20+40, getmaxy()*0.85,
getmaxx()*0.90+20+10, getmaxy()*0.85+20
};
drawpoly(4,points_up);
drawpoly(4,points_down);
drawpoly(4,points_left);
drawpoly(4,points_right);
//Square
rectangle(getmaxx()*0.90-20, getmaxy()*0.85-20, getmaxx()*0.90+20,
getmaxy()*0.85+20);
-
8/2/2019 Sem 4: CG Practical
5/57
setfillstyle(SOLID_FILL ,6);
setcolor(0);
//Pieslices (Pacmans)
pieslice(getmaxx()*0.25,getmaxy()*0.25, 30, 360-30, 30);
pieslice(getmaxx()*0.09,getmaxy()*0.85, 30, 360-30, 30);
pieslice(getmaxx()-10,getmaxy()*0.35, 30, 360-30, 30);
pieslice(getmaxx()*0.5-20,getmaxy()*0.5+35, 30, 360-30, 30);
setcolor(2);
//Borderline Rectangle
rectangle(0,0,getmaxx(),getmaxy());
//Outer Horizontal Lines
line(0, getmaxy()*0.5,getmaxx()*0.5-40, getmaxy()*0.5);
line(getmaxx()*0.5+40, getmaxy()*0.5, getmaxx(), getmaxy()*0.5 );
//Outer Vertical Lines
line(getmaxx()*0.5,0,getmaxx()*0.5, getmaxy()*0.5-40);
line(getmaxx()*0.5, getmaxy()*0.5+40,getmaxx()*0.5,getmaxy());
//Arcs
arc(getmaxx()*0.5,getmaxy()*0.5, 180-30, 180+30, 40);
arc(getmaxx()*0.5,getmaxy()*0.5, 90-30, 90+30, 40);
arc(getmaxx()*0.5,getmaxy()*0.5, 0-30, 0+30, 40);
arc(getmaxx()*0.5,getmaxy()*0.5, 270-30, 270+30, 40);
setcolor(4);
//Innermost Circle
circle(getmaxx()*0.5,getmaxy()*0.5,20);
circle(getmaxx()*0.5,getmaxy()*0.5,21);
//Inner Plus
line(getmaxx()*0.5-20, getmaxy()*0.5,getmaxx()*0.5+20, getmaxy()*0.5);
line(getmaxx()*0.5, getmaxy()*0.5-20,getmaxx()*0.5, getmaxy()*0.5+20);
getch();
closegraph();
return 1;
}
-
8/2/2019 Sem 4: CG Practical
6/57
PROGRAM1
-
8/2/2019 Sem 4: CG Practical
7/57
Q2:WaptodrawalineusingDDAAlgorithm./* Functions used:
1 putpixel()
2 outtextxy()3 setbkcolor()
*/
#include //for cout,cin
#include //for putpixel()
#include //for round()
#include //for getch(),clrscr()
void LINE(int x1,int y1,int x2,int y2)
{
int dx,dy;
float step;
float xincrement,yincrement;
float x,y;
dx=x2-x1;
dy=y2-y1;
if( abs(dx) > abs(dy) )
step=abs(dx);
else
step=abs(dy);
xincrement=dx/step;
yincrement=dy/step;
-
8/2/2019 Sem 4: CG Practical
8/57
x=x1; y=y1;
putpixel(x,y,15);
outtextxy(x-15,y-15,"START");
for(int i=1; i>y1;
coutx2>>y2;
cout
-
8/2/2019 Sem 4: CG Practical
9/57
ROGRA
M2
-
8/2/2019 Sem 4: CG Practical
10/57
Q3:Waptodrawasceneryusing20graphicsfunctions./* Functions Used:
01 getmaxx()
02 getmaxy()
03 outtextxy()
04 setbkcolor()
05 setcolor()
06 settextstyle()
07 setlinestyle()
08 setfillstyle()
09 floodfill()
10 putpixel
11 line
12 rectangle
13 bar
14 circle
15 sector
16 arc
17 pieslice
18 ellipse
19 fillellipse
20 fillpoly
*/
#include
#include
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver, &gmode, "C:/TC/BGI");
setbkcolor(WHITE);
setcolor(WHITE);
//LINE BTW SKY AND GRASS
line(0,getmaxy()*0.75, getmaxx(),getmaxy()*0.75);
//GRASS
setfillstyle(SOLID_FILL,GREEN);
floodfill(getmaxx(),getmaxy(),WHITE);
-
8/2/2019 Sem 4: CG Practical
11/57
//SKY
setfillstyle(SOLID_FILL,LIGHTCYAN);
floodfill(0,0,WHITE);
//SUN
setcolor(RED);
setfillstyle(SOLID_FILL,YELLOW);
circle(getmaxx()*0.90,getmaxy()*0.15,40);
floodfill(getmaxx()*0.90,getmaxy()*0.15,RED);
//RAINBOW
for(int i=1, j=0; i
-
8/2/2019 Sem 4: CG Practical
12/57
//TEXT
setcolor(DARKGRAY);
settextstyle(9,HORIZ_DIR,3);
outtextxy(getmaxx()*0.75-30, getmaxy()*0.75+20, "Paradise?");
//POND
setfillstyle(SOLID_FILL,BLUE);
pieslice(getmaxx()*0.10,getmaxy()*0.75+1,180,360,getmaxx()*0.10);
//POND > LEAF
setfillstyle(SLASH_FILL,GREEN);
fillellipse(getmaxx()*0.11,getmaxy()*0.75+35,10,5);
//HOUSE > WALL
setcolor(WHITE);
setfillstyle(SOLID_FILL,BROWN);bar(getmaxx()*0.5-50, getmaxy()*0.75-1,getmaxx()*0.5+50,
getmaxy()*0.75-100);
//HOUSE > DOOR
rectangle(getmaxx()*0.5-15, getmaxy()*0.75-1,getmaxx()*0.5+15,
getmaxy()*0.75-50);
putpixel(getmaxx()*0.5+10,getmaxy()*0.75-25,15);
//HOUSE > ROOF
int points[]={ getmaxx()*0.5+50, getmaxy()*0.75-100,
getmaxx()*0.5, getmaxy()*0.75-150,
getmaxx()*0.5-50, getmaxy()*0.75-100,
getmaxx()*0.5+50, getmaxy()*0.75-100
};
fillpoly(4,points);
//HOUSE > ROOF > WINDOW
setcolor(WHITE);
ellipse(getmaxx()*0.5, getmaxy()*0.75-120,0,360, 7,5);
getch();
closegraph();
}
-
8/2/2019 Sem 4: CG Practical
13/57
ROGRA
SCENER
M 3
Y
-
8/2/2019 Sem 4: CG Practical
14/57
Q4:WAPtodrawalineusingBresenham'sAlgorithm
#include
#include#include
#include
void LINE(int x1, int y1, int x2, int y2)
{
int x=x1;
int y=y1;
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int Sx=abs(x2-x1)/(x2-x1); //Sign
int Sy=abs(y2-y1)/(x2-x1);
int steps,flag;
//Determining Greater length
if(dy > dx)
{
steps=dy;
flag=1;
//Swapping dx dy
int t=dx;
dx=dy;
dy=t;
}
else
{
steps=dx;
flag=0;
}
//Decision variable
int P=2*dy-dx;
//First Pixel
putpixel(x,y,WHITE);
-
8/2/2019 Sem 4: CG Practical
15/57
//Other Pixels:
for(int i=1; i 0 )
{
if(flag==1)
x=x+Sx;
else
y=y+Sy;
P=P-2*dx;
} //END OF WHILE
if(flag==1)
y=y+Sy;
else
x=x+Sx;
P=P+2*dy;
putpixel(x,y,WHITE);
} //END OF FOR
}
void main()
{
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "C:/TC/BGI");
int x1,x2,y1,y2;
setbkcolor(DARKGRAY);setcolor(WHITE);
coutx1>>y1;
coutx2>>y2;
cout
-
8/2/2019 Sem 4: CG Practical
16/57
PROGR
M4
-
8/2/2019 Sem 4: CG Practical
17/57
Q5:WAPtodrawacircleusingBresenhamsAlgorithm.#include
#include
#include
#include
void otherSpoints(int x, int y,int xc,int yc)
{
putpixel(-x+xc,y+yc,WHITE);
putpixel(x+xc,-y+yc,WHITE);
putpixel(-x+xc,-y+yc,WHITE);
putpixel(y+xc,x+yc,WHITE);
putpixel(-y+xc,x+yc,WHITE);
putpixel(y+xc,-x+yc,WHITE);
putpixel(-y+xc,-x+yc,WHITE);
}
void CIRCLE(int r)
{
int x,y;int xc=getmaxx()/2;
int yc=getmaxy()/2;
// Plotting the First Point
x=0;
y=r;
putpixel(xc+x,yc+y,WHITE);
// Other Symmetry points:
otherSpoints(x,y,xc,yc);
//Initializing Decision Variable
int d=3-2*r;
//Plotting Other Points of Circle
while(x
-
8/2/2019 Sem 4: CG Practical
18/57
void main()
{
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "C:/TC/BGI");
setbkcolor(DARKGRAY);setcolor(WHITE);
int r;
coutr;
cout
-
8/2/2019 Sem 4: CG Practical
19/57
PROGRAM5
-
8/2/2019 Sem 4: CG Practical
20/57
Q6: WAP to draw a circle using Mid-Point Algorithm.
#include
#include
#include
void DrawPoints(int x, int y, int xc, int yc)
{
putpixel(xc+x,yc+y, CYAN);
putpixel(xc-x,yc+y, CYAN);
putpixel(xc+x,yc-y, CYAN);
putpixel(xc-x,yc-y, CYAN);
putpixel(xc+y,yc+x, CYAN);
putpixel(xc+y,yc-x, CYAN);
putpixel(xc-y,yc+x, CYAN);
putpixel(xc-y,yc-x, CYAN);
}
void MidPontCircle(int xc, int yc, int r)
{
int x=0;
int y=r;
DrawPoints(x,y,xc,yc);
int P= (int) 5.0/4.0 - r;
while(x < y)
{
if(P < 0)
{
P=P+2*x+3;
DrawPoints(++x, y, xc, yc);
}
else
{
P=P+2*(x-y)+5;
DrawPoints(++x,--y, xc, yc);
}
} }
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
int r;
int xc=getmaxx()/2;
int yc=getmaxy()/2;
coutr;
MidPontCircle(xc,yc,r);
getch();
closegraph();
}
-
8/2/2019 Sem 4: CG Practical
21/57
PROGRAM 6
-
8/2/2019 Sem 4: CG Practical
22/57
Q7: WAP to draw an elipse using Mid-Point Algorithm.
#include
#include#include
void DrawPoints(int x, int y, int xc, int yc)
{
putpixel(xc+x,yc+y, CYAN);
putpixel(xc-x,yc+y, CYAN);
putpixel(xc+x,yc-y, CYAN);
putpixel(xc-x,yc-y, CYAN);
}
void Eclipse(int xc, int yc, float a, float b)
{int x=0;
int y=b;
DrawPoints(x,y,xc,yc);
float P= b*b - a*a*b + 0.25*a*a;
while( 2*b*b*x = 0 ) //x!=a || y!=0 );
{ if(P2 < 0)
{
DrawPoints(++x,--y, xc, yc);
P2=P2+ 2*b*b*x - 2*a*a*y + a*a;
}
else
{
DrawPoints(x,--y, xc, yc);
P2=P2 - 2*a*a*y + a*a;
}
}
}
-
8/2/2019 Sem 4: CG Practical
23/57
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
float a,b;
int xc=getmaxx()/2;
int yc=getmaxy()/2;
couta>>b;
Eclipse(xc,yc,a,b);
getch();
closegraph();
}
-
8/2/2019 Sem 4: CG Practical
24/57
PROGRAM 7
-
8/2/2019 Sem 4: CG Practical
25/57
Q8: WAP to perform line clipping.
#include
#include
#include
int n; //No of Lines
int ClipWindow[2][2]; //Clipping Window Coordinates
/* For 2 vertices A and C, each having x and y coordinates*/
int Line[20][4]; //Line Coordinates
/* The 20 represents total number of lines possible
and the 4 represents the 4 coordinates required to draw ONE line */
void Draw()
{
clrscr();
int gdriver=DETECT,gmode;
initgraph(&gdriver, &gmode, "C:/TC/BGI");
setbkcolor(BLACK);
setcolor(WHITE);
for(int i=0; iClipWindow[1][0];
cin>>ClipWindow[1][1];
}
-
8/2/2019 Sem 4: CG Practical
26/57
long CheckBit(long x,long y)
{
long b=5; /* The 5 here is any random non zero number.
This ensures 0000 doesn't become 0 */
int Xmax= ClipWindow[0][0] > ClipWindow[1][0] ? ClipWindow[0][0] :
ClipWindow[1][0];
int Xmin= ClipWindow[0][0] < ClipWindow[1][0] ? ClipWindow[0][0] :ClipWindow[1][0];
int Ymax= ClipWindow[0][1] > ClipWindow[1][1] ? ClipWindow[0][1] :
ClipWindow[1][1];
int Ymin= ClipWindow[0][1] < ClipWindow[1][1] ? ClipWindow[0][1] :
ClipWindow[1][1];
//Case 4:
if(x < Xmin)
b=b*10 + 1;
else
b=b*10 + 0;
//Case 3:if(x > Xmax)
b=b*10 + 1;
else
b=b*10 + 0;
//Case 2:
if(y < Ymin)
b=b*10 + 1;
else
b=b*10 + 0;
//Case 1:
if(y > Ymax)
b=b*10 + 1;
else
b=b*10 + 0;
return b;
}
long AND(long a, long b)
{
//Using manipulated Palindrome Algo
long c;
int digitA;
int digitB;
int digitC[4];
for(int i=3; i>=0; i--)
{
digitA=a%10;
digitB=b%10;
if( digitA==1 && digitB==1)
digitC[i]=1;
else
digitC[i]=0;
a/=10;
b/=10;}
-
8/2/2019 Sem 4: CG Practical
27/57
c=5;
for(i=0; i ClipWindow[1][0] ? ClipWindow[0][0] :
ClipWindow[1][0];
int Xmin= ClipWindow[0][0] < ClipWindow[1][0] ? ClipWindow[0][0] :
ClipWindow[1][0];
int Ymax= ClipWindow[0][1] > ClipWindow[1][1] ? ClipWindow[0][1] :
ClipWindow[1][1];
int Ymin= ClipWindow[0][1] < ClipWindow[1][1] ? ClipWindow[0][1] :
ClipWindow[1][1];
long b1, b2, b1ANDb2;
int i,j,k;int digit_b1, digit_b2;
double Slope;
for(i=0; i
-
8/2/2019 Sem 4: CG Practical
28/57
//Partially Inside Cases
else if( b1!=50000 || b2!=50000 )
{
if( (Line[i][2] - Line[i][0]) == 0)
Slope= 1.0;else
Slope= (double) ((Line[i][3]-Line[i][1])/(Line[i][2]-Line[i][0]));
for(j=1; j
-
8/2/2019 Sem 4: CG Practical
29/57
case 4:
if(digit_b1==1)
{
Line[i][1]= Line[i][1] + ( (Xmin - Line[i][0] ) * Slope) ;
Line[i][0]=Xmin;
}else if(digit_b2==1)
{
Line[i][3]= Line[i][3] + ( (Xmin - Line[i][2] ) * Slope) ;
Line[i][2]=Xmin;
}
break;
} //End of Case
b1/=10;
b2/=10;
} //End of inner For Loop
b1=CheckBit(Line[i][0],Line[i][1]);
b2=CheckBit(Line[i][2],Line[i][3]);
b1ANDb2=AND(b1,b2);
if( b1!=50000 || b2!=50000 )
{ i--; continue; }
} //End of Inner If-Else-if
} //End of Outer If-Else-if
} //End of Outer For Loop
}
void main()
{
clrscr();
UnClipped_Input();
Draw();
Clip();
Draw();
}
-
8/2/2019 Sem 4: CG Practical
30/57
PROGRAM 8
-
8/2/2019 Sem 4: CG Practical
31/57
Q9: WAP to perform scaling of a Triangle.
#include
#include
#include
int n=4; //Variables for drawpoly() function
int p[8];
int d[8];
int Sx,Sy; //Scaling factors.
void Input()
{
coutSx;coutSy;
cout
-
8/2/2019 Sem 4: CG Practical
32/57
void Scale()
{
int i,j,k;
// Matrix with Triangle Coordinates
int xy1[3][3] = { p[0], p[1], 1,
p[2], p[3], 1,p[4], p[5], 1,
};
// Matrix with Scaling Factors
int SI[3][3] = { Sx, 0, 0,
0, Sy, 0,
0, 0, 1
};
// Matrix with Coordinates after Scaling.
int XY1[3][3];
//Matrix multiplication: xy1 x SI = XY1for(i=0; i
-
8/2/2019 Sem 4: CG Practical
33/57
PROGRAM 9
-
8/2/2019 Sem 4: CG Practical
34/57
Q10: WAP to perform translation of a Triangle.
#include
#include
#include
int n;
int P[3][10]; //3 x n MATRIX
int P2[3][10]; //3 x n MATRIX
int T[3][3]; //3 x 3 MATRIX
int tx,ty;
void INPUT()
{
cout>n;
cout
-
8/2/2019 Sem 4: CG Practical
35/57
void Display_Matrix(int p[3][10])
{
for(int i=0; i
-
8/2/2019 Sem 4: CG Practical
36/57
PROGRAM 10
-
8/2/2019 Sem 4: CG Practical
37/57
Q11: WAP to perform scaling of a Triangle w.r.t a point.
#include
#include
#include
int n;
int P[3][10]; int P2[3][10]; int S[3][3];
int Sx,Sy,h,k;
void INPUT()
{
int ch;
cout>n;
cout
-
8/2/2019 Sem 4: CG Practical
38/57
void ScalingPoint()
{
//Scaling Matrix:
//1st - making the identity Matrix
for(int i=0; i
-
8/2/2019 Sem 4: CG Practical
39/57
PROGRAM 11
-
8/2/2019 Sem 4: CG Practical
40/57
Q12: WAP to perform rotation of a Triangle
#include
#include
#include#include
int n; int h,k;
double P[3][10]; double P2[3][10]; double S[3][3]; double angle;
double deg2rad(double deg)
{
double rad= (180.0/(22.0/7.0))*deg;
return rad;
}
void INPUT()
{int ch;
cout>n;
cout
-
8/2/2019 Sem 4: CG Practical
41/57
void ROTATEpoint()
{
double x=angle;
// S[3][3] : Scaling MATRIX
// P[3][n] : 3 x n MATRIX
// P2[3][n] : Final MATRIX
//ROTATE Matrix:
//1st - making the identity Matrix
for(int i=0; i
-
8/2/2019 Sem 4: CG Practical
42/57
void main()
{
INPUT();
Display_Matrix(P);
cout
-
8/2/2019 Sem 4: CG Practical
43/57
(without fixed point)
(with fixed point)
PROGRAM 12
-
8/2/2019 Sem 4: CG Practical
44/57
Q13: WAP to perform reflection of a Triangle about a line
#include
#include
#include#include
#include
int n;
double P[3][10];
double P2[3][10];
double S[3][3];
float m; int b;
void INPUT()
{
int ch;
cout>n;
cout>b;
}
void Display_Matrix(double p[3][10])
{
for(int i=0; i
-
8/2/2019 Sem 4: CG Practical
45/57
void ReflectPoint()
{
// S[3][3] : Reflect Transform MATRIX
// P[3][n] : 3 x n MATRIX
// P2[3][n] : Final MATRIX
//Reflect Transform Matrix:
//1st - making the identity Matrix
for(int i=0; i
-
8/2/2019 Sem 4: CG Practical
46/57
void main()
{
INPUT();
Display_Matrix(P);
cout
-
8/2/2019 Sem 4: CG Practical
47/57
PROGRAM 13
-
8/2/2019 Sem 4: CG Practical
48/57
PROJECT:TOWNIRRIGATOR(GAME)
#include
#include
#include
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define ESC 27;
///////////GLOBAL VARIABLES////////////
int c,x,y,Xmin,Xmax,Ymin,Ymax;
char ch;
int speed;
int size;
int healthX;
int FLAG_Ob;
int FLAG_ChkPnts[6];
int FLAG_ChkPntsALL;
int COUNT_ChkPnts;
void checkCheckPoint(int, int , int);
void map()
{
//CHECKPOINTS
checkCheckPoint(280,100,0);
checkCheckPoint(210,340,1);
checkCheckPoint(290,365,2);
checkCheckPoint(470,420,3);
checkCheckPoint(60,60,4);
checkCheckPoint(400,70,5);
setcolor(WHITE);
}
-
8/2/2019 Sem 4: CG Practical
49/57
///////////CHECK POINT FUNCTION////////////
void checkCheckPoint(int X, int Y, int count)
{
int flag=0,i;
if( ( x > X-5 && x < X+5 ) && ( y > Y-5 && y < Y+5 ) )
{
FLAG_ChkPnts[count] = 1;
setcolor(LIGHTGRAY);
outtextxy(410,25," TOWN IRRIGATED!");
}
if( FLAG_ChkPnts[count] == 0 )
{
setcolor(WHITE);
setfillstyle(SOLID_FILL,RED);
circle(X,Y,6);
floodfill(X,Y,WHITE);
}
else
{
setcolor(WHITE);setfillstyle(SOLID_FILL,BLUE);
circle(X,Y,6);
floodfill(X,Y,WHITE);
}
for(i=0; i
-
8/2/2019 Sem 4: CG Practical
50/57
//////////OBSTACLE FUNCTION////////////
void checkObstacle(int OXmin, int OYmin, int OXmax, int OYmax)
{
int OLDx=x;
int OLDy=y;
setcolor(WHITE);
rectangle(OXmin,OYmin,OXmax,OYmax);
OXmin-=size;
OYmin-=size;
OXmax+=size;
OYmax+=size;
if( (x>OXmin && xOYmin && y
-
8/2/2019 Sem 4: CG Practical
51/57
///////////////MAIN FUNCTION//////////////
void main()
{
int i;
char name;
int flag_CHEAT;
int OXmin,OXmax,OYmin,OYmax;
int gd=DETECT,gm;
FLAG_Ob=0;
COUNT_ChkPnts=6;
speed=9; size=5;
c=0; flag_CHEAT=0;
Xmin=30;
Xmax=490;
Ymin=50;
Ymax=430;
x=Xmin+speed;y=Ymax-speed;
for(i=0; i
-
8/2/2019 Sem 4: CG Practical
52/57
///////////////STATIC IMAGE DRAW////////////////
cleardevice();
//HEADING:
setcolor(WHITE);
outtextxy(210,15,"TOWN IRRIGATOR");
outtextxy(220,30,"Version 5.0");
//MESSAGE BOX:
setcolor(WHITE);
rectangle(400, 15, 620, 35);
//GAME BOUNDARY
setcolor(WHITE);
rectangle(Xmin-size,Ymin-size,Xmax+size,Ymax+size);
//COLORING INSIDE BOUNDARY
setfillstyle(XHATCH_FILL,GREEN);
floodfill(Xmin,Ymin,WHITE);
//NINJA HEADsetcolor(LIGHTGRAY);
circle(560,290,18);//x,y,z
ellipse(560,290,0,360,18,7);//x,y,0,360,z,z-10
//NINJA EYES
setfillstyle(SOLID_FILL, BLACK);
floodfill(548,287,LIGHTGRAY);
setcolor(DARKGRAY);
outtextxy(548,287,"V V");//NINJA FACE
//INSTRUCTIONS
setcolor(WHITE);
rectangle(500,45,630,340);outtextxy(504,50,"*INSTRUCTIONS*");
outtextxy(504,62,"===============");
outtextxy(504,80,"Use Arrow keys");
outtextxy(504,90,"to move the");
outtextxy(504,100,"object.");
outtextxy(504,120,"Avoid obstacles");
outtextxy(504,130,"Pass all the");
outtextxy(504,140,"towns &");
outtextxy(504,150,"reach the");
outtextxy(504,160,"finish.");
outtextxy(504,180,"You can hit an");
outtextxy(504,190,"obstacle 5");
outtextxy(504,200,"times.");
outtextxy(524,240,"GO AHEAD !");
rectangle(500,350,630,435);
outtextxy(520,360,"** TEAM **");
outtextxy(520,380,"Atul");
outtextxy(520,390,"Harshveer");
outtextxy(520,400,"Neil");
outtextxy(520,410,"Sidharth");
-
8/2/2019 Sem 4: CG Practical
53/57
map();
setcolor(WHITE);
checkObstacle(70,70,100,120);
setfillstyle(SOLID_FILL,LIGHTGREEN);
floodfill(71,71,WHITE);
checkObstacle(140,200,180,Ymax); floodfill(141,201,WHITE);
checkObstacle(300,45,330,410); floodfill(301,46,WHITE);
checkObstacle(200,350,280,380); floodfill(201,351,WHITE);
checkObstacle(200,100,250,150); floodfill(201,101,WHITE);
checkObstacle(360,380,390,434); floodfill(361,381,WHITE);
checkObstacle(420,350,489,380); floodfill(421,351,WHITE);
checkObstacle(380,300,420,330); floodfill(381,301,WHITE);
checkObstacle(330,200,450,230); floodfill(331,201,WHITE);
checkObstacle(420,45,450,170); floodfill(421,46,WHITE);
checkObstacle(350,100,420,130); floodfill(351,101,WHITE);
//STARTING PUDDLE OF WATER
setcolor(CYAN);
setfillstyle(SOLID_FILL,CYAN);
bar(Xmin-size+1,Ymax+size-1, Xmin+20, Ymax-20);
//ENDING PUDDLE OF WATER
setcolor(CYAN);
setfillstyle(SOLID_FILL,CYAN);
bar(Xmax+size-1,Ymin-size+1, Xmax-20, Ymin+20);
///////////////DYNAMIC IMAGE DRAW////////////////
do
{
//GAME BOUNDARY (AGAIN)
setcolor(WHITE);rectangle(Xmin-size,Ymin-size,Xmax+size,Ymax+size);
ch=getch();
setfillstyle(SOLID_FILL,CYAN);
setcolor(CYAN);
circle(x,y,size);
floodfill(x,y,CYAN);
-
8/2/2019 Sem 4: CG Practical
54/57
switch(ch)
{
case '~':
flag_CHEAT=1;
FLAG_ChkPntsALL=1;
break;
case RIGHT:
x=x+speed;
//NINJA EYES
setfillstyle(SOLID_FILL, BLACK);
floodfill(551,287,LIGHTGRAY);
setcolor(DARKGRAY);
outtextxy(551,287,"V V");//NINJA FACE
break;
case LEFT:
x=x-speed;
//NINJA EYES
setfillstyle(SOLID_FILL, BLACK);floodfill(547,287,LIGHTGRAY);
setcolor(DARKGRAY);
outtextxy(547,287,"V V");//NINJA FACE
break;
case UP:
y=y-speed;
//NINJA EYES
setfillstyle(SOLID_FILL, BLACK);
floodfill(549,286,LIGHTGRAY);
setcolor(DARKGRAY);
outtextxy(549,286,"V V");//NINJA FACE
break;
case DOWN:
y=y+speed;
//NINJA EYES
setfillstyle(SOLID_FILL, BLACK);
floodfill(549,288,LIGHTGRAY);
setcolor(DARKGRAY);
outtextxy(549,288,"V V");//NINJA FACE
break;
}
if(x < Xmin )
x+=speed;
else if(x > Xmax )
x-=speed;
if(y < Ymin )
y+=speed;
else if(y > Ymax )
y-=speed;
-
8/2/2019 Sem 4: CG Practical
55/57
//CLEARING MESSAGE BOX
setcolor(WHITE);
rectangle(400, 15, 620, 35);
setfillstyle(SOLID_FILL,BLACK);
floodfill(401,16,WHITE);
if(flag_CHEAT==1){
setcolor(RED);
outtextxy(538,25,")GOD MODE(");
}
map();
setcolor(WHITE);
if( flag_CHEAT != 1 )
{
checkObstacle(70,70,100,120);
checkObstacle(140,200,180,Ymax); //Small one above the one on the top - (3)
checkObstacle(300,45,330,410); //longest vertical rectangle - (4)
checkObstacle(200,350,280,380); //rectangle between 2 and 4 - (5)
checkObstacle(200,100,250,150); //rectangle on top left of 4 - (6)
checkObstacle(360,380,390,434); //Bottom right of the longestrectangle 4 - (7)
checkObstacle(420,350,489,380); //Corner most Bottom Right horizontal- (8)checkObstacle(380,300,420,330); //rectangle above 7 -(9)
checkObstacle(330,200,450,230); //rectangle attached to longest one right -(10)
checkObstacle(420,45,450,170); //vertical right near exit - (11)
checkObstacle(350,100,420,130); //rectangle b/w two vertical rectangles - (12)
}
setfillstyle(SOLID_FILL,YELLOW);
setcolor(YELLOW);
circle(x,y,size);
floodfill(x,y,YELLOW);
setcolor(BLUE);
outtextxy(x-3,y-3,"X");
//END CHECK:if( ( x > Xmax-20 && x < Xmax+20 ) && ( y > Ymin-20 && y < Ymin+20 ) )
{
//CLEARING MESSAGE BOX
setfillstyle(SOLID_FILL,BLACK);
floodfill(401,16,WHITE);
if(FLAG_ChkPntsALL==1)
{
setcolor(GREEN);
outtextxy(410,25," YOU WIN !! :) ");
sleep(2);
break;
}
else {
setcolor(LIGHTGRAY);
outtextxy(410,25," TOWNS LEFT !! :S ");
} } }
while(ch!=27 && FLAG_Ob != 1);
getch();
cleardevice();
closegraph();
}
-
8/2/2019 Sem 4: CG Practical
56/57
PROJECT
-
8/2/2019 Sem 4: CG Practical
57/57