Thursday, October 28, 2010

Graphics program for Bouncing-ball

#include"stdio.h"

#include"math.h"

#include"graphics.h"



#define ROUND(a) ((int) (a+0.5))

void paracircle(int xcen, int ycen, int r);

//////////////////////////////////////////////////////////////////////

void ddaline2(int xa, int ya, int xb, int yb)

{

int dx = xb - xa, dy = yb - ya, steps, k ;

float xinc,yinc,x = xa,y = ya;

if (abs (dx) > abs (dy) )

steps = abs(dx);

else

steps = abs(dy);

xinc = dx/(float) steps;

yinc = dy/(float) steps;

putpixel( ROUND(x), ROUND(y),RED);

for(k = 0; k < steps ; k++)

{

x += xinc;

y += yinc;

putpixel(ROUND(x),ROUND(y),BROWN);

}

}

//////////////////////////////////////////////////////////////////////

void ddaline(int xa, int ya, int xb, int yb)

{

int dx = xb - xa, dy = yb - ya, steps, k ;

float xinc,yinc,x = xa,y = ya;

if (abs (dx) > abs (dy) )

steps = abs(dx);

else

steps = abs(dy);

xinc = dx/(float) steps;

yinc = dy/(float) steps;

putpixel( ROUND(x), ROUND(y),RED);

for(k = 0; k < steps ; k++)

{

x += xinc;

y += yinc;

ddaline2(0,400,getmaxx(),400);

paracircle(ROUND(x),ROUND(y),30);

delay(5);

if(k % 2 == 0)

cleardevice();

}

}

///////////////////////////////////////////////////////////////////

void paracircle(int xcen, int ycen, int r)

{

float x = 0;

float y;

int theta = 0;

static int i;

for(; theta <= 360; theta++)

{

x = xcen + r * cos( (theta * 3.14) / 180);

y = ycen + r * sin( (theta * 3.14) / 180);

putpixel( (int)x,(int)y, BROWN);

}

putpixel(xcen,ycen,RED);

ddaline2(xcen,ycen,xcen + r*cos((i+120)*3.1415/180),ycen + r*sin((i+120)*3.1415/180) );

ddaline2(xcen,ycen,xcen + r*cos((i+240)*3.1415/180),ycen + r*sin((i+240)*3.1415/180) );

ddaline2(xcen,ycen,xcen + r*cos((i+360)*3.1415/180),ycen + r*sin((i+360)*3.1415/180) );

i++;

}

///////////////////////////////////////////////////////////////////////////

void main()

{

int i,j;

int gd = DETECT,gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");



ddaline(30,30,180,370);

ddaline(180,370,330,130);

ddaline(330,130,480,370);

ddaline(480,370,550,230);

ddaline(550,230,600,370);

ddaline(600,370,getmaxx(),370);

getch();

closegraph();

}

No comments: