Sie sind auf Seite 1von 61

/*Program to implement 2-D transformations */

#include <iostream.h>
#include <stdlib.h>
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#define PI 3.14
float cord[10][3];
int n;
void myclrscr()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
}
void getPoints()
{
int i;
printf("Enter the no of defining vertices: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter coordinate %d: ",i+1);
scanf("%f %f",&cord[i][0],&cord[i][1]);
cord[i][2]=1;
}
}
void drawPolygon()
{
int i;
for(i=0;i<n-1;i++)
line(cord[i][0],cord[i][1],cord[i+1][0],cord[i+1][1]);
line(cord[0][0],cord[0][1],cord[i][0],cord[i][1]);
}
void mul(float mat1[][3],float m1,int n,float mat2[][3],int m2,float ans[][3])
{
int i,j,k;
float sum;

for(i=0;i<m1;i++)
for(j=0;j<m2;j++)
{
sum=0;
for(k=0;k<n;k++)
sum+=mat1[i][k]*mat2[k][j];
ans[i][j]=sum;
}
}
void translate(int tx,int ty,int draw)
{
int i;
float tMat[3][3]={ 1,0,0,
0,1,0,
0,0,1 };
tMat[2][0]=tx;
tMat[2][1]=ty;
mul(cord,n,3,tMat,3,cord);
if(draw) drawPolygon();
}
void scale(float sx,float sy,int xf,int yf)
{
int i;
float tMat[3][3]={1,0,0,
0,1,0,
0,0,1 };
tMat[0][0]=sx;
tMat[1][1]=sy;
tMat[2][0]=xf-sx*xf;
tMat[2][1]=yf-sy*yf;
mul(cord,n,3,tMat,3,cord);
drawPolygon();
}
void rotate(float angle,int xf,int yf)
{
int i;

float tMat[3][3]={1,0,0,
0,1,0,
0,0,1 };
angle*=PI/180;
translate(-xf,-yf,0);
tMat[0][0]=cos(angle);
tMat[0][1]=sin(angle);
tMat[1][0]=-sin(angle);
tMat[1][1]=cos(angle);
mul(cord,n,3,tMat,3,cord);
translate(xf,yf,0);
drawPolygon();
}
void main()
{
float xf,yf,sx,sy,tx,ty,r;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "e:\\proggy\\tc\\bgi");
getPoints();
drawPolygon();
getch();
myclrscr();
cout<<"1- Translation"<<"\n";
cout<<"2- Scaling"<<"\n";
cout<<"3- Rotation "<<"\n";
cout<<"0- Exit"<<"\n";
int choice;
while(1)
{
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1 : printf("Enter the translating co-ordinates:");
scanf("%f %f",&tx,&ty);
myclrscr();
translate(tx,ty,1);
break;
case 2 : printf("Enter the Scaling factors:");
scanf("%f %f",&sx,&sy);
printf("Enter the refrence co-ordinates: ");

scanf("%f %f",&xf,&yf);
myclrscr();
scale(sx,sy,xf,yf);
break;
case 3: printf("Enter the Rotating angle :");
scanf("%f",&r);
printf("Enter the refrence co-ordinates: ");
scanf("%f %f",&xf,&yf);
myclrscr();
rotate(r,xf,yf);
break;
case 0 : exit(0);
getch();
}
}
getch();
}

/* Program to show Animation */


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
int dx,dy,s1,s2,dp,swap=0;
void bressenham(int x1,int x2,int y1,int y2)
{
dx=abs(x2-x1);
dy=abs(y2-y1);
if(x2<x1) s1=-1;
else if(x2>x1) s1=1;
else s1=0;
if(y2<y1) s2=-1;
else if(y2>y1) s2=1;
else s2=0;
dp=2*dy-dx;
if(dy>dx)
{
int temp=dy;
dy=dx;
dx=temp;
swap=1;
}
}
void erase(int x,int y,int r,int x3,int y3,int x4,int y4
,int x5,int y5,int x6,int y6)
{
setcolor(BLACK);
circle(x,y,r);
line(x3,y3,x4,y4);
line(x5,y5,x6,y6);
}
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setcolor(WHITE);

int x1=100,y1=100,h,b,x3,y3,x4,y4,x5,y5,x6,y6,x,y,r;
printf("Enter the height of the wedge : ");
scanf("%d",&h);
printf("Enter the width of the edge : ");
scanf("%d",&b);
printf("Enter the radius of the circle : ");
scanf("%d",&r);
int x2=x1+b,y2=y1+h;
cleardevice();
line(x1,y1,x2,y2);
line(x1,y1,x1,y2);
line(x1,y2,x2,y2);
float angle=atan((float)(y2-y1)/(x2-x1));
x=x1+r*sin(angle);
y=y1-r*cos(angle);
x3=x; y3=y+r;
x4=x; y4=y-r;
x5=x+r; y5=y;
x6=x-r; y6=y;
line(x3,y3,x4,y4);
line(x5,y5,x6,y6);
circle(x,y,r);
float ang=1/r;
bressenham(x1,x2,y1,y2);
while(x<(x2+r*sin(angle)) && y<(y2-r*cos(angle)))
{
ang+=(float)1/r;
erase(x,y,r,x3,y3,x4,y4,x5,y5,x6,y6);
setcolor(WHITE);
line(x1,y1,x2,y2);
if(dp<0)
{
if(swap) y=y+s2;
else x=x+s1;
dp+=2*dy;
}
else
{
x=x+s1;
y=y+s2;
dp=dp+2*dy-2*dx;
}
x3=x+(float)r*cos(-ang);
x4=x-(float)r*cos(-ang);
y3=y-(float)r*sin(-ang);

y4=y+(float)r*sin(-ang);
x5=x-(float)r*sin(+ang);
x6=x+(float)r*sin(+ang);
y5=y+(float)r*cos(+ang);
y6=y-(float)r*cos(+ang);
line(x3,y3,x4,y4);
line(x5,y5,x6,y6);
circle(x,y,r);
delay(10);
}
getch();
}

/* To implement Bezier Curve */


