Thursday, November 18, 2010

Catching a fish in C

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void fish();
void fishcatch();
int fishx=0,fishy=460,fishang=0,tempx,tempy,fishx2=350,fishy2=400;
void main()
{
int gdriver = DETECT, gmode, errorcode;
int x,y,ch,yincr=0;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
x=350; y=240;

while(!kbhit())
{
rectangle(-1,240,641,481);
setfillstyle(1,BLUE);
floodfill(1,241,15);
line(x,y,x+100,y);
line(x+100,y,x+130,y-20);
line(x,y,x-30,y-20);
line(x-30,y-20,x+130,y-20);
setfillstyle(1,BROWN);
floodfill(x+1,y-1,15);
arc(x+50,y-20,0,180,15);
setfillstyle(1,RED);
floodfill(x+50,y-21,15);
circle(x+50,y-40,6);
setfillstyle(1,LIGHTGRAY);
floodfill(x+50,y-41,15);
delay(50);
cleardevice();
if(x>=300)
x=x-1;
else if(y+yincr<=400)
{
if(fishx<330)
yincr++;
}
line(x+40,y,x+50,y+yincr);

if(x<=350)
{
fishx++;
if(fishx>=330)
{
if(yincr>=0)
{
fishcatch();
fishy2--;
yincr--;
}
else
{
cleardevice();
printf("Fish curry: Rs.15/-");
getch();
exit(0);
}
}
else
{
fish();

if(y+yincr>=390)
fishy=fishy-0.5;

if(fishy<=400)
fishy=400;

if(fishx>=330)
fishx=330;
}
}

}


if(kbhit())
{
//printf("%c",getch());
}
getch();
}


void fish()
{
line(fishx,fishy,fishx+10,fishy-5);
line(fishx,fishy,fishx+10,fishy+5);
line(fishx+10,fishy-5,fishx+20,fishy);
line(fishx+20,fishy,fishx+10,fishy+5);
line(fishx,fishy,fishx-3,fishy-3);
line(fishx-3,fishy-3,fishx-3,fishy+3);
line(fishx-3,fishy+3,fishx,fishy);
setfillstyle(1,BROWN);
floodfill(fishx+2,fishy,15);
floodfill(fishx-1,fishy,15);
}

void fishcatch()
{
line(fishx2,fishy2,fishx2+5,fishy2+10);
line(fishx2,fishy2,fishx2-5,fishy2+10);
line(fishx2+5,fishy2+10,fishx2,fishy2+20);
line(fishx2-5,fishy2+10,fishx2,fishy2+20);
line(fishx2,fishy2,fishx2+3,fishy2+23);
line(fishx2,fishy2,fishx2-3,fishy2+23);
line(fishx2+3,fishy2+23,fishx2-3,fishy2+23);
setfillstyle(1,BROWN);
floodfill(fishx2,fishy2+2,15);
floodfill(fishx2,fishy2+22,15);
}

Sinking of Titanic

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void titanic(int,int);
void titanics(int,int);
void iceberg();
int poly[]={0,300,640,300,640,480,0,480,0,300};
int ix=1,iy=1;
void main()
{
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
int xincr=1,yincr=1,i;
int tempx=400,tempy=300;
int sink=0;
initgraph(&gdriver, &gmode, "c:/tc/bgi");

errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

while(1)
{
delay(30);
cleardevice();
iceberg();

if(sink==1)
{
tempy+=yincr;
}
else
{
tempx-=xincr;
}

if(tempx<=120)
{
sink=1;
titanics(tempx,tempy);
}
else
{
titanic(tempx,tempy);
}

if(tempy>=600)
break;
}
getch();
}

void titanic(int x,int y)
{
setcolor(15);
line(x,y,x+100,y);
line(x,y,x-20,y-20);
line(x-20,y-20,x+120,y-20);
line(x+120,y-20,x+100,y);
rectangle(x+10,y-30,x+90,y-20);
rectangle(x+20,y-40,x+80,y-30);
rectangle(x+60,y-70,x+70,y-40);
}

void titanics(int x,int y)
{
setcolor(15);
line(x,y,x+100,y-10);
line(x,y,x-20,y-30);
line(x-20,y-20,x+120,y-30);
line(x+120,y-20,x+100,y-10);
rectangle(x+10,y-30,x+90,y-10);
rectangle(x+20,y-40,x+80,y-10);
rectangle(x+60,y-70,x+70,y-10);
}

void iceberg()
{
setcolor(BLUE);
setfillstyle(1,BLUE);
fillpoly(4,poly);
setfillstyle(1,WHITE);
fillellipse(0,300,100,100);
}

Sunset in C

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT, gmode, errorcode;
int midx,midy,radius=40;
initgraph(&gdriver, &gmode, "c:/tc/bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
while(radius>=10)
{
cleardevice();
line(0,210,160,50);
setcolor(WHITE);
line(160,50,320,210);
line(320,210,480,50);
line(480,50,640,210);
line(0,210,640,210);


arc(midx,210,45,135,radius);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(midx,200,15);
setcolor(BLUE);
setfillstyle(SOLID_FILL, LIGHTBLUE);
floodfill(2,2,15);
setfillstyle(SOLID_FILL, BROWN);
floodfill(150,150,15);
floodfill(550,150,15);
setcolor(15);
line(0,400,640,300);
setfillstyle(SOLID_FILL, BLUE);
floodfill(2,350,15);
setfillstyle(SOLID_FILL, GREEN);
floodfill(2,410,15);
delay(100);
radius-=1;
}
if(radius<=10)
cleardevice();
getch();
}

Rotation of Fan in C

#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
#include<process.h>
#include<math.h>
void user();
float s1=0.0,s2=120.0,s3=240.0;
int midx,midy;
void main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "C:/TC/BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
midy=getmaxy()/2;
loop:
cleardevice();
circle(midx,midy,5);
setcolor(10);
line(midx,midy,(midx+100*cos(s1*3.14/180)),(midy-95*sin(s1*3.14/180)));
line(midx,midy,(midx+100*cos(s2*3.14/180)),(midy-95*sin(s2*3.14/180)));
line(midx,midy,(midx+100*cos(s3*3.14/180)),(midy-95*sin(s3*3.14/180)));
if(!kbhit()) //check for keystroke
{
s1+=10;
s2+=10;
s3+=10;
delay(25);
goto loop;
}
else //when key press
{
getch();
exit(0);
}
getch();
closegraph();
}

Sunrise in C

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT, gmode, errorcode;
int midx,midy,radius=10;
initgraph(&gdriver, &gmode, "c:/tc/bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx=getmaxx()/2;
while(radius<=40)
{
cleardevice();
line(0,210,160,50);
setcolor(WHITE);
line(160,50,320,210);
line(320,210,480,50);
line(480,50,640,210);
line(0,210,640,210);

arc(midx,210,45,133,radius);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(midx,200,15);
setcolor(BLUE);
setfillstyle(SOLID_FILL, LIGHTBLUE);
floodfill(2,2,15);
setfillstyle(SOLID_FILL, BROWN);
floodfill(150,150,15);
floodfill(550,150,15);
setcolor(15);
line(0,400,640,300);
setfillstyle(SOLID_FILL, BLUE);
floodfill(2,350,15);
setfillstyle(SOLID_FILL, GREEN);
floodfill(2,410,15);
delay(100);
radius+=1;
}
getch();
}

Analog Clock in C

