Tuesday 26 June 2012

c++ program to display walking man on a display.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
struct point
{
   float x,y;
} head;
typedef struct point p;
struct cnate
{    p p1;
     p p2;
}lhand,rhand,lleg,rleg,body;
typedef struct cnate c;
void draw(c temp)
{line(temp.p1.x,temp.p1.y,temp.p2.x,temp.p2.y);
  return;
}



c rotate(c temp,float angle)
{
   float ox,oy,c,s;
   ox=temp.p2.x;
   oy=temp.p2.y;
   angle=angle*3.14/180;
   c=cos(angle);
   s=sin(angle);
   temp.p2.x=ox*c-oy*s-temp.p1.x*c+temp.p1.y*s+temp.p1.x;
   temp.p2.y=ox*s+oy*c-temp.p1.x*s-temp.p1.y*c+temp.p1.y;
   return temp;
}
   c scale(c temp,float inc)
{
    temp.p1.x+=inc;
    temp.p2.x+=inc;
    return temp;
}
    void circ(p temp)
{   circle(temp.x,temp.y,20);
    return;
}
p scale1(p temp,float inc)
{
temp.x+=inc;
return temp;
}
void main(void)
{int gd=DETECT,gm,a1=3,cnt=0,a2=5;
float inc=3;
initgraph(&gd,&gm,"c:\\tc\\bgi");
head.x=30,head.y=350;
body.p1.x=30,body.p1.y=370,body.p2.x=30,body.p2.y=450;
lhand.p1.x=30,lhand.p1.y=375,lhand.p2.x=5,lhand.p2.y=400;
rhand.p1.x=30,rhand.p1.y=375,rhand.p2.x=55,rhand.p2.y=400;
lleg.p1.x=30,lleg.p1.y=450,lleg.p2.x=5,lleg.p2.y=475;
rleg.p1.x=30,rleg.p1.y=450,rleg.p2.x=55,rleg.p2.y=475;
while(!kbhit())
{flushall();
setcolor(0);
circ(head);
draw(body);
draw(lhand);
draw(rhand);
draw(lleg);
draw(rleg);
setcolor(14);
outtextxy(200,50,".....Walking man.....");
setcolor(15);
lhand=rotate(lhand,-a1);
rhand=rotate(rhand,a1);
lleg=rotate(lleg,-a2);
rleg=rotate(rleg,a2);
body=scale(body,inc);
lhand=scale(lhand,inc);
rhand=scale(rhand,inc);
lleg=scale(lleg,inc);
rleg=scale(rleg,inc);
head=scale1(head,inc);
circ(head);
draw(body);
draw(lhand);
draw(rhand);
draw(lleg);
draw(rleg);
cnt++;
delay(40);
if(cnt==7)
{
cnt=0;
a1=-a1;
a2=-a2;
}
if(rhand.p1.x>=600 || lhand.p1.x<=15)
inc=-inc;
}
closegraph();
}

No comments:

Post a Comment