// Gravity. // bam:9904 String title= "Gravity"; String author= "B.A.Martin"; String sub= "Press space-bar to advance time"; String subsub= "(or press keys 1, 2, 3, etc.)"; // Time, distance, velocity, acceleration int time=13; float d=0, v=0, a=10, v0=0, vf=0, vavg=0, dlast=0; float xo=135, xt=150, xv=200, xvavg=250, xd=300, xdrop=350; // Building size float xb=20, wide=100, tall=850, roof=60, bottom=roof+tall; color RED=color(255,0,0), GREEN=color(0,255,0), BLUE=color(0,0,255); void setup() { size( 600, 1000 ); } void draw() { scene(); titles(); action(); } void action() { // Labels above fill(BLUE); textSize(10); text( "time", xt, roof-10 ); text( "velocity", xv, roof-10 ); text( "v-avg", xvavg, roof-10 ); text( "distance", xd, roof-10 ); text( "drop", xdrop, roof-10 ); // Show values for each second of falling if (time==0) { show(0); return; } dlast=0; line( xb+wide+5, roof, xb+wide+280, roof ); for (int t=1; t<=time; t++ ) { show( t ); } } // Display object and values at time t // void show( float t ) { vf= a * t; vavg= (v0+vf) / 2; d= vavg * t; float y= roof + d +12; // fill(RED); ellipse( xo, y-6, 8,8 ); // Red ball. stroke(RED); float speed= 5 * vf/10; line( xo-3, y-6, xo, y-6-speed ); line( xo, y-6, xo, y-6-speed ); line( xo+3, y-6, xo, y-6-speed ); // stroke(200); fill(0); line( xb+wide+5, y-12, xb+wide+260, y-12 ); fill(0); textSize(12); text( ""+(int)t+" s", xt, y ); /* text( ""+(int)vf, xv, y ); text( ""+(int)vavg, xvavg, y ); text( ""+(int)d, xd, y ); text( ""+(int)(d-dlast), xdrop, y ); */ text( "__ m/s", xv, y ); text( "__ m/s", xvavg, y ); text( "__ m", xd, y ); text( "__ m", xdrop, y ); dlast= d; } void scene() { background( 200, 220, 255 ); fill(200,100,100); rect( xb, roof, wide, tall ); // Building stroke(0,150,0); line( xb, bottom, width, bottom ); stroke(0); } void titles() { fill(0); textSize(20); text( title, xb+20, 20 ); textSize(12); text( sub, xt, 12 ); fill( 150 ); text( subsub, xt, 24 ); text( author, 20, height-20 ); report(); } void report() { int xr=width*3/4, y=50, n=0, h=15; fill(0,0,255); text( "time: "+time, xr, y+h*n++ ); // Next line. text( "distance: "+d, xr, y+h*n++ ); text( "velocity: "+vf, xr, y+h*n++ ); text( "acceleration: "+a, xr, y+h*n++ ); } void keyPressed() { if (key == ' ') ++time; else if (key >= '0' && key <= '9') time= key-'0'; }