#include<dos.h>
#include<graphics.h>
#include<conio.h>
int a[20],b[20],m,n=0,j,d;
float bx[5000],by[5000],u;
union REGS i,o;
initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}
void showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
void restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}
void getmousepos(int *button,int *x,int *y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}
void hidemouseptr()
{
i.x.ax=2;

int86(0x33,&i,&o);
}
int compco()
{
int k,coeff=1;
for(k=d+1;k<=n;k++) coeff=coeff*k;
for(k=2;k<=n-d;k++) coeff=coeff/k;
return coeff;
}
float blenval()
{
int k;
float blend;
blend=compco();
for(k=1;k<=d;k++) blend=blend*u;
for(k=1;k<=n-d;k++) blend=blend*(1.0-u);
return blend;
}
void bezier()
{
float blend1;
bx[j]=0;by[j]=0;
for(d=0;d<=n;d++)
{
blend1=blenval();
bx[j]+=a[d]*blend1;
by[j]+=b[d]*blend1;
}
}
void curve()
{
for(j=0;j<m;j++) putpixel(bx[j],by[j],GREEN);
}
void drawi()
{
for(j=0;j<=m;j++)
{
u=(float)j/m;
bezier();
}
curve();

}
void main()
{
int gd=DETECT,gm;
int button,tx,ty;
clrscr();
m=4990;
initgraph(&gd,&gm,"");
cleardevice();
showmouseptr();
while(!kbhit())
{
getmousepos(&button,&tx,&ty);
if((button & 1) == 1)
{
hidemouseptr();
setcolor(MAGENTA);
a[n]=tx;
b[n]=ty;
//printf("%d %d\n",a[n],b[n]);
putpixel(tx,ty,WHITE);
circle(tx,ty,2);
n++;
delay(1000);
showmouseptr();
}
}
n--;
drawi();
getch();
}

/* Bressenhams Circle Drawing Algorithm */


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int xc,yc,r;
void symm(int x,int y)
{
putpixel(x+xc,y+yc,WHITE);
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 bhsmcircle(int xc,int yc,int r)
{
int x=0,y=r,dp;
dp=3-2*r;
while(x!=y)
{
if(dp<0)
{
dp+=4*x+6;
symm(++x,y);
}
else
{
dp+=4*(x-y)+10;
symm(++x,--y);
}
}
}
void main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");

printf("Enter the centre co-ordinates: ");


scanf("%d %d",&xc,&yc);
printf("Enter the radius: ");
scanf("%d",&r);
bhsmcircle(xc,yc,r);
getch();
}

/* Bressenhams Line Drawing Algorithm */


#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
void bshmLine(int x1,int y1,int x2,int y2)
{
int x=x1,y=y1,dx,dy,s1,s2;
int length,i,dp,temp,swap=0;
putpixel(x1,y1,WHITE);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(x2<x1) s1=-1;
else if(x2>x1) s1=1;
else s1=0;
if(y2<y1) s2=-1;
else if(y2>y1) s2=1;
else s2=0;
dp=2*dy-dx;
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
swap=1;
}
for(i=1;i<=dx;i++)
{
if(dp<0)
{
if(swap) putpixel(x,y=y+s2,WHITE);
else putpixel(x=x+s1,y,WHITE);
dp+=2*dy;
}
else
{
putpixel(x=x+s1,y=y+s2,WHITE);
dp=dp+2*dy-2*dx;
}

}
}
void main()
{
int x1,y1,x2,y2;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
printf("Enter the starting co-ordinates: ");
scanf("%d %d",&x1,&y1);
printf("Enter the ending co-ordinates: ");
scanf("%d %d",&x2,&y2);
bshmLine(x1,y1,x2,y2);
getch();
}

/*- Line clipping using cohen sutherland algo -*/


/*------------------------------------------------------------*/
#include<stdio.h>
#include<graphics.h>
typedef unsigned int outcode;
enum {
TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do{
if(!(code0 | code1))
{
accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);

x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);
if(accept) line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
}
/*--------------------------------------------------------------------*/
int calcode (x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;
if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;
return(code);
}

/*-------------------------------------------------*/
main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
cleardevice();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
}
/***************** COHEN-SUTHERLAND LINE CLIPPING ***************/

/* DDA Line Drawing Algorithm */


#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
void ddaLine(int x1,int y1,int x2,int y2)
{
float x=x1,y=y1,dx,dy;
int length,i;
putpixel(x1,y1,WHITE);
if(abs(x2-x1)>=abs(y2-y1))
length=abs(x2-x1);
else
length=abs(y2-y1);
dx=(float)(x2-x1)/length;
dy=(float)(y2-y1)/length;
for(i=1;i<=length;i++)
{
x=x+dx;
y=y+dy;
putpixel((int)x,(int)y,WHITE);
}
}
void main()
{
int x1,y1,x2,y2;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
printf("Enter the starting co-ordinates: ");
scanf("%d %d",&x1,&y1);
printf("Enter the ending co-ordinates: ");
scanf("%d %d",&x2,&y2);
ddaLine(x1,y1,x2,y2);
getch();
}

/*- Flood fill algo. using user defined function -*/


/******************************************************/
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y);
x=x-1;
fill_right(x,y-1);
fill_right(x,y+1);
}
}
void fill_left(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_left(--x,y);
x=x+1;
fill_left(x,y-1);
fill_left(x,y+1);
}
}
/*------------------------------------------------------*/
void main()
{
int x , y ,a[10][10];
int gd, gm ,n,i;
detectgraph(&gd,&gm);
initgraph(&gd,&gm," ");

printf("\n\n\tEnter the no. of edges of polygon : ");


scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");
for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
printf("\n\n\tEnter the seed pt. : ");
scanf("%d%d",&x,&y);
cleardevice();
setcolor(WHITE);
for(i=0;i<n;i++) /*- draw poly -*/
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill_right(x,y);
fill_left(x-1,y);
getch();
}
/*******************************************************/

///////////// GAME _ ARCHERY /////////


#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
void score(int i)
{
setcolor(10);
settextstyle(2,0,1);
outtextxy(25+i+5,(getmaxy())-25,"*");
}
void erase(int x)
{
setcolor(9);
settextstyle(2,0,1);
outtextxy(25+x+5,(getmaxy())-25,"*");
}
void main()
{
/* select a driver and mode that supports */
/* multiple background colors.
*/
int gdriver = EGA, gmode = EGAHI, errorcode;
int poly1[20],poly2[20],i,j,k,m=0,n=225,x,y,z,p;
int poly[20],a=0,b,s=0;
char ch,ans='y',msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* for centering text messages */
settextjustify(CENTER_TEXT, CENTER_TEXT);
x = getmaxx();// / 2;
y = getmaxy();// / 2;

setbkcolor(9);
getch();
/*score board*/
setcolor(12);
rectangle(5,y-55,105,y-5);
rectangle(8,y-52,102,y-8);
settextstyle(1,0,1);
outtextxy(55,y-45,"SCORE");
/*name*/
settextstyle(2,0,4);
outtextxy(x/2,y-10,"BY ARUN PRASANTH.B I-year ECE");
while(ans=='y')
{
/*bow*/
j=5;
i=5;
setcolor(14);
arc(5,25,315,45,45);
line(5,25,39,3);
line(5,25,39,47);
setcolor(9);
line(39,3,39,47);
line(50,24,50,26);
a=0;
m=0;
n=225;
s=0;
for(p=5;p<65;p=p+5)
{
erase(p);
}
while(a<10)
{
poly[0]=i+2; poly[1]=m+25;
poly[2]=i; poly[3]=m+24;
poly[4]=i+55; poly[5]=m+24;
poly[6]=i+50; poly[7]=m+20;
poly[8]=i+60; poly[9]=m+25;
poly[10]=i+50; poly[11]=m+30;
poly[12]=i+55; poly[13]=m+26;
poly[14]=i; poly[15]=m+26;

poly[16]=i+2; poly[17]=m+25;
setcolor(4);
setfillstyle(1,4);
fillpoly(9,poly);
m=m+25;
a++;
}
/*baloon*/
setcolor(10);
setfillstyle(1,10);
fillellipse(x-50,y-50,15,20);
b=10;
//getch();
while(b>0)
{
setcolor(4);
setfillstyle(1,4);
i=5;
poly[0]=i+2;
poly[1]=25;
poly[2]=i;
poly[3]=24;
poly[4]=i+55;
poly[5]=24;
poly[6]=i+50;
poly[7]=20;
poly[8]=i+60;
poly[9]=25;
poly[10]=i+50; poly[11]=30;
poly[12]=i+55; poly[13]=26;
poly[14]=i;
poly[15]=26;
poly[16]=i+2;
poly[17]=25;
fillpoly(9,poly);
setcolor(14);
line(5,25,39,3);
line(5,25,39,47);
for(k=y-50;k>0&&(!kbhit());k--)
{
setcolor(10);
setfillstyle(1,10);
fillellipse(x-50,k,15,20);
delay(15);
setcolor(9);
setfillstyle(1,9);
fillellipse(x-50,k,20,20);

}
j=5;
if(k==0&&b==1)
{
setcolor(9);
line(5,25,39,3);
line(5,25,39,47);
setcolor(14);
line(39,3,39,47);
line(50,24,50,26);
}
else if(k==0)
{
setcolor(4);
setfillstyle(1,4);
fillpoly(9,poly);
}
else
{
for(i=5,z=k;i<x||z>0;z--,i=i+5)
{
//if(z==0)
//break;
if(j<35)
{
setcolor(14);
line(j,25,39,3);
line(j,25,39,47);
}
if(j==35)
{
setcolor(14);
line(39,3,39,47);
line(50,24,50,26);
}
setcolor(4);
setfillstyle(1,4);
//m=0;
poly[0]=i+2;
poly[1]=25;
poly[2]=i;
poly[3]=24;
poly[4]=i+55;
poly[5]=24;
poly[6]=i+50;
poly[7]=20;
poly[8]=i+60;
poly[9]=25;

poly[10]=i+50; poly[11]=30;
poly[12]=i+55; poly[13]=26;
poly[14]=i;
poly[15]=26;
poly[16]=i+2;
poly[17]=25;
fillpoly(9,poly);
setcolor(10);
setfillstyle(1,10);
fillellipse(x-50,z,15,20);
delay(15);
if(j<35)
{
setcolor(9);
line(j,25,39,3);
line(j,25,39,47);
j++;
}
//burst
if(poly[8]>=x-70&&poly[8]<x&&z<=45&&z>=10)
{
setcolor(9);
setfillstyle(1,9);
fillellipse(x-50,z,20,20);
setcolor(10);
setfillstyle(1,10);
poly1[0]=x-70; poly1[1]=z+5;
poly1[2]=x-50; poly1[3]=z-10;
poly1[4]=x-30; poly1[5]=z+5;
poly1[6]=x-70; poly1[7]=z+5;
poly2[0]=x-70; poly2[1]=z-5;
poly2[2]=x-30; poly2[3]=z-5;
poly2[4]=x-50; poly2[5]=z+10;
poly2[6]=x-70; poly2[7]=z-5;
fillpoly(4,poly1);
fillpoly(4,poly2);
s=s+5;
delay(5);
score(s);
setcolor(9);
setfillstyle(1,9);
fillpoly(4,poly1);
fillpoly(4,poly2);
fillpoly(9,poly);

break;
}
setcolor(9);
setfillstyle(1,9);
fillpoly(9,poly);
fillellipse(x-50,z,20,20);
line(39,3,39,47);
line(50,24,50,26);
}
}
setcolor(9);
setfillstyle(1,9);
fillellipse(x-50,z,20,20);
line(39,3,39,47);
line(50,24,50,26);
i=5;
poly[0]=i+2;
poly[1]=n+25;
poly[2]=i;
poly[3]=n+24;
poly[4]=i+55;
poly[5]=n+24;
poly[6]=i+50;
poly[7]=n+20;
poly[8]=i+60;
poly[9]=n+25;
poly[10]=i+50; poly[11]=n+30;
poly[12]=i+55; poly[13]=n+26;
poly[14]=i;
poly[15]=n+26;
poly[16]=i+2;
poly[17]=n+25;
delay(10);
fillpoly(9,poly);
n=n-25;
b--;
fflush(stdin);
getch();
}//end of 1 game
setcolor(14);
line(39,3,39,47);
line(50,24,50,26);
setcolor(14);
settextstyle(2,0,4);
outtextxy(x/2,y/2,"Continue y/n");
ans=getch();
setcolor(9);
settextstyle(2,0,4);
outtextxy(x/2,y/2,"Continue y/n");

}
//clean up
getch();
restorecrtmode();
closegraph();
}

////////// LIANGE BARSKEY LINE CLIPPING


ALGO ///////////
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin , wymin , wxmax , wymax ;
float u1 = 0.0 , u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left , bottom boundry\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right ,bottom boundry\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||
( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||
( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");
initgraph(&gd,&gm,"");
setcolor(2);

rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}
if( u1 > u2 )
printf("line rejected\n");

else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;
x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );
printf("Original line cordinates\n");
printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);
printf("Windows coordiante are \n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d
",wxmin,wymin,wxmax,wymax);
printf("New coordinates are \n");
printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);
initgraph(&gd,&gm,"");
setcolor(2);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
setcolor(3);
line(x11,y11,x22,y22);
getch();
}
}
}
/*-*****************************************************************-*/

///////// PROGRAM FOR PIE CHART /////////


#include<stdio.h>
#include<math.h>
#include<graphics.h>
void piechart( int xc, int yc, float r);
struct point
{
float x,y;
} pts[2];
int N, xc, yc;
float r;
float data[5];
main()
{
int i;
clrscr();
printf("\n Enter no of points : ");
scanf("%d", &N);
printf("\n Enter data : ");
for(i=0; i<N ; i++)
scanf("%f",&data[i]);
xc = 640/2;
yc = 480/2;
r = 200;
piechart(xc, yc, r);
getch();
}
void piechart( int xc, int yc, float r)
{
int gd,gm,code=10;
int i,x2,y2;
float ls, theta, total;
detectgraph(&gd, &gm);
initgraph(&gd,&gm, "C:\\TC\\BGI");

circle(xc, yc, r);


total = 0; theta = 0;
for(i=0; i<N; i++)
total += data[i];
pts[0].x = xc;
pts[0].y = yc;
for(i=0; i<N; i++)
{
theta += 2 * 3.14 * data[i] /total;
pts[i].x = xc + r * cos(theta);
pts[i].y = yc + r * sin(theta);
x2 = pts[i].x;
y2 = pts[i].y;
setcolor(code++);
line(xc, 480-yc, x2, 480-y2);
getch();
}
}
/*--------------------------------------------------------*/

/*Program to implement Ploygon Clipping */


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
union REGS i,o;
struct pt
{
int x,y;
};
float xl,xr,yt,yb,m,slope[20];
int bc=0,xc,yc,n=0,k,dy,dx,x,y,temp,a[20][2],xi[20];
struct point
{
float x,y;
};
enum bound {left,right,bottom,top};
int inside(struct point p, enum bound b)
{
int c=1;
switch(b)
{
case left : if(p.x<xl)
c=0;
break;
case right : if(p.x>xr)
c=0;
break;
case bottom : if(p.y>yb)
c=0;
break;
case top : if(p.y<yt)
c=0;
break;
}
return(c);
}
struct point intersect (struct point p1,struct point p2,enum bound b)
{

struct point t;
float m=0;
if(p2.x!=p1.x)

m=(p2.y - p1.y)/(p2.x-p1.x);

switch(b)
{
case left : t.x =xl;
t.y = p2.y+(xl-p2.x)*m;
break;
case right: t.x=xr;
t.y = p2.y + (xr-p2.x)*m;
break;
case bottom: t.y = yb;
if(p1.x==p2.x) t.x=p2.x;
else t.x = p2.x +(yb-p2.y)/m;
break;
case top : t.y = yt;
if(p1.x==p2.x) t.x=p2.x;
else t.x = p2.x +(yt-p2.y)/m;
break;
}
return t;
}
initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}
showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
return(0);
}
hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
return(0);
}
getmousepos(int *button,int *x,int *y)

