//////// Planets orbit the sun. (bam/CST112) //////// Planet[] p; int np=0; float textX=10,textY=10; int textDY=0; float GM=10000; float sunX, sunY; float sunR; boolean clockwise=true; boolean debug, help=true, lines, tails=true, names=true, slow, quit, xyflag; // (Default values = false. int back=64; void setup() { size(1000,800); sunX= width/2; sunY= height/2; sunR= 4; p= new Planet[200]; // Array of planets. } void restart() { //// Re-initialize. np=0; help= true; names= true; tails= true; } void draw() { //// Draw the next frame //// background ( back ); // background( 0 ); fill(255,155,0); text( "P L A N E T S", width/2, 20); if (np<1) { text( "Click for new planets;", width/2, 40); text( "or enter p for", width/2, 50); text( "Solar System", width/2, 60); } if ( quit ) tryQuit(); frameRate( slow ? 2 : 30 ); fill(0); text( np+" planets", width-100, 20 ); textDY=0; int bugY=0; if (help) help( width-200, 50 ); sun(); // Planets -- display each one. for( int i=0; i 64 ? back - 16 : back / 2) ; } // Darken the background. if (key == 'd') debug= ! debug; // Toggle debugging flag. if (key == 'f') slow= ! slow; // Toggle slow flag. if (key == 'F') frameRate(.2); // Freeze! if (key == 'l') lines= ! lines; // Toggle lines flag. if (key == 'n') names= ! names; // Toggle names flag. if (key == 't') tails= ! tails; // Toggle tails flag. if (key == 'x') xyflag= ! xyflag; // Toggle xyflag flag. if (key=='P' || key=='p') solarPlanets(); if (key=='C' || key=='c') clockwise = ! clockwise; if (key=='R' || key=='r') { restart(); } if (key=='Q' || key=='q') { if (quit) exit(); else quit = ! quit; } } void help( float x, float y ) { //// Display help messages int dy=0; fill(255,155,0); text( "? Help", x, y+15*dy++ ); text( "b background darkens.", x, y+15*dy++ ); text( "c counterclockwise.", x, y+15*dy++ ); text( "d Debugging", x, y+15*dy++ ); text( "f frame rate (2 per second)", x, y+15*dy++ ); text( "F Freeze (5 seconds)", x, y+15*dy++ ); text( "l Lines (from sun to planet)", x, y+15*dy++ ); text( "n names (displayed with planet)", x, y+15*dy++ ); text( "t tails (from previous position)", x, y+15*dy++ ); text( "x xyflag (create planets from (x,y) rather than r,theta)", x, y+15*dy++ ); text( "----", x, y+15*dy++ ); text( "P Populate with solar planets", x, y+15*dy++ ); text( "R Restart", x, y+15*dy++ ); text( "Q Quit", x, y+15*dy++ ); // text( "", x, y+15*dy++ ); text( "Click to create a new planet", x, y+15*dy++ ); } void sun() { //// Draw the sun; fill( 255,255,0); ellipseMode(RADIUS); ellipse( sunX, sunY, sunR, sunR ); } void nextline( String s ) { //// Display text on next available line. text( s, textX, textY+15*textDY++ ); } void createPlanet( float x, float y) { //// Create a new planet at x,y if (np >= p.length) { background( 255 ); String s="TOO MANY PLANETS! Press R key to reset."; text( s, 10,10 ); text( s, 10, height/4 ); text( s, 10, height/2 ); fill(0); text( s, 10,30 ); text( s, width/2, height/4 ); text( s, width/2, height/2 ); return; } float r= dist(x,y, sunX,sunY); if (xyflag) { p[np]= new Planet( x,y ); } else { float theta= atan( (y-sunY) / (x-sunX) ); if (x < sunX) theta += PI; p[np]= new Planet( r ); p[np].theta= theta; } // Planet details. p[np].c= nprlist.length) return; // (Already done.) p[np]= new Planet( rlist[k] ); // Radius of orbit. p[np].theta= 0; // p[np].theta= -k * PI / 6;rp String name= k100000000) tan= -tan; theta= atan( tan ); */ } void show() { //// Show the planet at new position (x,y) fill( c ); ellipseMode( RADIUS ); ellipse( x, y, pR, pR ); if (ring>0) { noFill(); if (ring>0) { stroke( ring>1 ? 240 : 200 ); strokeWeight(ring); arc( x, y, 1.5*pR, 0.25*pR+ring, -0.25*PI, 1.25*PI ); strokeWeight(1); noStroke(); } } if (names) text( name, x+pR, y-5 ); if (tails) { stroke(255,0,0); line( oldx-2*(x-oldx), oldy-2*(y-oldy), x,y ); // Display a "tail" from last (x,y) position. text( "...v="+v, oldx-2*(x-oldx), oldy-2*(y-oldy) ); // Display a "tail" from last (x,y) position. stroke(0); } // Debugging: coords if (debug) { text( "("+x+", "+y+")", x+pR, y+15); text( "r: "+r+" theta="+theta, x+pR, y+30 ); text( "v: "+v, x+pR, y+45 ); } // Lines if (lines) { line(sunX,sunY, x,y ); } } void info( float textX, float textY ) { //// Display a line of information about this planet. text( name, textX+20, textY ); textX += 70; String s=""; s += " v="+roundto(v,4); s += " d="+roundto(d,4); s += " r="+r; s += " theta="+roundto( theta, 4 ); s += " " +"("+roundto( x, 4 ) +","+roundto( y, 4 ) +")"; if (ring > 0) s += " ring="+ring; float rr= r - sqrt( sq(x - sunX) + sq(y - sunY) ); if (abs(rr) > r*0.0001 ) s += " rr="+rr; text( s, textX, textY ); } float roundto(float f, int places) { //// round to specified number of places. float p= pow(10,places); return round( f * p ) / p; } }