Thursday, 9 May 2013

Bouncing Ball


#include<stdio.h>
#include<graphics.h>
#include<math.h>
#define ROUND(a)((int)(a+0.5))
void paraCircle(int,int,int,int);
int base=400;
int color=15;
void parabola()
{
 int xo=70,x=0,yo=base,theta=0;
 float vh=10.0,vy=80.0;
 int vhit=0;
 int y=0;
 float t;
 int dly=40;
 int xmax=getmaxx();
 outtextxy(20,50,"Press any key to throw");
 paraCircle(xo,yo,10,theta);
 line(0,base+10,xmax-20,base+10);
 line(xmax-20,0,xmax-20,base+10);
 getch();
 do
 {
  theta=0;
  paraCircle(xo,yo,10,theta);
  for(t=1;;t+=.2)
  {
   x=ROUND(xo+(vh*t));
   y=ROUND(yo-(vy*t)+5*(t*t));
   line(0,base+10,xmax-20,base+10);
   line(xmax-20,0,xmax-20,base+10);
   paraCircle(x,y,10,theta);
   delay(dly);
   cleardevice();
   if(x==xmax-30)
   {
    vh=-vh;
    vhit=1;
   }
   if(vhit==0)
    theta+=10;
   else  
    theta-=10;
   if(y==base)
    break;
  }
  vy-=5;
  if(vhit==0)
   vh-=1;
  else if(vhit==1)
   vh+=1;
  dly-=4;
  paraCircle(x,y,10,theta);
  xo=x;
 }while(vy>0&&vh!=0);
 line(0,base+10,xmax-20,base+10);
 line(xmax-20,0,xmax-20,base+10);
}
void paraCircle(int xc,int yc,int r,int t)
{
 int i,x,y;
 for(i=0;i<90;i++)
 {
  x=xc+ROUND(r*cos(3.1412*(i+t)/180));
  y=yc+ROUND(r*sin(3.1412*(i+t)/180));
  putpixel(x,y,5);
 }
 for(i=90;i<180;i++)
 {
  x=xc+ROUND(r*cos(3.1412*(i+t)/180));
  y=yc+ROUND(r*sin(3.1412*(i+t)/180));
  putpixel(x,y,2);
 }
 for(i=180;i<270;i++)
 {
  x=xc+ROUND(r*cos(3.1412*(i+t)/180));
  y=yc+ROUND(r*sin(3.1412*(i+t)/180));
  putpixel(x,y,12);
 }
 for(i=270;i<360;i++)
 {
  x=xc+ROUND(r*cos(3.1412*(i+t)/180));
  y=yc+ROUND(r*sin(3.1412*(i+t)/180));
  putpixel(x,y,7);
 }
}
int main()
{
 int gd=DETECT,gm=VGAMAX;
 initgraph(&gd,&gm,"NULL");
 parabola();
 while(!kbhit());
 closegraph();
 return 0;
}

No comments:

Post a Comment