{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}
void main()
{
enum bound b;
int cou,i,gd=DETECT,gm,flag;
struct point p[30],pout[30],z;
initgraph(&gd,&gm,"");
cleardevice();
showmouseptr();
while(bc!=2) //poly
{
getmousepos(&bc,&xc,&yc);
if(bc==1)
{
p[n].x=xc;
p[n].y=yc;
n++;
hidemouseptr();
if(n>1)
line(p[n-2].x,p[n-2].y,xc,yc);
showmouseptr();
delay(200);
}
}
p[n]=p[0];
hidemouseptr();
line(p[n-1].x,p[n-1].y,p[n].x,p[n].y);
showmouseptr();
getmousepos(&bc,&xc,&yc);
flag=1;
bc=0;
while(bc!=2) //window
{
if((bc==1) && (flag==1))
{
xl=xc;
yt=yc;
flag=2;
delay(200);

}
else
{
xr=xc;
yb=yc;
delay(200);
}
getmousepos(&bc,&xc,&yc);
}
hidemouseptr();
rectangle(xl,yt,xr,yb);
getch();
for(b=left;b<=top;b++)
{
cou =-1;
for(i=0;i<n;i++)
if((inside(p[i],b)==0) && (inside(p[i+1],b)==1))
{
z=intersect(p[i],p[i+1],b);
pout[++cou] =z;
pout[++cou]=p[i+1];
}
else
if((inside(p[i],b)==1)&&(inside(p[i+1],b)==1))
pout[++cou]=p[i+1];
else
if((inside(p[i],b)==1)&&(inside(p[i+1],b)==0))
{
z = intersect(p[i],p[i+1],b);
pout[++cou]=z;
}
pout[++cou]=pout[0];
n=cou;
for(i=0;i<=n;i++) p[i]=pout[i];
}
getch();
cleardevice();
rectangle(xl,yt,xr,yb);
for(i=0;i<n;i++) line(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
getch();
for(i=0;i<=n;i++)
{
a[i][0]=p[i].x;
a[i][1]=p[i].y;
}
getch();}

///////// CG PROJECT ON PAINT /////////////


#include <stdio.h>
#include <iostream.h>
#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
//VIEWPORT dimensions
#define XLPOS 0
#define YLPOS 20
#define XRPOS maxx-50
#define YRPOS maxy-50
//COLOR Window dimensions
#define COLOR_XLPOS 50
#define COLOR_YLPOS maxy-40
#define COLOR_XRPOS 400
#define COLOR_YRPOS maxy
#define COLOR_XSIZE 50
#define COLOR_YSIZE 20
//FUNCTION Window dimensions
#define FUNC_XLPOS maxx-40
#define FUNC_YLPOS 50
#define FUNC_XRPOS maxx
#define FUNC_YRPOS 300
#define FUNC_XSIZE 20
#define FUNC_YSIZE 50
#define DEFAULT_FUNCTION POINT
#define ERASE_SIZE 5
int sqr(int n) { return n*n; }
enum bool{FALSE,TRUE};
enum Button{NONE,LEFT,RIGHT,BOTH};
enum
Function{POINT,PENCIL,LINE,POLYLINE,CIRCLE,RECTANGLE,FILL,ERASE,SAV
E,OPEN};
enum Area{VIEWPORT,COLOR,FUNCTION,NON};
char*
functions[]={"Point","Pen","Line","Poly","Circle","Rect","Fill","Erase","Save","Open"};

int selectedColor=WHITE,borderColor=WHITE;
int maxx,maxy;
union REGS p,o;
initmouse()
{
p.x.ax=0;
int86(0x33,&p,&o);
return(o.x.ax);
}
void showmouseptr()
{
p.x.ax=1;
int86(0x33,&p,&o);
}
void restrictmouseptr(int x1,int y1,int x2,int y2)
{
p.x.ax=7;
p.x.ax=x1;
p.x.ax=x2;
int86(0x33,&p,&o);
p.x.ax=8;
p.x.ax=y1;
p.x.ax=y2;
int86(0x33,&p,&o);
}
void getmousepos(int* button,int* x,int* y)
{
p.x.ax=3;
int86(0x33,&p,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}
void hidemouseptr()
{
p.x.ax=2;
int86(0x33,&p,&o);
}

Area getClickedArea(int x,int y)


{
if(x>XLPOS && x<XRPOS && y<YRPOS &
y>YLPOS) return VIEWPORT;
else if(x>=FUNC_XLPOS && x<FUNC_XRPOS &&
y>=FUNC_YLPOS && y<=FUNC_YRPOS) return FUNCTION;
else if(x>=COLOR_XLPOS && x<COLOR_XRPOS
&& y>COLOR_YLPOS && y<COLOR_YRPOS) return COLOR;
else return NON;
}
Button pressed(int button)
{
switch(button)
{
case 0: return NONE;break;
case 1: return LEFT;break;
case 2: return RIGHT;break;
case 3: return BOTH;break;
}
}
void drawBorder()
{
setcolor(WHITE);
rectangle(XLPOS,YLPOS,XRPOS,YRPOS);
setcolor(selectedColor);
}
void getCords(int& x,int& y,Button& b)
{
int button;
getmousepos(&button,&x,&y);
b=pressed(button);
}
void drawFunctions(Function selectedFunction)
{
int z=0;
for(int
i=FUNC_XLPOS;i<FUNC_XRPOS;i+=FUNC_XSIZE)
{
for(int
j=FUNC_YLPOS;j<FUNC_YRPOS;j+=FUNC_YSIZE)
{
setfillstyle(SOLID_FILL,z++);

bar(i,j,i+FUNC_XSIZE,j+FUNC_YSIZE);
setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
outtextxy(i,j+(FUNC_YSIZE)/2,functions[z1]);
}
}
setcolor(WHITE);
outtextxy(FUNC_XLPOS,FUNC_YRPOS+44,"");
outtextxy(FUNC_XLPOS,FUNC_YRPOS+50,"");
outtextxy(FUNC_XLPOS,FUNC_YRPOS+56,"");
setcolor(RED);
outtextxy(FUNC_XLPOS,FUNC_YRPOS+50,functions[selectedFunction]);
setcolor(selectedColor);
setfillstyle(SOLID_FILL,BLACK);
}
void drawColors(int selectedColor,int borderColor)
{
int z=WHITE;
for(int
i=COLOR_XLPOS;i<COLOR_XRPOS;i+=COLOR_XSIZE)
{
for(int
j=COLOR_YLPOS;j<COLOR_YRPOS;j+=COLOR_YSIZE)
{
setfillstyle(SOLID_FILL,z%16);
z++;
bar(i,j,i+COLOR_XSIZE,j+COLOR_YSIZE);
}
}
setfillstyle(SOLID_FILL,selectedColor);
bar(COLOR_XRPOS+COLOR_XSIZE,COLOR_YLPOS,COLOR_XRPOS+2*(COLOR_
XSIZE),COLOR_YRPOS-COLOR_YSIZE);
setfillstyle(SOLID_FILL,borderColor);
bar(COLOR_XRPOS+COLOR_XSIZE,COLOR_YRPOSCOLOR_YSIZE,COLOR_XRPOS+2*(COLOR_XSIZE),COLOR_YRPOS);

setfillstyle(SOLID_FILL,BLACK);
}
void setImage(char* file)
{
hidemouseptr();
clrscr();
cleardevice();
drawBorder();
drawColors(selectedColor,borderColor);
drawFunctions(DEFAULT_FUNCTION);
FILE* fp=fopen(file,"rb");
for(int i=XLPOS;i<XRPOS;i++)
{
for(int j=YLPOS;j<YRPOS;j++)
{
putpixel(i,j,getc(fp));
}
}
fclose(fp);
showmouseptr();
}
void getImage(char* file)
{
hidemouseptr();
FILE* fp=fopen(file,"w");
for(int i=XLPOS;i<XRPOS;i++)
{
for(int j=YLPOS;j<YRPOS;j++)
{
fputc(getpixel(i,j),fp);
}
}
fclose(fp);
setImage(file);
showmouseptr();
}

void funcPoint(int x,int y,Button b)


{
if(b==LEFT)
{
hidemouseptr();
putpixel(x,y,getcolor());
showmouseptr();
}
}
void funcLine(int x,int y,Button b)
{
int prevx=x;
int prevy=y;
while(b==LEFT)
{
bool isOut=FALSE;
if(getClickedArea(x,y)!=VIEWPORT) isOut=TRUE;
if(isOut) break;
getCords(x,y,b);
}
hidemouseptr();
if(x!=prevx && y!=prevy) line(x,y,prevx,prevy);
showmouseptr();
}
void funcPolyLine(int x,int y,Button b)
{
bool isOut;
while(b!=RIGHT)
{
int prevx=x;
int prevy=y;
do
{
getCords(x,y,b);
}
while(b!=LEFT && b!=RIGHT);
isOut=FALSE;
if(getClickedArea(x,y)!=VIEWPORT) isOut=TRUE;
if(isOut) break;

hidemouseptr();
line(prevx,prevy,x,y);
showmouseptr();
}
}
void funcPencil(int x,int y,Button b)
{
bool isOut;
if(b==LEFT)
{
int prevx=x;
int prevy=y;
hidemouseptr();
while(b==LEFT)
{
isOut=FALSE;
if(getClickedArea(x,y)!=VIEWPORT)
isOut=TRUE;
if(isOut) break;
line(x,y,prevx,prevy);
prevx=x;prevy=y;
getCords(x,y,b);
}
showmouseptr();
}
}
void funcFill(int x,int y,Button b)
{
if(b==LEFT)
{
hidemouseptr();
setfillstyle(SOLID_FILL,selectedColor);
floodfill(x,y,borderColor);
showmouseptr();
}
}
void funcCircle(int x,int y,Button b)
{
if(b==LEFT)
{
int prevx=x;
int prevy=y;

bool isOut;
while(b==LEFT)
{
isOut=FALSE;
if(getClickedArea(x,y)!=VIEWPORT)
isOut=TRUE;
if(isOut) break;
getCords(x,y,b);
}
hidemouseptr();
if(!isOut)
ellipse((int)fabs(prevx+x)/2,
(int)fabs(prevy+y)/2,0,360,(int)fabs(prevx-x)/2,(int)fabs(prevy-y)/2);
showmouseptr();
}
}
void funcRectangle(int x,int y,Button b)
{
if(b==LEFT)
{
int prevx=x;
int prevy=y;
bool isOut;
while(b==LEFT)
{
isOut=FALSE;
if(getClickedArea(x,y)!=VIEWPORT)
isOut=TRUE;
if(isOut) break;
getCords(x,y,b);
}
hidemouseptr();
if(!isOut) rectangle(prevx,prevy,x,y);
showmouseptr();
}
}
void funcErase(int x,int y,Button b)
{
if(b==LEFT)
{
int prevx=x;
int prevy=y;

bool isOut;
while(b==LEFT)
{
isOut=FALSE;
if(getClickedArea(x,y)!=VIEWPORT)
isOut=TRUE;
if(isOut) break;
hidemouseptr();
setfillstyle(SOLID_FILL,BLACK);
bar(prevx-ERASE_SIZE,prevyERASE_SIZE,x+ERASE_SIZE,y+ERASE_SIZE);
showmouseptr();
prevx=x;
prevy=y;
getCords(x,y,b);
}
}
}
void main()
{
clrscr();
getch();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setcolor(selectedColor);
int button,x,y;
maxx=getmaxx();
maxy=getmaxy();
setviewport(0,0,maxx,maxy,1);
drawBorder();
drawColors(selectedColor,borderColor);
drawFunctions(DEFAULT_FUNCTION);
if(initmouse()==0)
{
closegraph();
restorecrtmode();
cout<<"Mouse driver not loaded";
exit(1);
}
showmouseptr();
while(!kbhit())

{
restrictmouseptr(0,0,maxx,maxy);
Button b;
Area a;
Function f;
getCords(x,y,b);
if(b==LEFT || b==RIGHT) a=getClickedArea(x,y);
if(a==VIEWPORT)
{
restrictmouseptr(XLPOS,YLPOS,XRPOS,YRPOS);
switch(f)
{
case POINT:
funcPoint(x,y,b);break;
case LINE:
funcLine(x,y,b);a=NON;break;
case POLYLINE:
funcPolyLine(x,y,b);a=NON;break;
case PENCIL:
funcPencil(x,y,b);break;
case FILL:
funcFill(x,y,b);break;
case CIRCLE:
funcCircle(x,y,b);break;
case RECTANGLE:
funcRectangle(x,y,b);break;
case ERASE:
funcErase(x,y,b);drawBorder();break;
}
}
else if(a==COLOR)
{
hidemouseptr();
if(b==LEFT)
{
selectedColor=getpixel(x,y);
setcolor(selectedColor);
setfillstyle(SOLID_FILL,selectedColor);

}
else if(b==RIGHT)
{
borderColor=getpixel(x,y);
}
b=NONE;
drawColors(selectedColor,borderColor);
a=NON;
showmouseptr();
}
else if(a==FUNCTION)
{
hidemouseptr();
f=getpixel(x,y);
drawFunctions(f);
switch(f)
{
case SAVE:
cout<<"Enter file to save: ";
char file[20];
gets(file);
getImage(file);break;
case OPEN:
cout<<"Enter file to open: ";
gets(file);
setImage(file);break;
}
a=NON;
showmouseptr();
}
}
getch();
}

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

#include <graphics.h>
#include <dos.h>
#include<stdlib.h>
#include<time.h>
int arr[1000][2],prev[1000][2],k;
draw()
{
int i,x,y,x1,y1;
setcolor(8);
rectangle(0,0,490,480);
for ( i = 0 ; i < k;i++)
{
x = arr[i][0]*10;
y = arr[i][1]*10;
setcolor(11);
rectangle(x,y,x+10,y+10);
}
x1 = prev[k-1][0]*10;
y1 = prev[k-1][1]*10;
setcolor(BLACK);
rectangle(x1,y1,x1+10,y1+10);
}
main()
{
char direct = 77;
int gd = DETECT,gm,i,j,life,count,born,level,valid,bx,by,direct1,score = 0;
int bonus,count1;
k = 3;
for( i = 0 ; i < k;i++)
{
arr[i][0] = k-1-i;
arr[i][1] = 0;
}
clrscr();
initgraph(&gd,&gm,"e:\\proggy\\tc\\bgi");
setcolor(LIGHTMAGENTA);
outtextxy(150,20,"HAVE FUN WITH SNAKES");
outtextxy(10,30,"RULES TO PLAY (play using arrow keys)");
outtextxy(10,40,"GAME STARTS WITH A");
setcolor(11);
outtextxy(10,60,"SNAKE() WITH LENGTH THREE");
setcolor(LIGHTMAGENTA);
outtextxy(250,60,"GO AND EAT THE ");
setcolor(RED);

outtextxy(10,70,"RED COLOUR REFRESHING DIET()");


setcolor(LIGHTMAGENTA);
outtextxy(250,70,"LENGTH OF THE SNAKE INCREASES WITH MORE DIET");
setcolor(10);
outtextxy(10,80,"Press ESC to quit the game");
outtextxy(10,100,"GAME GETS OVER WHEN THE SNAKE DASHES ITSELF");
setcolor(7);
outtextxy(10,140,"GAME COPYRIGHT KAMAL PARYANI ");
outtextxy(10,150,"any suggestions or queries mail at kamal8182@yahoo.co.in");
getch();
initgraph(&gd,&gm,"e:\\proggy\\tc\\bgi");
printf("Select level : \n");
printf("\n4 -- excellent");
printf("\n3 -- good");
printf("\n2 -- moderate");
printf("\n1 -- beginner");
printf("\nENTER");
scanf("%d",&level);
initgraph(&gd,&gm,"e:\\proggy\\tc\\bgi");
// col = 1;
// for ( i = 0; i < k;i++)
count = 0;
setcolor(2);
outtextxy(500,196,"SCORE");
direct = 77;
life = 0;
randomize();
while(direct != 27)
{
if ( life == 0)
while(valid)
{
valid = 0;
bx = random(47);
by = random(46);
for(i = 0 ; i < k;i++)
if(bx == arr[i][0] && by == arr[i][1])
valid = 1;
}
life = 1;
setcolor(LIGHTMAGENTA);
if(count == 200)
{
bonus = 1;
count1 = 0;

setcolor(RED);
}
if(bonus)
{
count1++;
setcolor(RED);
outtextxy(500,140,"PROTIENS");
}
rectangle(bx*10,by*10,bx*10+10,by*10+10);
count++;
if(bonus)
count1++;
if(count1 == 40)
{
bonus = 0;
count1 = 0;
count = 0;
setcolor(WHITE);
outtextxy(500,140,"");
}
for (i = 0; i < k;i++)
{
prev[i][0] = arr[i][0];
prev[i][1] = arr[i][1];
}
if(kbhit())
{
getch();
direct = getch();
if(abs(direct1 - direct) == 8)
direct = direct1;
if(abs(direct1 - direct) == 2)
direct = direct1;
}
direct1 = direct;
delay(200/level);
switch(direct)
{
case 72 : arr[0][1]--;
break;
case 75 : arr[0][0]--;
break;
case 77 : arr[0][0]++;
break;
case 80 : arr[0][1]++;

break;
}
for(i = 1 ; i < k;i++)
if(arr[0][0] == arr[i][0] && arr[0][1] == arr[i][1])
{
outtextxy(500,300,"GAME OVER");
getch();
exit(0);
}
if(arr[0][0] == bx && arr[0][1] == by)
{
life = 0;
valid = 1;
score++;
if(bonus == 1)
{
bonus = 0;
count1 = 0;
count = 0;
score+=4;
setcolor(WHITE);
outtextxy(500,140,"");
}
k++;
setcolor(BLACK);
rectangle(bx*10,by*10,bx*10+10,by*10+10);
switch(direct)
{
case 72 : arr[0][1] = by -1;
break;
case 75 : arr[0][0] = bx -1;
break;
case 77 : arr[0][0] = bx +1;
break;
case 80 : arr[0][1] = by +1;
break;
}
}
for(i = 1 ; i < k;i++)
{
arr[i][0] = prev[i-1][0];
arr[i][1] = prev[i-1][1];

}
for(i = 0;i < k;i++)
{
if(arr[i][0] > 48)
arr[i][0] = 0;
if(arr[i][0] < 0)
arr[i][0] = 48;
if(arr[i][1] > 47)
arr[i][1] = 0;
if(arr[i][1] < 0)
arr[i][1] = 46;
}
gotoxy(72,13);
printf("%4d",score);
draw();
}
getch();
}

//////////////////////////// PROJECT ON
SNAKE ///////////////
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include<stdlib.h>
#include<time.h>
int arr[1000][2],prev[1000][2],k;
draw()
{
int i,x,y,x1,y1;
setcolor(8);
rectangle(0,0,490,480);
for ( i = 0 ; i < k;i++)
{
x = arr[i][0]*10;
y = arr[i][1]*10;
setcolor(11);
rectangle(x,y,x+10,y+10);
}
x1 = prev[k-1][0]*10;
y1 = prev[k-1][1]*10;
setcolor(BLACK);
rectangle(x1,y1,x1+10,y1+10);
}
main()
{
char direct = 77;
int gd = DETECT,gm,i,j,life,count,born,level,valid,bx,by,direct1,score = 0;
int bonus,count1;
k = 3;
for( i = 0 ; i < k;i++)
{
arr[i][0] = k-1-i;
arr[i][1] = 0;
}
clrscr();
initgraph(&gd,&gm,"e:\\proggy\\tc\\bgi");
setcolor(LIGHTMAGENTA);
outtextxy(150,20,"HAVE FUN WITH SNAKES");
outtextxy(10,30,"RULES TO PLAY (play using arrow keys)");
outtextxy(10,40,"GAME STARTS WITH A");

setcolor(11);
outtextxy(10,60,"SNAKE() WITH LENGTH THREE");
setcolor(LIGHTMAGENTA);
outtextxy(250,60,"GO AND EAT THE ");
setcolor(RED);
outtextxy(10,70,"RED COLOUR REFRESHING DIET()");
setcolor(LIGHTMAGENTA);
outtextxy(250,70,"LENGTH OF THE SNAKE INCREASES WITH MORE DIET");
setcolor(10);
outtextxy(10,80,"Press ESC to quit the game");
outtextxy(10,100,"GAME GETS OVER WHEN THE SNAKE DASHES ITSELF");
setcolor(7);
outtextxy(10,140,"GAME COPYRIGHT KAMAL PARYANI ");
outtextxy(10,150,"any suggestions or queries mail at kamal8182@yahoo.co.in");
getch();
initgraph(&gd,&gm,"e:\\proggy\\tc\\bgi");
printf("Select level : \n");
printf("\n4 -- excellent");
printf("\n3 -- good");
printf("\n2 -- moderate");
printf("\n1 -- beginner");
printf("\nENTER");
scanf("%d",&level);
initgraph(&gd,&gm,"e:\\proggy\\tc\\bgi");
// col = 1;
// for ( i = 0; i < k;i++)
count = 0;
setcolor(2);
outtextxy(500,196,"SCORE");
direct = 77;
life = 0;
randomize();
while(direct != 27)
{
if ( life == 0)
while(valid)
{
valid = 0;
bx = random(47);
by = random(46);
for(i = 0 ; i < k;i++)
if(bx == arr[i][0] && by == arr[i][1])
valid = 1;
}
life = 1;

setcolor(LIGHTMAGENTA);
if(count == 200)
{
bonus = 1;
count1 = 0;
setcolor(RED);
}
if(bonus)
{
count1++;
setcolor(RED);
outtextxy(500,140,"PROTIENS");
}
rectangle(bx*10,by*10,bx*10+10,by*10+10);
count++;
if(bonus)
count1++;
if(count1 == 40)
{
bonus = 0;
count1 = 0;
count = 0;
setcolor(WHITE);
outtextxy(500,140,"");
}
for (i = 0; i < k;i++)
{
prev[i][0] = arr[i][0];
prev[i][1] = arr[i][1];
}
if(kbhit())
{
getch();
direct = getch();
if(abs(direct1 - direct) == 8)
direct = direct1;
if(abs(direct1 - direct) == 2)
direct = direct1;
}
direct1 = direct;
delay(200/level);
switch(direct)
{
case 72 : arr[0][1]--;
break;

case 75 : arr[0][0]--;
break;
case 77 : arr[0][0]++;
break;
case 80 : arr[0][1]++;
break;
}
for(i = 1 ; i < k;i++)
if(arr[0][0] == arr[i][0] && arr[0][1] == arr[i][1])
{
outtextxy(500,300,"GAME OVER");
getch();
exit(0);
}
if(arr[0][0] == bx && arr[0][1] == by)
{
life = 0;
valid = 1;
score++;
if(bonus == 1)
{
bonus = 0;
count1 = 0;
count = 0;
score+=4;
setcolor(WHITE);
outtextxy(500,140,"");
}
k++;
setcolor(BLACK);
rectangle(bx*10,by*10,bx*10+10,by*10+10);
switch(direct)
{
case 72 : arr[0][1] = by -1;
break;
case 75 : arr[0][0] = bx -1;
break;
case 77 : arr[0][0] = bx +1;
break;
case 80 : arr[0][1] = by +1;
break;
}
}

for(i = 1 ; i < k;i++)


{
arr[i][0] = prev[i-1][0];
arr[i][1] = prev[i-1][1];
}
for(i = 0;i < k;i++)
{
if(arr[i][0] > 48)
arr[i][0] = 0;
if(arr[i][0] < 0)
arr[i][0] = 48;
if(arr[i][1] > 47)
arr[i][1] = 0;
if(arr[i][1] < 0)
arr[i][1] = 46;
}
gotoxy(72,13);
printf("%4d",score);
draw();
}
getch();
}

/*Program to implement scan-fill*/


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

#include<conio.h>
#include<dos.h>
union REGS i,o;
struct pt
{
int x,y;
};
float xl,xr,yt,yb;
int bc=0,xc,yc;
struct point
{
float x,y;
};
float m;
int n=0,k,dy,dx,x,y,temp,a[20][2],xi[20];
float slope[20];
initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}
showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
return(0);
}
hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
return(0);
}
void getmousepos(int *button,int *x,int *y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;

}
void scanfill()
{
int ik,j;
for(ik=0;ik<n;ik++)
{
dy=a[ik+1][1]-a[ik][1];
dx=a[ik+1][0]-a[ik][0];
if(dy==0) slope[ik]=1.0;
if(dx==0) slope[ik]=0.0;
if((dy!=0)&&(dx!=0))
{
slope[ik]=(float) dx/dy;
}
}
for(y=0;y<480;y++)
{
k=0;
for(ik=0;ik<n;ik++)
{
if( ((a[ik][1]<=y)&&(a[ik+1][1]>y))||((a[ik][1]>y)&&(a[ik+1][1]<=y)))
{
xi[k]=(int)(a[ik][0]+slope[ik]*(y-a[ik][1]));
k++;
}
}
for(j=0;j<k-1;j++) //- Arrange x-intersections in order
for(ik=0;ik<k-1;ik++)
{
if(xi[ik]>xi[ik+1])
{
temp=xi[ik];
xi[ik]=xi[ik+1];
xi[ik+1]=temp;
}
}
setcolor(15);
for(ik=0;ik<k;ik+=2)
{
line(xi[ik],y,xi[ik+1]+1,y);
delay(10);

}
}//for(y)
}
void main()
{
int cou,i,gd=DETECT,gm,flag;
struct point p[30],pout[30],z;
initgraph(&gd,&gm,"");
cleardevice();
showmouseptr();
while(bc!=2) //poly
{
getmousepos(&bc,&xc,&yc);
if(bc==1)
{
p[n].x=xc;
p[n].y=yc;
n++;
hidemouseptr();
if(n>1)
line(p[n-2].x,p[n-2].y,xc,yc);
showmouseptr();
delay(200);
}
}
p[n]=p[0];
hidemouseptr();
line(p[n-1].x,p[n-1].y,p[n].x,p[n].y);
showmouseptr();
getmousepos(&bc,&xc,&yc);
//flag=1;
bc=0;
hidemouseptr();
cleardevice();
for(i=0;i<n;i++)
line(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
for(i=0;i<=n;i++)
{
a[i][0]=p[i].x;
a[i][1]=p[i].y;
}
getch();

getch();
scanfill();
getch();
}