SlideShare une entreprise Scribd logo
1  sur  57
Télécharger pour lire hors ligne
 
 
 
 
 
 
 
 
 
 
 
 
 
COMPUTER
GRAPHICS
LAB
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NEIL MATHEW – 4CS4 – Y3305 - A2324710002
NEIL MATHEW – 4CS4 – Y3305 - A2324710002
 
CONTENTS:
Sno:  Program  Sign 
1  WAP using 10 graphics functions.
 
2  WAP to draw line using DDA Algorithm
 
3 
WAP to draw scenery using 20 graphic
functions.
 
4  WAP to draw a line using Bresenham's Algo
 
5  WAP to draw a circle using Bresenham's Algo
 
6  WAP to draw a circle using Mid-Point Algo
 
7  WAP to draw an ellipse using Mid-Point Algo
 
8  WAP to perform Line Clipping
 
9  WAP to perform scaling of a triangle.
 
10  WAP to perform translation
 
11  WAP to perform Scaling w.r.t a Point.
 
12  WAP to perform rotation w.r.t to a point.
 
13  WAP to perform reflection w.r.t to a line
 
14  Project
 
 
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 <graphics.h>
#include <iostream.h>
#include <conio.h>
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<getmaxx(); i+=40)
{
for(j=0; j<=getmaxy(); j+=60)
{ circle(i,j,1); }
}
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);
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;
}
   
  PROGRAM 1 
 
Q2: Wap to draw a line using DDA Algorithm.    
/* Functions used:
1 putpixel()
2 outtextxy()
3 setbkcolor()
*/
#include <iostream.h> //for cout,cin
#include <graphics.h> //for putpixel()
#include <math.h> //for round()
#include<conio.h> //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;
 
x=x1; y=y1;
putpixel(x,y,15);
outtextxy(x-15,y-15,"START");
for(int i=1; i<=step; i++)
{
x+=xincrement;
y+=yincrement;
putpixel(x,y,15);
}
outtextxy(x+10,y+10,"END");
}
int main()
{
clrscr();
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
int x1,y1,x2,y2;
setbkcolor(DARKGRAY);
cout<<" Enter initial x and y coordinate: ";
cin>>x1>>y1;
cout<<"n Enter final x and y coordinate: ";
cin>>x2>>y2;
cout<<"n Press enter to draw line. ";
getch();
LINE(x1,y1,x2,y2);
getch();
closegraph();
return 2;
}
 
 
      
 
  PPROGRA
 
AM 2 
 