/*WAP to illustrate an analog clock*/
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
#include<process.h>
#include<math.h>
void user();
float h,ma,hour,minute,sec,m,s;
void main()
{
int gdriver = DETECT, gmode, errorcode;
struct time t;
initgraph(&gdriver, &gmode, "C:/TC/BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
loop:
gettime(&t);
hour=t.ti_hour;
minute=t.ti_min;
sec=t.ti_sec;
if(hour>11)
hour=hour-12;
h=90-(hour*30+minute/12);
ma=90-(minute*6);
s=90-(sec*6);
m=minute-minute/12;
cleardevice();
gotoxy(40,15);
printf("%2.0f:%2.0f:%2.0f",hour,minute,sec);
gotoxy(10,10);
setcolor(15);
circle(350,250,100);
setfillstyle(1,9);
circle(350,250,2);
circle(350,250,10);
outtextxy(335,200,"RADON");
setcolor(RED);
outtextxy(346,160,"12");
outtextxy(346,340,"6");
outtextxy(425,247,"3");
outtextxy(260,247,"9");
setcolor(7);
line(350,250,(350+70*cos(h*3.14/180)),(250-70*sin(h*3.14/180))); //hour hand
setcolor(10);
line(350,250,(350+90*cos(s*3.14/180)),(250-95*sin(s*3.14/180))); //second hand
setcolor(9);
line(350,250,(350+60*cos(ma*3.14/180)),(250-90*sin(ma*3.14/180))); //minute hand
setcolor(8);
if(sec>59)
{
sec=0;
m++;
m=6;
s=s-90;
if(m>59)
{
m=0;
h=6;
}
}
if(!kbhit()) //check for keystroke
{
sec++;
s=s-6;
delay(1000);
goto loop;
}
else //when key press
{
getch();
exit(0);
}
getch();
closegraph();
}

Mid-point Ellipse Drawing Algorithm

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void ellipsemid(int xcenter,int ycenter,int rx,int ry);
void point(int x,int xcenter,int ycenter,int y);
int maxx,maxy,xcenter,ycenter,rx,ry,grok;

void main()
{
int gdriver = DETECT, errorcode, gmode, a,b,c,d;

initgraph(&gdriver, &gmode, "c:/tc/bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

clrscr();
maxx=getmaxx();
maxy=getmaxy();
xcenter=200;
ycenter=200;
rx=50;
ry=50;
setcolor(4);
ellipsemid(xcenter,ycenter,rx,ry);
getch();
//closegraph();
}


void ellipsemid(int xcenter,int ycenter,int rx,int ry)
{
int p,px,py,x,y,ry2,rx2,tworx2,twory2;
ry2=ry*ry;
rx2=rx*rx;
twory2=2*ry2;
tworx2=2*rx2;
x=0;
y=ry;
point(x,xcenter,ycenter,y);
p=abs(ry2-ry2*ry+(0.25*rx2));
px=0;
py=twory2*y;
while(px<py)
{
x=x+1;
px=px+twory2;
if(p>=0)
{
y=y-1;
py=py-tworx2;
}
if(p<0)
{
p=p+ry2+px;
}
else
{
p=p+ry2+px;
}
point(x,xcenter,ycenter,y);
}
p=abs(ry2*(x+6.5)*(x+6.5)+rx2*(y-1)-rx2*ry2);
while(y>0)
{
y=y-1;
py=py-tworx2;
if(p<0)
{
x=x+1;
px=px+twory2;
}
if(p>0)
{
p=p+rx2-py;
}
else
{
p=p+rx2-py+px;
point(x,xcenter,ycenter,y);
}
}
}
void point(int x,int xcenter,int ycenter,int y)
{
setcolor(4);
putpixel(xcenter+x,ycenter+y,4);
putpixel(xcenter-x,ycenter+y,4);
putpixel(xcenter+x,ycenter-y,4);
putpixel(xcenter-x,ycenter-y,4);
putpixel(xcenter+x,ycenter+y,4);
putpixel(xcenter-x,ycenter+y,4);
putpixel(xcenter+x,ycenter-y,4);
putpixel(xcenter-x,ycenter-y,4);
}

Line Clipping in C

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int xwmin,ywmin,xwmax,ywmax;
void rcode(int,int,int []);
int check(int[],int[]);
void main()
{
int gdriver = DETECT, gmode, errorcode;
int x1,y1,x2,y2,code1[4],code2[4],x,y,m;
int point[2][2],p=0;
initgraph(&gdriver, &gmode, "c:/tc/bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
printf("\n Enter the coordinates of window ");
printf("\n xwmin and ywmin: ");
scanf("%d%d",&xwmin,&ywmin);
printf("\n xwmax and ywmax: ");
scanf("%d%d",&xwmax,&ywmax);
printf("\n Enter the coordinates of line ");
printf("\n Enter the coordinate for the first end point of the line: ");
scanf("%d%d",&x1,&y1);
printf("\n Enter the coordinate for the second end point of the line: ");
scanf("%d%d",&x2,&y2);
rcode(x1,y1,code1);
rcode(x2,y2,code2);

if(check(code1,code2)==1)
{
rectangle(xwmin,ywmin,xwmax,ywmax);
printf("1");
}
else
{
m=(y2-y1)/(x2-x1);
rectangle(xwmin,ywmin,xwmax,ywmax);
line(x1,y1,x2,y2);
setcolor(4);
if(x1<xwmin)
{
y=y1+m*(xwmin-x1);
x=xwmin;
x1=x;
y1=y;
}
if(x2>xwmax)
{
y=y2+m*(xwmax-x2);
x=xwmax;
x2=x;
y2=y;
}
if(y1<ywmin)
{
x=x1+(ywmin-y1)/m;
y=ywmin;
x1=x;
y1=y;
}
if(y2>ywmax)
{
x=x2+(ywmax-y2)/m;
y=ywmax;
x2=x;
y2=y;
}


line(x1,y1,x2,y2);

}
getch();
}

int check(int code1[],int code2[])
{
int flag=0;
if(code1[3]==1&&code2[3]==1)
flag=1;
if(code1[2]==1&&code2[2]==1)
flag=1;
if(code1[1]==1&&code2[1]==1)
flag=1;
if(code1[0]==1&&code2[0]==1)
flag=1;

return flag;
}

void rcode(int x,int y,int code[])
{
if(y>ywmax)//bottom
code[2]=1;
else
code[2]=0;
if(y<ywmin)//top
code[3]=1;
else
code[3]=0;
if(x>xwmax)//right
code[1]=1;
else
code[1]=0;
if(x<xwmin)//left
code[0]=1;
else
code[0]=0;
}

Contact Form

Name

Email *

Message *

The Insane Techie - Android App

Launched an android app for the blog on 07th June 2016. Get it from google play store... Tips for using the app Use in landscape mo...