#include #include #include #include #include #define WIDTH 500 #define HEIGHT 500 #define NPTS 500 #define PI 3.14159265359879323846 const int R = 30; const int MAX_X = WIDTH-2*R; const int MIN_X = 2*R; const int MAX_Y = HEIGHT-2*R; const int MIN_Y = 2*2; const double THETA = PI/4; const double T = .25; double X0 = 10; double Y0 = 400; double V0 = 20.3; double F = 5; double g = -9.8; double t = 0; double px = X0; double py = Y0; double vy = V0*sin(THETA); double vx = V0*cos(THETA); bool end = false; Display *D; int S,Loop,Point; Window W; GC Gc,GcRev; XEvent Event; XPoint PointBuf[1000],OldPointBuf[1000]; int u_sleep(unsigned int usec); void get_coord(int& x, int &y); main() { int x, y, oldx, oldy; D = XOpenDisplay(""); S = DefaultScreen(D); W = XCreateSimpleWindow(D,DefaultRootWindow(D),0,0,WIDTH,HEIGHT,1, WhitePixel(D,S),WhitePixel(D,S)); XStoreName(D,W,"Bouncing Ball"); XMapRaised(D,W); Gc = XCreateGC(D,W,0L,(XGCValues *) 0); XSetForeground(D,Gc,BlackPixel(D,S)); XSetBackground(D,Gc,WhitePixel(D,S)); XSetLineAttributes(D,Gc,5,LineSolid,CapButt,JoinMiter); GcRev = XCreateGC(D,W,0L,(XGCValues *) 0); XSetForeground(D,GcRev,WhitePixel(D,S)); XSetBackground(D,GcRev,BlackPixel(D,S)); XSetLineAttributes(D,GcRev,5,LineSolid,CapButt,JoinMiter); while (!end) { oldx = x; oldy = y; vy = vy + g*T; px = px + vx; py = py + vy; if (py <= 0 || py >= MAX_Y) { if (vy != 0) { if (vy < F && vy > -F) { vy = 0; } else { vy = vy > 0 ? vy-F : vy+F; vy = -vy; } } } if (px <= 0 || px >= MAX_X) { if (vx != 0) { if (vx < F && vx > -F) { vx = 0; } else { vx = vx > 0 ? vx-F : vx+F; vx = -vx; } } } if (px > MAX_X) { px = MAX_X; } else if (px < 0) { px = 0; } if (py > MAX_Y) { py = MAX_Y; } else if (py < 0) { py = 0; } get_coord(x,y); XFillArc(D,W,GcRev,oldx,oldy,R*2,R*2,0,5760*4); XFlush(D); XFillArc(D,W,Gc,x,y,R*2,R*2,0,5760*4); XFlush(D); u_sleep(1000); t+=T; } } void get_coord(int& x, int& y) { x = (int) px; y = MAX_Y - (int) py; } int u_sleep(unsigned int usec) { static int subtotal = 0; int msec; struct pollfd foo; subtotal += usec; if (subtotal < 1000) return (0); msec = subtotal/1000; return poll(&foo,(unsigned long)0, msec); }