using namespace std; #include "asn2.h" #include // Function to make a color based on RGB values: Glib::RefPtr darea::makecolorgc(short r, short g, short b) { Glib::RefPtr win = get_window(); Glib::RefPtr some_gc = Gdk::GC::create(win); Gdk::Color some_color; Glib::RefPtr some_colormap = get_default_colormap(); some_color.set_red(r); some_color.set_green(g); some_color.set_blue(b); some_colormap->alloc_color(some_color); some_gc->set_foreground(some_color); return some_gc; } // Function to display text void darea::displaytext(const char *c, Glib::RefPtr colorgc, int x, int y ) { Glib::RefPtr win = get_window(); Glib::RefPtr pangolayout = create_pango_layout(c); win->draw_layout(colorgc, x, y, pangolayout); } // function to force a s second delay (controls animation speed) void delayforsecs(double s) { int x = 3; Glib::Timer t; t.start(); while (t.elapsed()get_black_gc(); whitegc = get_style()->get_white_gc(); ggc = makecolorgc(0,65535,0); // Green GC rgc = makecolorgc(65535,0,0); // Red GC bgc = makecolorgc(0,0,65535); // Blue GC dbuffer->draw_rectangle(blackgc, true, 0, 0, width, height); box.drawbox(dbuffer,0,0,0,799,599,399,rgc); started = true; i = 0; ///// you may want to initialize your body objects here } // iroutine // Main event loop: bool darea::on_expose_event(GdkEventExpose *event) { if (!started) iroutine(); animate(); return true; } // Called during each execution of on_expose_event. Each call to animate // should produce one complete frame of animation. void darea::animate() { delayforsecs(1.0); // 1 second delay between frames // clear and draw static background: dbuffer->draw_rectangle(blackgc, true, 0, 0, width, height); box.drawbox(dbuffer,0,0,0,799,599,399,bgc); ////////////////// Do some drawings: box.drawline(dbuffer,0,0,0,500,500,350,rgc); // red line box.drawcircle(dbuffer, false, 280, 120, 150,40,bgc); // blue outline box.drawcircle(dbuffer, true, 80, 120, 15+i, 50,ggc); // true=filled i += 10; // moves circle on next frame (i is non-local var) ///////////////// // draw buffer to screen: win->draw_drawable(blackgc,dbuffer,0,0,0,0,width,height); // "invalidate" signals the window app to redraw itself: Gdk::Rectangle r(0,0,width,height); win->invalidate_rect(r,true); } // onexposeevent void body::calcforce(body *B) // effect of B on this body { double d2, dx, dy, dist, sm, sz, dz; double accAX, accAY, accAZ, deeper; // accelerations dx = B->xcord - xcord; dy = B->ycord - ycord; dz = B->zcord - zcord; d2 = (dx*dx) + (dy*dy) + (dz*dz); if (d2==0) d2=1e-100; // avoid division by zero dist = sqrt(d2); // distance // scale into unit vector dx = dx/dist; dy = dy/dist; dz = dz/dist; // dx,dy,dz now represents the direction of acceleration // calculate acceleration vector: accAX = dx * (G*B->mass)/d2; accAY = dy * (G*B->mass)/d2; accAZ = dz * (G*B->mass)/d2; // change velocity vector according to acceleration: xvel += accAX * UTIME; yvel += accAY * UTIME; zvel += accAZ * UTIME; // position coords will be changed during simulation, since // we still have to calculate the effect of other bodies on this one } // calcforce /* compile: g++ boxworld.o asn2.cpp -o gravity `pkg-config gtkmm-2.0 --cflags --libs` */