Q3: Wap to draw a scenery using 20 graphics functions.    
/* 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<graphics.h>
#include<conio.h>
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);
 
//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<=6, j<=getmaxx()*0.04; i++,j++)
{ //i for colour, j for width of each arc
setcolor(i);
arc(getmaxx()*0.5,getmaxy()*0.75,0,180,getmaxx()*0.40+j);
}
//RAIN
setlinestyle(3,0,2);
setcolor(BLUE);
line(getmaxx()*0.25+60,getmaxy()*0.25,getmaxx()*0.25+60,getmaxy()*0.75);
line(getmaxx()*0.25+50,getmaxy()*0.25,getmaxx()*0.25+50,getmaxy()*0.75);
line(getmaxx()*0.25+40,getmaxy()*0.25,getmaxx()*0.25+40,getmaxy()*0.75);
line(getmaxx()*0.25+30,getmaxy()*0.25,getmaxx()*0.25+30,getmaxy()*0.75);
line(getmaxx()*0.25+20,getmaxy()*0.25,getmaxx()*0.25+20,getmaxy()*0.75);
//CLOUD
setcolor(WHITE);
setfillstyle(HATCH_FILL,WHITE);
sector(getmaxx()*0.25,getmaxy()*0.25,0,360,30,30);
sector(getmaxx()*0.25+20,getmaxy()*0.25,0,360,30,30);
sector(getmaxx()*0.25+30,getmaxy()*0.25-30,0,360,30,30);
sector(getmaxx()*0.25+40,getmaxy()*0.25-10,0,360,30,30);
sector(getmaxx()*0.25+60,getmaxy()*0.25-20,0,360,30,30);
sector(getmaxx()*0.25+50,getmaxy()*0.25+10,0,360,30,30);
setlinestyle(0,0,1); //rectangles affected by this too.
 
//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();
}
PPROGRA
SCENER
AM 3
Y
 
Q4: WAP to draw a line using Bresenham's Algorithm   
#include<math.h>
#include<iostream.h>
#include<graphics.h>
#include <conio.h>
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);
   
 
//Other Pixels:
for(int i=1; i<=steps; i++)
{
while( P > 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);
cout<<"n Enter the Initial X Y Coordinate: ";
cin>>x1>>y1;
cout<<"n Enter the Final X Y Coordinate: ";
cin>>x2>>y2;
cout<<"n Press Enter to draw line.";
getch();
outtextxy(x1-10,y1-10, "START");
LINE(x1,y1,x2,y2);
outtextxy(x2+10,y2+10, "END");
getch();
closegraph();
}
PROGRA
 
 
 
AM 4 
 
 
Q5: WAP to draw a circle using Bresenham’s Algorithm.    
#include <graphics.h>
#include <math.h>
#include <iostream.h>
#include <conio.h>
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<y)
{
if(d<0)
{
d=d+4*x+6;
x+=1;
}
else
{
d=d+4*(x-y)+10;
x+=1;
y-=1;
}
putpixel(x+xc,y+yc,WHITE); otherSpoints(x,y,xc,yc);
} //end of while
} //end of function
 
void main()
{
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "C:/TC/BGI");
setbkcolor(DARKGRAY);
setcolor(WHITE);
int r;
cout<<"n Enter the radius: ";
cin>>r;
cout<<"n Press Enter to draw Circle.";
getch();
CIRCLE(r);
getch();
closegraph();
}
 PROGR
 
 
 
    
 
 
 
AM 5 
 
Q6: WAP to draw a circle using Mid-Point Algorithm.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
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:TCBGI");
int r;
int xc=getmaxx()/2;
int yc=getmaxy()/2;
cout<<"n Enter the radius: ";
cin>>r;
MidPontCircle(xc,yc,r);
getch();
closegraph();
}
PROGRAM 6
Q7: WAP to draw an elipse using Mid-Point Algorithm.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
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 <= 2*a*a*y )
{
if(P < 0)
{
DrawPoints(++x, y, xc, yc);
P=P+ 2*b*b*x + b*b;
}
else
{
DrawPoints(++x,--y, xc, yc);
P=P+ 2*b*b*x - 2*a*a*y + b*b;
}
}
float P2= b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
while( y >= 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;
}
}
}
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:TCBGI");
float a,b;
int xc=getmaxx()/2;
int yc=getmaxy()/2;
cout<<"n Enter Major axis (a) and Minor axis (b) : ";
cin>>a>>b;
Eclipse(xc,yc,a,b);
getch();
closegraph();
}
PROGRAM 7
Q8: WAP to perform line clipping.
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
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; i<n; i++)
line(Line[i][0],Line[i][1],Line[i][2],Line[i][3]);
setcolor(CYAN);
rectangle(ClipWindow[0][0],ClipWindow[0][1],ClipWindow[1][0],ClipWindo
w[1][1]);
outtextxy(10,10, "Press any key to continue");
getch(); closegraph();
}
void UnClipped_Input()
{
cout<<"n Enter the number of lines to draw : ";
cin>>n; cout<<endl;
for(int i=0; i<n; i++)
{
cout<<" LINE #"<<(i+1);
cout<<"n Enter the Initial position (x,y) : ";
cin>>Line[i][0];
cin>>Line[i][1];
cout<<" Enter the Final position (x,y) : ";
cin>>Line[i][2];
cin>>Line[i][3];
cout<<"n";
}
cout<<"n >> Enter the coordinates of the Clipping Window:";
cout<<"n >> Vertex A (x,y) : ";
cin>>ClipWindow[0][0];
cin>>ClipWindow[0][1];
cout<<" >> Vertex C (x,y) : ";
cin>>ClipWindow[1][0];
cin>>ClipWindow[1][1];
}
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;
}
c=5;
for(i=0; i<4; i++)
c=(long) c*10+digitC[i];
return c;
} //END of AND function
void Clip()
{
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];
long b1, b2, b1ANDb2;
int i,j,k;
int digit_b1, digit_b2;
double Slope;
for(i=0; i<n; i++)
{ //FOR EACH LINE:
b1=CheckBit(Line[i][0], Line[i][1]);
b2=CheckBit(Line[i][2], Line[i][3]);
b1ANDb2=AND(b1,b2);
if( b1ANDb2 != 50000 ) //OUTSIDE
{
//Remove OUTSIDE Line Coordinates from Array
for(int k=i; k<n-1; k++)
{
Line[k][0]=Line[k+1][0];
Line[k][1]=Line[k+1][1];
Line[k][2]=Line[k+1][2];
Line[k][3]=Line[k+1][3];
}
n--;
i--;
}
else //INSIDE
{
//Completely Inside
if( b1==50000 && b2==50000 )
{
//No Change to be made to Line Coordinates.
}
//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<=4; j++)
{
digit_b1=b1%10;
digit_b2=b2%10;
switch(j)
{
case 1:
if(digit_b1==1)
{
Line[i][0]= Line[i][0] + ( (Ymax - Line[i][1]) / Slope );
Line[i][1]=Ymax;
}
else if(digit_b2==1)
{
Line[i][2]= Line[i][2] + ( (Ymax - Line[i][3] ) / Slope );
Line[i][3]=Ymax;
}
break;
case 2:
if(digit_b1==1)
{
Line[i][0]= Line[i][0] + ( (Ymin - Line[i][1] ) / Slope );
Line[i][1]=Ymin;
}
else if(digit_b2==1)
{
Line[i][2]= Line[i][2] + ( (Ymin - Line[i][3] ) / Slope );
Line[i][3]=Ymin;
}
break;
case 3:
if(digit_b1==1)
{
Line[i][1]= Line[i][1] + ( ( Xmax - Line[i][0] ) * Slope) ;
Line[i][0]=Xmax;
}
else if(digit_b2==1)
{
Line[i][3]= Line[i][3] + ( (Xmax - Line[i][2] ) * Slope) ;
Line[i][2]=Xmax;
}
break;
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();
}
PROGRAM 8
Q9: WAP to perform scaling of a Triangle.
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
int n=4; //Variables for drawpoly() function
int p[8];
int d[8];
int Sx,Sy; //Scaling factors.
void Input()
{
cout<<"n Enter the scaling factor of x:";
cin>>Sx;
cout<<" Enter the scaling factor of y:";
cin>>Sy;
cout<<"n Using default triangle values n | A(0,-20), B(20,-20),
C(20, 20) |";
cout<<"nn Click to Continue... ";
getch();
cleardevice();
p[0]=0;
p[1]=-30;
p[2]=-30;
p[3]=+30;
p[4]=+30;
p[5]=+30;
p[6]=0;
p[7]=-30;
}
void Draw(int Xmid, int Ymid)
{
for(int i=0; i<8; i++)
{
if( i%2==0 )
d[i]=Xmid+p[i];
else
d[i]=Ymid+p[i];
}
drawpoly(n,d);
getch();
}
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 = XY1
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
XY1[i][j]=0;
for(k=0; k<3; k++)
{
XY1[i][j]+=xy1[i][k]*SI[k][j];
} } }
// Converting Final Matrix into Single Array
k=0;
for(i=0; i<3; i++)
{
for(j=0; j<2; j++) // Till 2 because '1' not needed.
{
p[k++]=XY1[i][j];
}
}
p[k++]=XY1[0][0];
p[k]=XY1[0][1];
}
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
Input();
setcolor(CYAN);
outtextxy(10,getmaxy()*0.25," BEFORE SCALING: ");
Draw(getmaxx()*0.25,getmaxy()*0.5 );
Scale();
setcolor(GREEN);
outtextxy(getmaxx()*0.5,getmaxy()*0.25," AFTER SCALING: ");
Draw(getmaxx()*0.75, getmaxy()*0.5);
closegraph();
}
PROGRAM 9
Q10: WAP to perform translation of a Triangle.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
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 For Polygon where No of edges (>3) = ";
cin>>n;
cout<<"n Enter the coordinates of "<<n<<" sided Polygon: n";
for(int i=0; i<n; i++)
{
cout<<" "<<i+1<<" Point (x,y) : ";
cin>>P[0][i]>>P[1][i];
P[2][i]=1;
}
cout<<"n Enter the Translation Factors:";
cout<<"n for X axis: "; cin>>tx;
cout<<" for Y axis: "; cin>>ty;
}
void Translate()
{
//Transaction Matrix: //1st - making the identity Matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
if(i==j) //DIAGONAL
T[i][j] = 1;
else
T[i][j] = 0;
}
} //2nd - making changes
T[0][2] = tx;
T[1][2] = ty;
// Matrix Multiplication
// T(3x3) x P(3xN)
for(i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
P2[i][j]=0;
for(int k=0; k<3; k++)
{
P2[i][j]+=T[i][k]*P[k][j];
}
}
}
}
void Display_Matrix(int p[3][10])
{
for(int i=0; i<3; i++)
{
cout<<"n |";
for(int j=0; j<n; j++)
cout<<" "<<p[i][j];
cout<<" |";
}
}
void DRAW(int p[3][10])
{
for(int i=0; i<n-1; i++)
{
line(p[0][i], p[1][i], p[0][i+1], p[1][i+1]);
}
line(p[0][n-1], p[1][n-1], p[0][0], p[1][0]);
}
void main()
{
INPUT();
Display_Matrix(P);
cout<<endl;
Translate();
for(int i=0; i<3; i++)
{
cout<<"n |";
for(int j=0; j<3; j++)
cout<<" "<<T[i][j];
cout<<" |";
}
cout<<endl;
Display_Matrix(P2);
cout<<"nn Press Enter to see Graphically? ";
getch();
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
setcolor(CYAN);
DRAW(P);
setcolor(GREEN);
DRAW(P2);
getch();
closegraph();
}
PROGRAM 10
Q11: WAP to perform scaling of a Triangle w.r.t a point.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
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 For Polygon where No of edges (>3) = ";
cin>>n;
cout<<"n Enter the coordinates of "<<n<<" sided Polygon: n";
for(int i=0; i<n; i++)
{
cout<<" "<<i+1<<" Point (x,y) : ";
cin>>P[0][i]>>P[1][i];
P[2][i]=1;
}
cout<<"n Enter the Scaling Factors:";
cout<<"n for X axis: "; cin>>Sx;
cout<<" for Y axis: "; cin>>Sy;
cout<<"n Scaling W.R.T which point (1/2/3...) : ";
cin>>ch;
h=P[0][ch-1];
k=P[1][ch-1];
}
void Display_Matrix(int p[3][10])
{
for(int i=0; i<3; i++)
{
cout<<"n |";
for(int j=0; j<n; j++)
cout<<" "<<p[i][j];
cout<<" |";
}
}
void DRAW(int p[3][10])
{
for(int i=0; i<n-1; i++)
{
line(p[0][i], p[1][i], p[0][i+1], p[1][i+1]);
}
line(p[0][n-1], p[1][n-1], p[0][0], p[1][0]);
}
void ScalingPoint()
{
//Scaling Matrix:
//1st - making the identity Matrix
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
if(i==j) //DIAGONAL
S[i][j] = 1;
else
S[i][j] = 0;
}
}//2nd - making changes
S[0][0] = Sx;
S[1][1] = Sy;
S[0][2] = -(h*Sx)+h;
S[1][2] = -(k*Sy)+k;
// Matrix Multiplication S(3x3) x P(3xN)
for(i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
P2[i][j]=0;
for(int k=0; k<3; k++)
{
P2[i][j]+=S[i][k]*P[k][j];
}
} } }
void main()
{
INPUT(); Display_Matrix(P);
cout<<endl;
ScalingPoint();
for(int i=0; i<3; i++)
{
cout<<"n |";
for(int j=0; j<3; j++)
cout<<" "<<S[i][j];
cout<<" |";
}
cout<<endl;
Display_Matrix(P2);
cout<<"nn Press Enter to see Graphically? ";
getch();
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
setcolor(CYAN);
DRAW(P);
setcolor(GREEN);
DRAW(P2);
getch();
closegraph();
}
PROGRAM 11
Q12: WAP to perform rotation of a Triangle
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
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 For Polygon where No of edges (>3) = ";
cin>>n;
cout<<"n Enter the coordinates of "<<n<<" sided Polygon: n";
for(int i=0; i<n; i++)
{
cout<<" "<<i+1<<" Point (x,y) : ";
cin>>P[0][i]>>P[1][i];
P[2][i]=1;
}
cout<<"n Enter the Angle: "; cin>>angle;
cout<<"n Scaling W.R.T which point (1/2/3...) : ";
cin>>ch;
if(ch!=0)
{
h=P[0][ch-1];
k=P[1][ch-1];
}
}
void Display_Matrix(double p[3][10])
{
for(int i=0; i<3; i++)
{
cout<<"n |";
for(int j=0; j<n; j++)
cout<<" "<<p[i][j];
cout<<" |";
}
}
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<3; i++)
{
for(int j=0; j<3; j++)
{
if(i==j) //DIAGONAL
S[i][j] = 1;
else
S[i][j] = 0;
}
}
//2nd - making changes
S[0][0] = cos ( deg2rad(x) );
S[0][1] = - sin( deg2rad(x) );
S[1][0] = sin ( deg2rad(x) );
S[1][1] = cos ( deg2rad(x) );
S[0][2] = -h*cos(deg2rad(x)) + k*sin ( deg2rad(x) ) + h;
S[1][2] = -h*sin(deg2rad(x)) - k*cos ( deg2rad(x) ) + k;
// Matrix Multiplication
// S(3x3) x P(3xN)
for(i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
P2[i][j]=0;
for(int k=0; k<3; k++)
{
P2[i][j]+=S[i][k]*P[k][j];
}}}
}
void DRAW(double p[3][10])
{
for(int i=0; i<n-1; i++)
{
line(getmaxx()*0.5+p[0][i], getmaxy()*0.5+p[1][i],
getmaxx()*0.5+p[0][i+1], getmaxy()*0.5+p[1][i+1]);
}
line(getmaxx()*0.5+p[0][n-1], getmaxy()*0.5+p[1][n-1],
getmaxx()*0.5+p[0][0], getmaxy()*0.5+p[1][0]);
}
void main()
{
INPUT();
Display_Matrix(P);
cout<<endl;
ROTATEpoint();
for(int i=0; i<3; i++)
{
cout<<"n |";
for(int j=0; j<3; j++)
cout<<" "<<S[i][j];
cout<<" |";
}
cout<<endl;
Display_Matrix(P2);
cout<<"nn Press Enter to see Graphically? ";
getch();
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
setcolor(DARKGRAY);
//X AXIS
line(getmaxx()*0.5, 0, getmaxx()*0.5, getmaxy());
//Y AXIS
line(0, getmaxy()*0.5, getmaxx(), getmaxy()*0.5);
setcolor(CYAN);
DRAW(P);
setcolor(GREEN);
DRAW(P2);
getch();
closegraph();
}
(without fixed point)
(with fixed point)
PROGRAM 12
Q13: WAP to perform reflection of a Triangle about a line
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdio.h>
int n;
double P[3][10];
double P2[3][10];
double S[3][3];
float m; int b;
void INPUT()
{
int ch;
cout<<"n For Polygon where No of edges (>=3) = ";
cin>>n;
cout<<"n Enter the coordinates of "<<n<<" sided Polygon: n";
for(int i=0; i<n; i++)
{
cout<<" "<<i+1<<" Point (x,y) : ";
cin>>P[0][i]>>P[1][i];
P[2][i]=1;
}
cout<<"n Enter the equation of line ( Y = m X + b) : ";
char ch1;
cin>>ch1; cin>>ch1; cin>>m; cin>>ch1; cin>>ch1; cin>>b;
}
void Display_Matrix(double p[3][10])
{
for(int i=0; i<3; i++)
{
cout<<"n |";
for(int j=0; j<n; j++)
cout<<" "<<p[i][j];
cout<<" |";
}
}
void DRAW(double p[3][10])
{
for(int i=0; i<n-1; i++)
{
line(getmaxx()*0.5+p[0][i], getmaxy()*0.5+p[1][i],
getmaxx()*0.5+p[0][i+1], getmaxy()*0.5+p[1][i+1]);
}
line(getmaxx()*0.5+p[0][n-1], getmaxy()*0.5+p[1][n-1],
getmaxx()*0.5+p[0][0], getmaxy()*0.5+p[1][0]);
}
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<3; i++)
{
for(int j=0; j<3; j++)
{
if(i==j) //DIAGONAL
S[i][j] = 1;
else
S[i][j] = 0;
}
}
//2nd - making changes
S[0][0] = (1-m*m) / (m*m+1) ;
S[0][1] = (2*m) / (m*m+1) ;
S[0][2] = (-2*m*b) / (m*m+1) ;
S[1][0] = (2*m) / (m*m+1) ;
S[1][1] = (m*m-1) / (m*m+1) ;
S[1][2] = (2*b) / (m*m+1);
// Matrix Multiplication
// S(3x3) x P(3xN)
for(i=0; i<3; i++)
{
for(int j=0; j<n; j++)
{
P2[i][j]=0;
for(int k=0; k<3; k++)
{
P2[i][j]+=S[i][k]*P[k][j];
}
}
}
}
void main()
{
INPUT();
Display_Matrix(P);
cout<<endl;
ReflectPoint();
for(int i=0; i<3; i++)
{
cout<<"n |";
for(int j=0; j<3; j++)
cout<<" "<<S[i][j];
cout<<" |";
}
cout<<endl;
Display_Matrix(P2);
cout<<"nn Press Enter to see Graphically? ";
getch();
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
setcolor(DARKGRAY);
//X AXIS
line(getmaxx()*0.5, 0, getmaxx()*0.5, getmaxy());
//Y AXIS
line(0, getmaxy()*0.5, getmaxx(), getmaxy()*0.5);
setcolor(CYAN);
DRAW(P);
setcolor(GREEN);
DRAW(P2);
getch();
closegraph();
}
PROGRAM 13
 
PROJECT:  
TOWN	IRRIGATOR	(GAME)	
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#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);
}
 
///////////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<COUNT_ChkPnts; i++)
{
if( FLAG_ChkPnts[i] !=1 )
{ flag=0; break; }
flag=1;
}
if(flag==1)
{
//CLEARING MESSAGE BOX
setfillstyle(SOLID_FILL,BLACK);
floodfill(410,25,WHITE);
setcolor(GREEN);
outtextxy(430,25, "ALL TOWNS IRRIGATED!");
FLAG_ChkPntsALL=1;
}
}
 
//////////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 && x<OXmax) && (y>OYmin && y<OYmax) ) {
if( ch==RIGHT )
{ x-=speed; }
else if(ch==LEFT)
{ x+=speed; }
if( ch==UP )
{ y+=speed; }
else if(ch==DOWN)
{ y-=speed; }
if( OLDx == x || OLDy == y )
{
c++;
setcolor(LIGHTGRAY);
if(c!=5 && FLAG_ChkPntsALL!=1)
{
//CLEARING MESSAGE BOX
setfillstyle(SOLID_FILL,BLACK);
floodfill(401,16,WHITE);
outtextxy(410,25," YOU HIT ROUGH LAND!"); }}}
if(FLAG_ChkPntsALL!=1)
{
setcolor(RED);
if(c==1)
outtextxy( 570 ,25,"X");
else if(c==2)
if(c==2)
outtextxy( 570 ,25,"XX");
if(c==3)
outtextxy( 570 ,25,"XXX");
if(c==4)
outtextxy( 570 ,25,"XXXX");
if(c==5)
{
setcolor(RED);
outtextxy(410,25,"YOU LOSE BAD!");
outtextxy( 570 ,25,"XXXXX");
FLAG_Ob=1;
sleep(1); } }}
 
///////////////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<COUNT_ChkPnts; i++)
FLAG_ChkPnts[i]=0;
initgraph(&gd,&gm,"C:/TC/BGI");
/////////////////LOADER SCREEN////////////////
rectangle(50,90,600,300);
outtextxy((getmaxx()*0.5)-100, (getmaxy()*0.5)-130,
"**** TOWN IRRIGATOR ****");
outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-80,
"The Towns in the district have been hit by a severe drought.");
outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-65,
"Due to the scarcity of Water, the population is suffering ! ");
outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-50,
"You can be a HERO, by supplying water to these towns.");
outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-35,
"Irrigate the towns and reach the finish without");
outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-20,
"colliding with the obstacles.");
outtextxy((getmaxx()*0.5)-100, (getmaxy()*0.5)+30,
"Press Any Key when ready...");
getch();
outtextxy((getmaxx()*0.5)-80, (getmaxy()*0.5)+90,
"Loading Map... :)");
sleep(1);
outtextxy((getmaxx()*0.5)-90, (getmaxy()*0.5)+105,
"Building Towns... :)");
sleep(1);
outtextxy((getmaxx()*0.5)-120, (getmaxy()*0.5)+120,
"Cleaning up the Mess... :P");
sleep(1);
delay(10);
 
///////////////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 HEAD
setcolor(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");
 
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);
 
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;
 
//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 longest rectangle 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();
}
PROJECT
Computer Graphics Lab

Contenu connexe

Tendances

Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
Amit Kapoor
 
Computer Graphics Practical
Computer Graphics PracticalComputer Graphics Practical
Computer Graphics Practical
Neha Sharma
 

Tendances (20)

Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
 
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
 
Computer graphics
Computer graphics Computer graphics
Computer graphics
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
 
Computer graphics practical(jainam)
Computer graphics practical(jainam)Computer graphics practical(jainam)
Computer graphics practical(jainam)
 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics lab
 
Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)Cg lab cse-v (1) (1)
Cg lab cse-v (1) (1)
 
Introduction to graphics programming in c
Introduction to graphics programming in cIntroduction to graphics programming in c
Introduction to graphics programming in c
 
Lecture on graphics
Lecture on graphicsLecture on graphics
Lecture on graphics
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
 
662305 10
662305 10662305 10
662305 10
 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics Functions
 
C graphics programs file
C graphics programs fileC graphics programs file
C graphics programs file
 
Arrays
ArraysArrays
Arrays
 
Computer Graphics Practical
Computer Graphics PracticalComputer Graphics Practical
Computer Graphics Practical
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 

Similaire à Computer Graphics Lab

Computer graphics
Computer graphicsComputer graphics
Computer graphics
amitsarda3
 
Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilder
Eduardo Lundgren
 
Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]
Palak Sanghani
 
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfbfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
shehabhamad_90
 
2D & 3D Modelling with Mathematica
2D & 3D Modelling with Mathematica2D & 3D Modelling with Mathematica
2D & 3D Modelling with Mathematica
Miles Ford
 
CA301_CG_Filled Area Primitives-New.pptx
CA301_CG_Filled Area Primitives-New.pptxCA301_CG_Filled Area Primitives-New.pptx
CA301_CG_Filled Area Primitives-New.pptx
KaushikiJha3
 

Similaire à Computer Graphics Lab (20)

Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Copy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with QtCopy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with Qt
 
Graphic Design Lab File.docx
Graphic Design Lab File.docxGraphic Design Lab File.docx
Graphic Design Lab File.docx
 
Dsp manual
Dsp manualDsp manual
Dsp manual
 
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
 
Creating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdfCreating an Uber Clone - Part IV - Transcript.pdf
Creating an Uber Clone - Part IV - Transcript.pdf
 
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
 
graphics programming in java
graphics programming in javagraphics programming in java
graphics programming in java
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Unit 2 notes
Unit 2 notesUnit 2 notes
Unit 2 notes
 
3.pdf
3.pdf3.pdf
3.pdf
 
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
 
Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilder
 
Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]
 
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfbfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
 
2D & 3D Modelling with Mathematica
2D & 3D Modelling with Mathematica2D & 3D Modelling with Mathematica
2D & 3D Modelling with Mathematica
 
CA301_CG_Filled Area Primitives-New.pptx
CA301_CG_Filled Area Primitives-New.pptxCA301_CG_Filled Area Primitives-New.pptx
CA301_CG_Filled Area Primitives-New.pptx
 
Output primitives computer graphics c version
Output primitives   computer graphics c versionOutput primitives   computer graphics c version
Output primitives computer graphics c version
 
Building a Visualization Language
Building a Visualization LanguageBuilding a Visualization Language
Building a Visualization Language
 

Plus de Neil Mathew

Plus de Neil Mathew (20)

AMIZONER: Weekly Progress Reports
AMIZONER: Weekly Progress ReportsAMIZONER: Weekly Progress Reports
AMIZONER: Weekly Progress Reports
 
AMIZONER: Final Report
AMIZONER: Final ReportAMIZONER: Final Report
AMIZONER: Final Report
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
 
Role of IT & Computer in Environment
Role of IT & Computer in EnvironmentRole of IT & Computer in Environment
Role of IT & Computer in Environment
 
French Open
French OpenFrench Open
French Open
 
Data Structures Lab
Data Structures LabData Structures Lab
Data Structures Lab
 
DBMS Lab
DBMS LabDBMS Lab
DBMS Lab
 
Communication Assessment File
Communication Assessment FileCommunication Assessment File
Communication Assessment File
 
Cloud Computing and Google's advancement in the field
Cloud Computing and Google's advancement in the fieldCloud Computing and Google's advancement in the field
Cloud Computing and Google's advancement in the field
 
Unix Programming Lab
Unix Programming LabUnix Programming Lab
Unix Programming Lab
 
The Cube - Class XII Project
The Cube - Class XII ProjectThe Cube - Class XII Project
The Cube - Class XII Project
 
Copywriting & Storyboarding (Advertising & Sales Promotion)
Copywriting & Storyboarding (Advertising & Sales Promotion)Copywriting & Storyboarding (Advertising & Sales Promotion)
Copywriting & Storyboarding (Advertising & Sales Promotion)
 
American Airlines Merger (Management In Action Case Study)
American Airlines Merger (Management In Action Case Study)American Airlines Merger (Management In Action Case Study)
American Airlines Merger (Management In Action Case Study)
 
Good Governance Leads To Flourish Society & Nation (BS SAP)
Good Governance Leads To Flourish Society & Nation (BS SAP)Good Governance Leads To Flourish Society & Nation (BS SAP)
Good Governance Leads To Flourish Society & Nation (BS SAP)
 
High Fortune (Product Brand Management)
High Fortune (Product Brand Management)High Fortune (Product Brand Management)
High Fortune (Product Brand Management)
 
Cost Benefit Analysis in Public Project Appraisal (PPAC)
Cost Benefit Analysis in Public Project Appraisal (PPAC)Cost Benefit Analysis in Public Project Appraisal (PPAC)
Cost Benefit Analysis in Public Project Appraisal (PPAC)
 
Corportate Entrepreneurship at WIPRO
Corportate Entrepreneurship at WIPROCorportate Entrepreneurship at WIPRO
Corportate Entrepreneurship at WIPRO
 
Consumer Perception of Job Seekers in updating their Job Profiles on Job Portals
Consumer Perception of Job Seekers in updating their Job Profiles on Job PortalsConsumer Perception of Job Seekers in updating their Job Profiles on Job Portals
Consumer Perception of Job Seekers in updating their Job Profiles on Job Portals
 
How OLX changed the consumer involvement in the Scrap Market
How OLX changed the consumer involvement in the Scrap MarketHow OLX changed the consumer involvement in the Scrap Market
How OLX changed the consumer involvement in the Scrap Market
 
Innovation Jockeys 3 - Gramseva: Kisan
Innovation Jockeys 3 - Gramseva: KisanInnovation Jockeys 3 - Gramseva: Kisan
Innovation Jockeys 3 - Gramseva: Kisan
 

Dernier

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Dernier (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

Computer Graphics Lab

  • 2. NEIL MATHEW – 4CS4 – Y3305 - A2324710002   CONTENTS: Sno:  Program  Sign  1  WAP using 10 graphics functions.   2  WAP to draw line using DDA Algorithm   3  WAP to draw scenery using 20 graphic functions.   4  WAP to draw a line using Bresenham's Algo   5  WAP to draw a circle using Bresenham's Algo   6  WAP to draw a circle using Mid-Point Algo   7  WAP to draw an ellipse using Mid-Point Algo   8  WAP to perform Line Clipping   9  WAP to perform scaling of a triangle.   10  WAP to perform translation   11  WAP to perform Scaling w.r.t a Point.   12  WAP to perform rotation w.r.t to a point.   13  WAP to perform reflection w.r.t to a line   14  Project    
  • 3. 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 <graphics.h> #include <iostream.h> #include <conio.h> 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<getmaxx(); i+=40) { for(j=0; j<=getmaxy(); j+=60) { circle(i,j,1); } }
  • 4. 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);
  • 5. 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; }
  • 7.   Q2: Wap to draw a line using DDA Algorithm.     /* Functions used: 1 putpixel() 2 outtextxy() 3 setbkcolor() */ #include <iostream.h> //for cout,cin #include <graphics.h> //for putpixel() #include <math.h> //for round() #include<conio.h> //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.   x=x1; y=y1; putpixel(x,y,15); outtextxy(x-15,y-15,"START"); for(int i=1; i<=step; i++) { x+=xincrement; y+=yincrement; putpixel(x,y,15); } outtextxy(x+10,y+10,"END"); } int main() { clrscr(); int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:/TC/BGI"); int x1,y1,x2,y2; setbkcolor(DARKGRAY); cout<<" Enter initial x and y coordinate: "; cin>>x1>>y1; cout<<"n Enter final x and y coordinate: "; cin>>x2>>y2; cout<<"n Press enter to draw line. "; getch(); LINE(x1,y1,x2,y2); getch(); closegraph(); return 2; }
  • 10.   Q3: Wap to draw a scenery using 20 graphics functions.     /* 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<graphics.h> #include<conio.h> 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);
  • 11.   //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<=6, j<=getmaxx()*0.04; i++,j++) { //i for colour, j for width of each arc setcolor(i); arc(getmaxx()*0.5,getmaxy()*0.75,0,180,getmaxx()*0.40+j); } //RAIN setlinestyle(3,0,2); setcolor(BLUE); line(getmaxx()*0.25+60,getmaxy()*0.25,getmaxx()*0.25+60,getmaxy()*0.75); line(getmaxx()*0.25+50,getmaxy()*0.25,getmaxx()*0.25+50,getmaxy()*0.75); line(getmaxx()*0.25+40,getmaxy()*0.25,getmaxx()*0.25+40,getmaxy()*0.75); line(getmaxx()*0.25+30,getmaxy()*0.25,getmaxx()*0.25+30,getmaxy()*0.75); line(getmaxx()*0.25+20,getmaxy()*0.25,getmaxx()*0.25+20,getmaxy()*0.75); //CLOUD setcolor(WHITE); setfillstyle(HATCH_FILL,WHITE); sector(getmaxx()*0.25,getmaxy()*0.25,0,360,30,30); sector(getmaxx()*0.25+20,getmaxy()*0.25,0,360,30,30); sector(getmaxx()*0.25+30,getmaxy()*0.25-30,0,360,30,30); sector(getmaxx()*0.25+40,getmaxy()*0.25-10,0,360,30,30); sector(getmaxx()*0.25+60,getmaxy()*0.25-20,0,360,30,30); sector(getmaxx()*0.25+50,getmaxy()*0.25+10,0,360,30,30); setlinestyle(0,0,1); //rectangles affected by this too.
  • 12.   //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(); }
  • 14.   Q4: WAP to draw a line using Bresenham's Algorithm    #include<math.h> #include<iostream.h> #include<graphics.h> #include <conio.h> 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);    
  • 15.   //Other Pixels: for(int i=1; i<=steps; i++) { while( P > 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); cout<<"n Enter the Initial X Y Coordinate: "; cin>>x1>>y1; cout<<"n Enter the Final X Y Coordinate: "; cin>>x2>>y2; cout<<"n Press Enter to draw line."; getch(); outtextxy(x1-10,y1-10, "START"); LINE(x1,y1,x2,y2); outtextxy(x2+10,y2+10, "END"); getch(); closegraph(); }
  • 17.   Q5: WAP to draw a circle using Bresenham’s Algorithm.     #include <graphics.h> #include <math.h> #include <iostream.h> #include <conio.h> 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<y) { if(d<0) { d=d+4*x+6; x+=1; } else { d=d+4*(x-y)+10; x+=1; y-=1; } putpixel(x+xc,y+yc,WHITE); otherSpoints(x,y,xc,yc); } //end of while } //end of function
  • 18.   void main() { int gdriver=DETECT, gmode; initgraph(&gdriver, &gmode, "C:/TC/BGI"); setbkcolor(DARKGRAY); setcolor(WHITE); int r; cout<<"n Enter the radius: "; cin>>r; cout<<"n Press Enter to draw Circle."; getch(); CIRCLE(r); getch(); closegraph(); }
  • 20. Q6: WAP to draw a circle using Mid-Point Algorithm. #include<iostream.h> #include<conio.h> #include<graphics.h> 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:TCBGI"); int r; int xc=getmaxx()/2; int yc=getmaxy()/2; cout<<"n Enter the radius: "; cin>>r; MidPontCircle(xc,yc,r); getch(); closegraph(); }
  • 22. Q7: WAP to draw an elipse using Mid-Point Algorithm. #include<iostream.h> #include<conio.h> #include<graphics.h> 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 <= 2*a*a*y ) { if(P < 0) { DrawPoints(++x, y, xc, yc); P=P+ 2*b*b*x + b*b; } else { DrawPoints(++x,--y, xc, yc); P=P+ 2*b*b*x - 2*a*a*y + b*b; } } float P2= b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; while( y >= 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; } } }
  • 23. void main() { int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:TCBGI"); float a,b; int xc=getmaxx()/2; int yc=getmaxy()/2; cout<<"n Enter Major axis (a) and Minor axis (b) : "; cin>>a>>b; Eclipse(xc,yc,a,b); getch(); closegraph(); }
  • 25. Q8: WAP to perform line clipping. #include<graphics.h> #include<iostream.h> #include<conio.h> 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; i<n; i++) line(Line[i][0],Line[i][1],Line[i][2],Line[i][3]); setcolor(CYAN); rectangle(ClipWindow[0][0],ClipWindow[0][1],ClipWindow[1][0],ClipWindo w[1][1]); outtextxy(10,10, "Press any key to continue"); getch(); closegraph(); } void UnClipped_Input() { cout<<"n Enter the number of lines to draw : "; cin>>n; cout<<endl; for(int i=0; i<n; i++) { cout<<" LINE #"<<(i+1); cout<<"n Enter the Initial position (x,y) : "; cin>>Line[i][0]; cin>>Line[i][1]; cout<<" Enter the Final position (x,y) : "; cin>>Line[i][2]; cin>>Line[i][3]; cout<<"n"; } cout<<"n >> Enter the coordinates of the Clipping Window:"; cout<<"n >> Vertex A (x,y) : "; cin>>ClipWindow[0][0]; cin>>ClipWindow[0][1]; cout<<" >> Vertex C (x,y) : "; cin>>ClipWindow[1][0]; cin>>ClipWindow[1][1]; }
  • 26. 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; }
  • 27. c=5; for(i=0; i<4; i++) c=(long) c*10+digitC[i]; return c; } //END of AND function void Clip() { 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]; long b1, b2, b1ANDb2; int i,j,k; int digit_b1, digit_b2; double Slope; for(i=0; i<n; i++) { //FOR EACH LINE: b1=CheckBit(Line[i][0], Line[i][1]); b2=CheckBit(Line[i][2], Line[i][3]); b1ANDb2=AND(b1,b2); if( b1ANDb2 != 50000 ) //OUTSIDE { //Remove OUTSIDE Line Coordinates from Array for(int k=i; k<n-1; k++) { Line[k][0]=Line[k+1][0]; Line[k][1]=Line[k+1][1]; Line[k][2]=Line[k+1][2]; Line[k][3]=Line[k+1][3]; } n--; i--; } else //INSIDE { //Completely Inside if( b1==50000 && b2==50000 ) { //No Change to be made to Line Coordinates. }
  • 28. //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<=4; j++) { digit_b1=b1%10; digit_b2=b2%10; switch(j) { case 1: if(digit_b1==1) { Line[i][0]= Line[i][0] + ( (Ymax - Line[i][1]) / Slope ); Line[i][1]=Ymax; } else if(digit_b2==1) { Line[i][2]= Line[i][2] + ( (Ymax - Line[i][3] ) / Slope ); Line[i][3]=Ymax; } break; case 2: if(digit_b1==1) { Line[i][0]= Line[i][0] + ( (Ymin - Line[i][1] ) / Slope ); Line[i][1]=Ymin; } else if(digit_b2==1) { Line[i][2]= Line[i][2] + ( (Ymin - Line[i][3] ) / Slope ); Line[i][3]=Ymin; } break; case 3: if(digit_b1==1) { Line[i][1]= Line[i][1] + ( ( Xmax - Line[i][0] ) * Slope) ; Line[i][0]=Xmax; } else if(digit_b2==1) { Line[i][3]= Line[i][3] + ( (Xmax - Line[i][2] ) * Slope) ; Line[i][2]=Xmax; } break;
  • 29. 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(); }
  • 31. Q9: WAP to perform scaling of a Triangle. #include<graphics.h> #include<iostream.h> #include<conio.h> int n=4; //Variables for drawpoly() function int p[8]; int d[8]; int Sx,Sy; //Scaling factors. void Input() { cout<<"n Enter the scaling factor of x:"; cin>>Sx; cout<<" Enter the scaling factor of y:"; cin>>Sy; cout<<"n Using default triangle values n | A(0,-20), B(20,-20), C(20, 20) |"; cout<<"nn Click to Continue... "; getch(); cleardevice(); p[0]=0; p[1]=-30; p[2]=-30; p[3]=+30; p[4]=+30; p[5]=+30; p[6]=0; p[7]=-30; } void Draw(int Xmid, int Ymid) { for(int i=0; i<8; i++) { if( i%2==0 ) d[i]=Xmid+p[i]; else d[i]=Ymid+p[i]; } drawpoly(n,d); getch(); }
  • 32. 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 = XY1 for(i=0; i<3; i++) { for(j=0; j<3; j++) { XY1[i][j]=0; for(k=0; k<3; k++) { XY1[i][j]+=xy1[i][k]*SI[k][j]; } } } // Converting Final Matrix into Single Array k=0; for(i=0; i<3; i++) { for(j=0; j<2; j++) // Till 2 because '1' not needed. { p[k++]=XY1[i][j]; } } p[k++]=XY1[0][0]; p[k]=XY1[0][1]; } void main() { int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:/TC/BGI"); Input(); setcolor(CYAN); outtextxy(10,getmaxy()*0.25," BEFORE SCALING: "); Draw(getmaxx()*0.25,getmaxy()*0.5 ); Scale(); setcolor(GREEN); outtextxy(getmaxx()*0.5,getmaxy()*0.25," AFTER SCALING: "); Draw(getmaxx()*0.75, getmaxy()*0.5); closegraph(); }
  • 34. Q10: WAP to perform translation of a Triangle. #include<iostream.h> #include<conio.h> #include<graphics.h> 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 For Polygon where No of edges (>3) = "; cin>>n; cout<<"n Enter the coordinates of "<<n<<" sided Polygon: n"; for(int i=0; i<n; i++) { cout<<" "<<i+1<<" Point (x,y) : "; cin>>P[0][i]>>P[1][i]; P[2][i]=1; } cout<<"n Enter the Translation Factors:"; cout<<"n for X axis: "; cin>>tx; cout<<" for Y axis: "; cin>>ty; } void Translate() { //Transaction Matrix: //1st - making the identity Matrix for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(i==j) //DIAGONAL T[i][j] = 1; else T[i][j] = 0; } } //2nd - making changes T[0][2] = tx; T[1][2] = ty; // Matrix Multiplication // T(3x3) x P(3xN) for(i=0; i<3; i++) { for(int j=0; j<n; j++) { P2[i][j]=0; for(int k=0; k<3; k++) { P2[i][j]+=T[i][k]*P[k][j]; } } } }
  • 35. void Display_Matrix(int p[3][10]) { for(int i=0; i<3; i++) { cout<<"n |"; for(int j=0; j<n; j++) cout<<" "<<p[i][j]; cout<<" |"; } } void DRAW(int p[3][10]) { for(int i=0; i<n-1; i++) { line(p[0][i], p[1][i], p[0][i+1], p[1][i+1]); } line(p[0][n-1], p[1][n-1], p[0][0], p[1][0]); } void main() { INPUT(); Display_Matrix(P); cout<<endl; Translate(); for(int i=0; i<3; i++) { cout<<"n |"; for(int j=0; j<3; j++) cout<<" "<<T[i][j]; cout<<" |"; } cout<<endl; Display_Matrix(P2); cout<<"nn Press Enter to see Graphically? "; getch(); int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:/TC/BGI"); setcolor(CYAN); DRAW(P); setcolor(GREEN); DRAW(P2); getch(); closegraph(); }
  • 37. Q11: WAP to perform scaling of a Triangle w.r.t a point. #include<iostream.h> #include<conio.h> #include<graphics.h> 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 For Polygon where No of edges (>3) = "; cin>>n; cout<<"n Enter the coordinates of "<<n<<" sided Polygon: n"; for(int i=0; i<n; i++) { cout<<" "<<i+1<<" Point (x,y) : "; cin>>P[0][i]>>P[1][i]; P[2][i]=1; } cout<<"n Enter the Scaling Factors:"; cout<<"n for X axis: "; cin>>Sx; cout<<" for Y axis: "; cin>>Sy; cout<<"n Scaling W.R.T which point (1/2/3...) : "; cin>>ch; h=P[0][ch-1]; k=P[1][ch-1]; } void Display_Matrix(int p[3][10]) { for(int i=0; i<3; i++) { cout<<"n |"; for(int j=0; j<n; j++) cout<<" "<<p[i][j]; cout<<" |"; } } void DRAW(int p[3][10]) { for(int i=0; i<n-1; i++) { line(p[0][i], p[1][i], p[0][i+1], p[1][i+1]); } line(p[0][n-1], p[1][n-1], p[0][0], p[1][0]); }
  • 38. void ScalingPoint() { //Scaling Matrix: //1st - making the identity Matrix for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(i==j) //DIAGONAL S[i][j] = 1; else S[i][j] = 0; } }//2nd - making changes S[0][0] = Sx; S[1][1] = Sy; S[0][2] = -(h*Sx)+h; S[1][2] = -(k*Sy)+k; // Matrix Multiplication S(3x3) x P(3xN) for(i=0; i<3; i++) { for(int j=0; j<n; j++) { P2[i][j]=0; for(int k=0; k<3; k++) { P2[i][j]+=S[i][k]*P[k][j]; } } } } void main() { INPUT(); Display_Matrix(P); cout<<endl; ScalingPoint(); for(int i=0; i<3; i++) { cout<<"n |"; for(int j=0; j<3; j++) cout<<" "<<S[i][j]; cout<<" |"; } cout<<endl; Display_Matrix(P2); cout<<"nn Press Enter to see Graphically? "; getch(); int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:/TC/BGI"); setcolor(CYAN); DRAW(P); setcolor(GREEN); DRAW(P2); getch(); closegraph(); }
  • 40. Q12: WAP to perform rotation of a Triangle #include<iostream.h> #include<conio.h> #include<graphics.h> #include<math.h> 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 For Polygon where No of edges (>3) = "; cin>>n; cout<<"n Enter the coordinates of "<<n<<" sided Polygon: n"; for(int i=0; i<n; i++) { cout<<" "<<i+1<<" Point (x,y) : "; cin>>P[0][i]>>P[1][i]; P[2][i]=1; } cout<<"n Enter the Angle: "; cin>>angle; cout<<"n Scaling W.R.T which point (1/2/3...) : "; cin>>ch; if(ch!=0) { h=P[0][ch-1]; k=P[1][ch-1]; } } void Display_Matrix(double p[3][10]) { for(int i=0; i<3; i++) { cout<<"n |"; for(int j=0; j<n; j++) cout<<" "<<p[i][j]; cout<<" |"; } }
  • 41. 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<3; i++) { for(int j=0; j<3; j++) { if(i==j) //DIAGONAL S[i][j] = 1; else S[i][j] = 0; } } //2nd - making changes S[0][0] = cos ( deg2rad(x) ); S[0][1] = - sin( deg2rad(x) ); S[1][0] = sin ( deg2rad(x) ); S[1][1] = cos ( deg2rad(x) ); S[0][2] = -h*cos(deg2rad(x)) + k*sin ( deg2rad(x) ) + h; S[1][2] = -h*sin(deg2rad(x)) - k*cos ( deg2rad(x) ) + k; // Matrix Multiplication // S(3x3) x P(3xN) for(i=0; i<3; i++) { for(int j=0; j<n; j++) { P2[i][j]=0; for(int k=0; k<3; k++) { P2[i][j]+=S[i][k]*P[k][j]; }}} } void DRAW(double p[3][10]) { for(int i=0; i<n-1; i++) { line(getmaxx()*0.5+p[0][i], getmaxy()*0.5+p[1][i], getmaxx()*0.5+p[0][i+1], getmaxy()*0.5+p[1][i+1]); } line(getmaxx()*0.5+p[0][n-1], getmaxy()*0.5+p[1][n-1], getmaxx()*0.5+p[0][0], getmaxy()*0.5+p[1][0]); }
  • 42. void main() { INPUT(); Display_Matrix(P); cout<<endl; ROTATEpoint(); for(int i=0; i<3; i++) { cout<<"n |"; for(int j=0; j<3; j++) cout<<" "<<S[i][j]; cout<<" |"; } cout<<endl; Display_Matrix(P2); cout<<"nn Press Enter to see Graphically? "; getch(); int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:/TC/BGI"); setcolor(DARKGRAY); //X AXIS line(getmaxx()*0.5, 0, getmaxx()*0.5, getmaxy()); //Y AXIS line(0, getmaxy()*0.5, getmaxx(), getmaxy()*0.5); setcolor(CYAN); DRAW(P); setcolor(GREEN); DRAW(P2); getch(); closegraph(); }
  • 43. (without fixed point) (with fixed point) PROGRAM 12
  • 44. Q13: WAP to perform reflection of a Triangle about a line #include<iostream.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<stdio.h> int n; double P[3][10]; double P2[3][10]; double S[3][3]; float m; int b; void INPUT() { int ch; cout<<"n For Polygon where No of edges (>=3) = "; cin>>n; cout<<"n Enter the coordinates of "<<n<<" sided Polygon: n"; for(int i=0; i<n; i++) { cout<<" "<<i+1<<" Point (x,y) : "; cin>>P[0][i]>>P[1][i]; P[2][i]=1; } cout<<"n Enter the equation of line ( Y = m X + b) : "; char ch1; cin>>ch1; cin>>ch1; cin>>m; cin>>ch1; cin>>ch1; cin>>b; } void Display_Matrix(double p[3][10]) { for(int i=0; i<3; i++) { cout<<"n |"; for(int j=0; j<n; j++) cout<<" "<<p[i][j]; cout<<" |"; } } void DRAW(double p[3][10]) { for(int i=0; i<n-1; i++) { line(getmaxx()*0.5+p[0][i], getmaxy()*0.5+p[1][i], getmaxx()*0.5+p[0][i+1], getmaxy()*0.5+p[1][i+1]); } line(getmaxx()*0.5+p[0][n-1], getmaxy()*0.5+p[1][n-1], getmaxx()*0.5+p[0][0], getmaxy()*0.5+p[1][0]); }
  • 45. 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<3; i++) { for(int j=0; j<3; j++) { if(i==j) //DIAGONAL S[i][j] = 1; else S[i][j] = 0; } } //2nd - making changes S[0][0] = (1-m*m) / (m*m+1) ; S[0][1] = (2*m) / (m*m+1) ; S[0][2] = (-2*m*b) / (m*m+1) ; S[1][0] = (2*m) / (m*m+1) ; S[1][1] = (m*m-1) / (m*m+1) ; S[1][2] = (2*b) / (m*m+1); // Matrix Multiplication // S(3x3) x P(3xN) for(i=0; i<3; i++) { for(int j=0; j<n; j++) { P2[i][j]=0; for(int k=0; k<3; k++) { P2[i][j]+=S[i][k]*P[k][j]; } } } }
  • 46. void main() { INPUT(); Display_Matrix(P); cout<<endl; ReflectPoint(); for(int i=0; i<3; i++) { cout<<"n |"; for(int j=0; j<3; j++) cout<<" "<<S[i][j]; cout<<" |"; } cout<<endl; Display_Matrix(P2); cout<<"nn Press Enter to see Graphically? "; getch(); int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:/TC/BGI"); setcolor(DARKGRAY); //X AXIS line(getmaxx()*0.5, 0, getmaxx()*0.5, getmaxy()); //Y AXIS line(0, getmaxy()*0.5, getmaxx(), getmaxy()*0.5); setcolor(CYAN); DRAW(P); setcolor(GREEN); DRAW(P2); getch(); closegraph(); }
  • 48.   PROJECT:   TOWN IRRIGATOR (GAME) #include<stdio.h> #include<conio.h> #include<graphics.h> #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); }
  • 49.   ///////////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<COUNT_ChkPnts; i++) { if( FLAG_ChkPnts[i] !=1 ) { flag=0; break; } flag=1; } if(flag==1) { //CLEARING MESSAGE BOX setfillstyle(SOLID_FILL,BLACK); floodfill(410,25,WHITE); setcolor(GREEN); outtextxy(430,25, "ALL TOWNS IRRIGATED!"); FLAG_ChkPntsALL=1; } }
  • 50.   //////////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 && x<OXmax) && (y>OYmin && y<OYmax) ) { if( ch==RIGHT ) { x-=speed; } else if(ch==LEFT) { x+=speed; } if( ch==UP ) { y+=speed; } else if(ch==DOWN) { y-=speed; } if( OLDx == x || OLDy == y ) { c++; setcolor(LIGHTGRAY); if(c!=5 && FLAG_ChkPntsALL!=1) { //CLEARING MESSAGE BOX setfillstyle(SOLID_FILL,BLACK); floodfill(401,16,WHITE); outtextxy(410,25," YOU HIT ROUGH LAND!"); }}} if(FLAG_ChkPntsALL!=1) { setcolor(RED); if(c==1) outtextxy( 570 ,25,"X"); else if(c==2) if(c==2) outtextxy( 570 ,25,"XX"); if(c==3) outtextxy( 570 ,25,"XXX"); if(c==4) outtextxy( 570 ,25,"XXXX"); if(c==5) { setcolor(RED); outtextxy(410,25,"YOU LOSE BAD!"); outtextxy( 570 ,25,"XXXXX"); FLAG_Ob=1; sleep(1); } }}
  • 51.   ///////////////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<COUNT_ChkPnts; i++) FLAG_ChkPnts[i]=0; initgraph(&gd,&gm,"C:/TC/BGI"); /////////////////LOADER SCREEN//////////////// rectangle(50,90,600,300); outtextxy((getmaxx()*0.5)-100, (getmaxy()*0.5)-130, "**** TOWN IRRIGATOR ****"); outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-80, "The Towns in the district have been hit by a severe drought."); outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-65, "Due to the scarcity of Water, the population is suffering ! "); outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-50, "You can be a HERO, by supplying water to these towns."); outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-35, "Irrigate the towns and reach the finish without"); outtextxy((getmaxx()*0.5)-240, (getmaxy()*0.5)-20, "colliding with the obstacles."); outtextxy((getmaxx()*0.5)-100, (getmaxy()*0.5)+30, "Press Any Key when ready..."); getch(); outtextxy((getmaxx()*0.5)-80, (getmaxy()*0.5)+90, "Loading Map... :)"); sleep(1); outtextxy((getmaxx()*0.5)-90, (getmaxy()*0.5)+105, "Building Towns... :)"); sleep(1); outtextxy((getmaxx()*0.5)-120, (getmaxy()*0.5)+120, "Cleaning up the Mess... :P"); sleep(1); delay(10);
  • 52.   ///////////////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 HEAD setcolor(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");
  • 53.   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);
  • 54.   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;
  • 55.   //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 longest rectangle 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(); }