Search This Blog

C Program to Animate a Man Drawing Water From Well

This is a c program to animate a man drawing water from well. This is computer graphics lab experiment. The program uses graphics library in c (graphics.h). The output is as follows:


 The program is as follows:

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

int fill=0,inlen=15,dir=1;

void drawMan(int x,int y)
{
static int q=1;
setcolor(YELLOW);
setfillstyle(EMPTY_FILL,BLACK);
fillellipse(x+100,y-50,10,10);
line(x+100,y-40,x+100,y+20);
line(x+100,y+20,x+85,y+60);
line(x+100,y+20,x+115,y+60);
//arms
if(q%2)
 {
 line(x+100,y-20,x+80,y-30);
 line(x+100,y-25,x+85,y-20);
 }
else
 {
 line(x+100,y-20,x+80,y-20);
 line(x+100,y-25,x+85,y-30);
 }
q++;

}

void drawBucket(int x, int y,int fill)
{
setcolor(RED);
line(x-10,y,x+10,y);
line(x-10,y,x-7,y+15);
line(x-7,y+15,x+7,y+15);
line(x+7,y+15,x+10,y);
if(fill==0)
 setfillstyle(EMPTY_FILL,BLACK);
else if(fill==1)
 setfillstyle(SOLID_FILL,3);
fillellipse(x,y,10,3);
}


void drawWell(int x,int y,int deg)
{
int ww=70,wh=20,yt,tx,ty,ropelen=200;
float rad1,rad2,cos45;
int i,t;
setcolor(BROWN);
cos45=cos(45*3.14f/180);

//lines
line(x-ww,y+2*wh,x-ww,y-120);
line(x-ww+15,y+2*wh,x-ww+15,y-120);
line(x+ww,y+2*wh,x+ww,y-120);
line(x+ww-15,y+2*wh,x+ww-15,y-120);
line(x-ww,y-120,x+ww,y-120);
line(x-ww,y-110,x+ww,y-110);
line(x,y-115,x,y-100);

//kappi
setfillstyle(SOLID_FILL,LIGHTGRAY);
fillellipse(x,y-95,10,10);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(x,y-95,2,2);
rad1=deg*3.14f/180;
rad2=(deg+90)*3.14f/180;
yt=y-95;
setcolor(RED);
//rotation of pulley
line(x,yt,x+10*sin(rad1),yt+10*cos(rad1));
line(x,yt,x+10*sin(rad2),yt+10*cos(rad2));
line(x,yt,x-10*sin(rad1),yt-10*cos(rad1));
line(x,yt,x-10*sin(rad2),yt-10*cos(rad2));
//ropeover well
line(x-10,yt,x-10,yt+inlen);
//rope in hands
t=(ropelen-inlen)*cos45;
line(x+10,yt,x+10+t,yt+t);
//draw bucket if not inside well
if(!(yt+inlen+15>=y+wh-60))
 drawBucket(x-10,yt+inlen,fill);
inlen=inlen+dir*3;


//ellipses
setcolor(BROWN);
setfillstyle(SOLID_FILL,DARKGRAY);
for(i=0;i<=60;i+=15)
 fillellipse(x,y+2*wh-i,ww,wh);
setfillstyle(SOLID_FILL,3);
fillellipse(x,y+2*wh-60,ww-15,wh-8);
if(yt+inlen+15>=y+wh-60&&yt+inlen+7<y+2*wh-60)
 drawBucket(x-10,yt+inlen,fill);
if(yt+inlen+7>=y+3*wh-60)
  {
  dir*=-1;
  fill=1;
  }
}

void main()
{
int gd=DETECT,gm,x,y,deg=30;
initgraph(&gd,&gm,"..\\BGI");
x=getmaxx()/2;
y=getmaxy()/2+40;

while(!kbhit())
{
drawWell(x,y,deg);
drawMan(x,y);
deg=deg+dir*25;
delay(350);
cleardevice();
}
getch();
}

No comments: