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