From 234cbdd85fca3f081d64cd18b955327ad4482041 Mon Sep 17 00:00:00 2001 From: Casey Reas Date: Thu, 20 Dec 2012 03:21:07 +0000 Subject: [PATCH] Minor changes to examples, more code color tweaks for 2b8 --- build/shared/lib/preferences.txt | 10 ++--- .../Cellular Automata/Conway/Conway.pde | 21 +++------ .../Cellular Automata/Spore1/Spore1.pde | 39 +++++++--------- .../Cellular Automata/Spore2/Spore2.pde | 37 +++++++-------- .../ContinuousLines/ContinuousLines.pde | 2 +- .../Fractals and L-Systems/Koch/Koch.pde | 36 ++++++--------- .../Geometry/NoiseSphere/NoiseSphere.pde | 4 +- .../Topics/Geometry/RGBCube/RGBCube.pde | 15 ++++--- .../Motion/BouncyBubbles/BouncyBubbles.pde | 11 +++-- .../CircleCollision/CircleCollision.pde | 21 +++++---- .../Motion/MovingOnCurves/MovingOnCurves.pde | 6 +-- java/examples/Topics/Motion/Puff/Puff.pde | 45 ++++++++++--------- .../Topics/Motion/Reflection1/Reflection1.pde | 5 +-- 13 files changed, 114 insertions(+), 138 deletions(-) diff --git a/build/shared/lib/preferences.txt b/build/shared/lib/preferences.txt index 010271938..0575ba4a9 100644 --- a/build/shared/lib/preferences.txt +++ b/build/shared/lib/preferences.txt @@ -233,17 +233,17 @@ editor.laf.linux = com.sun.java.swing.plaf.gtk.GTKLookAndFeel editor.token.function1.style = #006699,plain editor.token.function2.style = #006699,plain -editor.token.function3.style = #669933,plain +editor.token.function3.style = #669900,plain editor.token.function4.style = #006699,bold -editor.token.keyword1.style = #549cc0,plain -editor.token.keyword2.style = #549cc0,plain -editor.token.keyword3.style = #669933,plain +editor.token.keyword1.style = #72a7c2,plain +editor.token.keyword2.style = #72a7c2,plain +editor.token.keyword3.style = #669900,plain editor.token.keyword4.style = #ff6699,plain editor.token.keyword5.style = #e37139,plain editor.token.literal1.style = #7D4793,plain -editor.token.literal2.style = #666666,plain +editor.token.literal2.style = #617952,plain editor.token.operator.style = #006699,plain diff --git a/java/examples/Topics/Cellular Automata/Conway/Conway.pde b/java/examples/Topics/Cellular Automata/Conway/Conway.pde index 7720fbdf6..7540f5e85 100644 --- a/java/examples/Topics/Cellular Automata/Conway/Conway.pde +++ b/java/examples/Topics/Cellular Automata/Conway/Conway.pde @@ -15,8 +15,7 @@ int sx, sy; float density = 0.5; int[][][] world; -void setup() -{ +void setup() { size(640, 360); frameRate(12); sx = width; @@ -31,8 +30,7 @@ void setup() } } -void draw() -{ +void draw() { background(0); // Drawing and update cycle @@ -40,13 +38,11 @@ void draw() for (int y = 0; y < sy; y=y+1) { //if (world[x][y][1] == 1) // Change recommended by The.Lucky.Mutt - if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1)) - { + if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1)) { world[x][y][0] = 1; set(x, y, #FFFFFF); } - if (world[x][y][1] == -1) - { + if (world[x][y][1] == -1) { world[x][y][0] = 0; } world[x][y][1] = 0; @@ -56,12 +52,10 @@ void draw() for (int x = 0; x < sx; x=x+1) { for (int y = 0; y < sy; y=y+1) { int count = neighbors(x, y); - if (count == 3 && world[x][y][0] == 0) - { + if (count == 3 && world[x][y][0] == 0) { world[x][y][1] = 1; } - if ((count < 2 || count > 3) && world[x][y][0] == 1) - { + if ((count < 2 || count > 3) && world[x][y][0] == 1) { world[x][y][1] = -1; } } @@ -69,8 +63,7 @@ void draw() } // Count the number of adjacent cells 'on' -int neighbors(int x, int y) -{ +int neighbors(int x, int y) { return world[(x + 1) % sx][y][0] + world[x][(y + 1) % sy][0] + world[(x + sx - 1) % sx][y][0] + diff --git a/java/examples/Topics/Cellular Automata/Spore1/Spore1.pde b/java/examples/Topics/Cellular Automata/Spore1/Spore1.pde index cb5f3892a..7ea5255eb 100644 --- a/java/examples/Topics/Cellular Automata/Spore1/Spore1.pde +++ b/java/examples/Topics/Cellular Automata/Spore1/Spore1.pde @@ -18,25 +18,26 @@ color spore_color; int runs_per_loop = 10000; color black = color(0, 0, 0); -void setup() -{ +void setup() { size(640, 360); frameRate(24); - clearscr(); + reset(); +} + +void reset() { + clearScreen(); w = new World(); spore_color = color(172, 255, 128); seed(); } -void seed() -{ +void seed() { // Add cells at random places for (int i = 0; i < maxcells; i++) { int cX = (int)random(width); int cY = (int)random(height); - if (w.getpix(cX, cY) == black) - { + if (w.getpix(cX, cY) == black) { w.setpix(cX, cY, spore_color); cells[numcells] = new Cell(cX, cY); numcells++; @@ -44,8 +45,7 @@ void seed() } } -void draw() -{ +void draw() { // Run cells in random order for (int i = 0; i < runs_per_loop; i++) { int selected = min((int)random(numcells), numcells - 1); @@ -53,23 +53,19 @@ void draw() } } -void clearscr() -{ +void clearScreen() { background(0); } -class Cell -{ +class Cell { int x, y; - Cell(int xin, int yin) - { + Cell(int xin, int yin) { x = xin; y = yin; } // Perform action based on surroundings - void run() - { + void run() { // Fix cell coordinates while(x < 0) { x+=width; @@ -106,8 +102,8 @@ class Cell // The World class simply provides two functions, get and set, which access the // display in the same way as getPixel and setPixel. The only difference is that // the World class's get and set do screen wraparound ("toroidal coordinates"). -class World -{ +class World { + void setpix(int x, int y, int c) { while(x < 0) x+=width; while(x > width - 1) x-=width; @@ -125,9 +121,8 @@ class World } } -void mousePressed() -{ +void mousePressed() { numcells = 0; - setup(); + reset(); } diff --git a/java/examples/Topics/Cellular Automata/Spore2/Spore2.pde b/java/examples/Topics/Cellular Automata/Spore2/Spore2.pde index 2ad9da4b7..3ed57d1a5 100644 --- a/java/examples/Topics/Cellular Automata/Spore2/Spore2.pde +++ b/java/examples/Topics/Cellular Automata/Spore2/Spore2.pde @@ -22,7 +22,11 @@ void setup() { size(640, 360); frameRate(24); - clearscr(); + reset(); +} + +void reset() { + clearScreen(); w = new World(); spore1 = color(128, 172, 255); spore2 = color(64, 128, 255); @@ -54,8 +58,7 @@ void seed() } } -void draw() -{ +void draw() { // Run cells in random order for (int i = 0; i < runs_per_loop; i++) { int selected = min((int)random(numcells), numcells - 1); @@ -63,26 +66,19 @@ void draw() } } -void clearscr() -{ - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - set(x, y, color(0)); - } - } +void clearScreen() { + background(0); } -class Cell -{ +class Cell { int x, y; - Cell(int xin, int yin) - { + Cell(int xin, int yin) { x = xin; y = yin; } + // Perform action based on surroundings - void run() - { + void run() { // Fix cell coordinates while(x < 0) { x+=width; @@ -148,8 +144,8 @@ class Cell // The World class simply provides two functions, get and set, which access the // display in the same way as getPixel and setPixel. The only difference is that // the World class's get and set do screen wraparound ("toroidal coordinates"). -class World -{ +class World { + void setpix(int x, int y, int c) { while(x < 0) x+=width; while(x > width - 1) x-=width; @@ -167,8 +163,7 @@ class World } } -void mousePressed() -{ - setup(); +void mousePressed() { + reset(); } diff --git a/java/examples/Topics/Drawing/ContinuousLines/ContinuousLines.pde b/java/examples/Topics/Drawing/ContinuousLines/ContinuousLines.pde index 86fa9c2e9..a9e36e080 100644 --- a/java/examples/Topics/Drawing/ContinuousLines/ContinuousLines.pde +++ b/java/examples/Topics/Drawing/ContinuousLines/ContinuousLines.pde @@ -11,7 +11,7 @@ void setup() { void draw() { stroke(255); - if(mousePressed) { + if (mousePressed == true) { line(mouseX, mouseY, pmouseX, pmouseY); } } diff --git a/java/examples/Topics/Fractals and L-Systems/Koch/Koch.pde b/java/examples/Topics/Fractals and L-Systems/Koch/Koch.pde index 75af6b06a..377c35b67 100644 --- a/java/examples/Topics/Fractals and L-Systems/Koch/Koch.pde +++ b/java/examples/Topics/Fractals and L-Systems/Koch/Koch.pde @@ -26,7 +26,6 @@ void draw() { if (k.getCount() > 5) { k.restart(); } - } @@ -38,24 +37,21 @@ class KochFractal { ArrayList lines; // A list to keep track of all the lines int count; - public KochFractal() - { - start = new Point(0,height/2 + height/4); - end = new Point(width,height/2 + height/4); + KochFractal() { + start = new Point(0, height/2 + height/4); + end = new Point(width, height/2 + height/4); lines = new ArrayList(); restart(); } - void nextLevel() - { + void nextLevel() { // For every line that is in the arraylist // create 4 more lines in a new arraylist lines = iterate(lines); count++; } - void restart() - { + void restart() { count = 0; // Reset count lines.clear(); // Empty the array list lines.add(new KochLine(start,end)); // Add the initial line (from one end point to the other) @@ -66,8 +62,7 @@ class KochFractal { } // This is easy, just draw all the lines - void render() - { + void render() { for(int i = 0; i < lines.size(); i++) { KochLine l = (KochLine)lines.get(i); l.render(); @@ -82,11 +77,9 @@ class KochFractal { // Step 3: Return the new arraylist and it becomes the list of line segments for the structure // As we do this over and over again, each line gets broken into 4 lines, which gets broken into 4 lines, and so on. . . - ArrayList iterate(ArrayList before) - { + ArrayList iterate(ArrayList before) { ArrayList now = new ArrayList(); //Create emtpy list - for(int i = 0; i < before.size(); i++) - { + for (int i = 0; i < before.size(); i++) { KochLine l = (KochLine)lines.get(i); // A line segment inside the list // Calculate 5 koch points (done for us by the line object) Point a = l.start(); @@ -114,7 +107,7 @@ class KochLine { // Two points, // a is the "left" point and // b is the "right point - Point a,b; + Point a, b; KochLine(Point a_, Point b_) { a = a_.copy(); @@ -123,7 +116,7 @@ class KochLine { void render() { stroke(255); - line(a.x,a.y,b.x,b.y); + line(a.x, a.y, b.x, b.y); } Point start() { @@ -135,24 +128,21 @@ class KochLine { } // This is easy, just 1/3 of the way - Point kochleft() - { + Point kochleft() { float x = a.x + (b.x - a.x) / 3f; float y = a.y + (b.y - a.y) / 3f; return new Point(x,y); } // More complicated, have to use a little trig to figure out where this point is! - Point kochmiddle() - { + Point kochmiddle() { float x = a.x + 0.5f * (b.x - a.x) + (sin(radians(60))*(b.y-a.y)) / 3; float y = a.y + 0.5f * (b.y - a.y) - (sin(radians(60))*(b.x-a.x)) / 3; return new Point(x,y); } // Easy, just 2/3 of the way - Point kochright() - { + Point kochright() { float x = a.x + 2*(b.x - a.x) / 3f; float y = a.y + 2*(b.y - a.y) / 3f; return new Point(x,y); diff --git a/java/examples/Topics/Geometry/NoiseSphere/NoiseSphere.pde b/java/examples/Topics/Geometry/NoiseSphere/NoiseSphere.pde index 165885339..e14a5acbe 100644 --- a/java/examples/Topics/Geometry/NoiseSphere/NoiseSphere.pde +++ b/java/examples/Topics/Geometry/NoiseSphere/NoiseSphere.pde @@ -44,8 +44,8 @@ void draw() { } -class Pelo -{ +class Pelo { + float z = random(-radio, radio); float phi = random(TWO_PI); float largo = random(1.15, 1.2); diff --git a/java/examples/Topics/Geometry/RGBCube/RGBCube.pde b/java/examples/Topics/Geometry/RGBCube/RGBCube.pde index 2040d0c0b..760711f2b 100644 --- a/java/examples/Topics/Geometry/RGBCube/RGBCube.pde +++ b/java/examples/Topics/Geometry/RGBCube/RGBCube.pde @@ -8,29 +8,30 @@ float xmag, ymag = 0; float newXmag, newYmag = 0; -void setup() -{ +void setup() { size(640, 360, P3D); noStroke(); colorMode(RGB, 1); } -void draw() -{ +void draw() { background(0.5); pushMatrix(); - translate(width/2, height/2, -30); newXmag = mouseX/float(width) * TWO_PI; newYmag = mouseY/float(height) * TWO_PI; float diff = xmag-newXmag; - if (abs(diff) > 0.01) { xmag -= diff/4.0; } + if (abs(diff) > 0.01) { + xmag -= diff/4.0; + } diff = ymag-newYmag; - if (abs(diff) > 0.01) { ymag -= diff/4.0; } + if (abs(diff) > 0.01) { + ymag -= diff/4.0; + } rotateX(-ymag); rotateY(-xmag); diff --git a/java/examples/Topics/Motion/BouncyBubbles/BouncyBubbles.pde b/java/examples/Topics/Motion/BouncyBubbles/BouncyBubbles.pde index 302ba8360..0b3067d1b 100644 --- a/java/examples/Topics/Motion/BouncyBubbles/BouncyBubbles.pde +++ b/java/examples/Topics/Motion/BouncyBubbles/BouncyBubbles.pde @@ -12,17 +12,16 @@ float gravity = 0.03; float friction = -0.9; Ball[] balls = new Ball[numBalls]; -void setup() -{ +void setup() { size(640, 360); - noStroke(); for (int i = 0; i < numBalls; i++) { balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls); } + noStroke(); + fill(255, 204); } -void draw() -{ +void draw() { background(0); for (int i = 0; i < numBalls; i++) { balls[i].collide(); @@ -32,6 +31,7 @@ void draw() } class Ball { + float x, y; float diameter; float vx = 0; @@ -90,7 +90,6 @@ class Ball { } void display() { - fill(255, 204); ellipse(x, y, diameter, diameter); } } diff --git a/java/examples/Topics/Motion/CircleCollision/CircleCollision.pde b/java/examples/Topics/Motion/CircleCollision/CircleCollision.pde index 8809aa9ed..b40d4936a 100644 --- a/java/examples/Topics/Motion/CircleCollision/CircleCollision.pde +++ b/java/examples/Topics/Motion/CircleCollision/CircleCollision.pde @@ -5,7 +5,8 @@ * Based on Keith Peter's Solution in * Foundation Actionscript Animation: Making Things Move! */ - + + Ball[] balls = { new Ball(100, 400, 20), new Ball(700, 400, 80) @@ -24,7 +25,7 @@ void setup() { void draw() { background(51); fill(204); - for (int i=0; i< 2; i++){ + for (int i = 0; i < 2; i++){ balls[i].x += vels[i].x; balls[i].y += vels[i].y; ellipse(balls[i].x, balls[i].y, balls[i].r*2, balls[i].r*2); @@ -52,7 +53,8 @@ void checkObjectCollision(Ball[] b, PVector[] v){ /* bTemp will hold rotated ball positions. You just need to worry about bTemp[1] position*/ Ball[] bTemp = { - new Ball(), new Ball() }; + new Ball(), new Ball() + }; /* b[1]'s position is relative to b[0]'s so you can use the vector between them (bVect) as the @@ -65,7 +67,8 @@ void checkObjectCollision(Ball[] b, PVector[] v){ // rotate Temporary velocities PVector[] vTemp = { - new PVector(), new PVector() }; + new PVector(), new PVector() + }; vTemp[0].x = cosine * v[0].x + sine * v[0].y; vTemp[0].y = cosine * v[0].y - sine * v[0].x; vTemp[1].x = cosine * v[1].x + sine * v[1].y; @@ -75,14 +78,15 @@ void checkObjectCollision(Ball[] b, PVector[] v){ conservation of momentum equations to calculate the final velocity along the x-axis. */ PVector[] vFinal = { - new PVector(), new PVector() }; + new PVector(), new PVector() + }; // final rotated velocity for b[0] vFinal[0].x = ((b[0].m - b[1].m) * vTemp[0].x + 2 * b[1].m * - vTemp[1].x) / (b[0].m + b[1].m); + vTemp[1].x) / (b[0].m + b[1].m); vFinal[0].y = vTemp[0].y; // final rotated velocity for b[0] vFinal[1].x = ((b[1].m - b[0].m) * vTemp[1].x + 2 * b[0].m * - vTemp[0].x) / (b[0].m + b[1].m); + vTemp[0].x) / (b[0].m + b[1].m); vFinal[1].y = vTemp[1].y; // hack to avoid clumping @@ -94,7 +98,8 @@ void checkObjectCollision(Ball[] b, PVector[] v){ in the opposite direction */ // rotate balls Ball[] bFinal = { - new Ball(), new Ball() }; + new Ball(), new Ball() + }; bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y; bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x; bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y; diff --git a/java/examples/Topics/Motion/MovingOnCurves/MovingOnCurves.pde b/java/examples/Topics/Motion/MovingOnCurves/MovingOnCurves.pde index e4f16ecff..b2022adf8 100644 --- a/java/examples/Topics/Motion/MovingOnCurves/MovingOnCurves.pde +++ b/java/examples/Topics/Motion/MovingOnCurves/MovingOnCurves.pde @@ -17,16 +17,14 @@ float y = 0.0; // Current y-coordinate float step = 0.01; // Size of each step along the path float pct = 0.0; // Percentage traveled (0.0 to 1.0) -void setup() -{ +void setup() { size(640, 360); noStroke(); distX = endX - beginX; distY = endY - beginY; } -void draw() -{ +void draw() { fill(0, 2); rect(0, 0, width, height); pct += step; diff --git a/java/examples/Topics/Motion/Puff/Puff.pde b/java/examples/Topics/Motion/Puff/Puff.pde index 49256b8a6..034ee6d21 100644 --- a/java/examples/Topics/Motion/Puff/Puff.pde +++ b/java/examples/Topics/Motion/Puff/Puff.pde @@ -9,6 +9,7 @@ * body segments, controlling body shape and jitter. */ + // For puff head float headX; float headY; @@ -17,13 +18,13 @@ float speedY = .9; // For puff body int cells = 1000; -float[]px= new float[cells]; -float[]py= new float[cells]; -float[]radiiX = new float[cells]; -float[]radiiY = new float[cells]; -float[]angle = new float[cells]; -float[]frequency = new float[cells]; -float[]cellRadius = new float[cells]; +float[] px= new float[cells]; +float[] py= new float[cells]; +float[] radiiX = new float[cells]; +float[] radiiY = new float[cells]; +float[] angle = new float[cells]; +float[] frequency = new float[cells]; +float[] cellRadius = new float[cells]; void setup(){ @@ -34,7 +35,7 @@ void setup(){ headY = height/2; // Fill body arrays - for (int i=0; i< cells; i++){ + for (int i = 0; i < cells; i++){ radiiX[i] = random(-7, 7); radiiY[i] = random(-4, 4); frequency[i]= random(-9, 9); @@ -49,42 +50,42 @@ void draw(){ fill(255, 255, 255, 5); // Follow the leader - for (int i =0; i< cells; i++){ - if (i==0){ - px[i] = headX+sin(radians(angle[i]))*radiiX[i]; - py[i] = headY+cos(radians(angle[i]))*radiiY[i]; + for (int i = 0; i < cells; i++){ + if (i == 0){ + px[i] = headX + sin(radians(angle[i]))*radiiX[i]; + py[i] = headY + cos(radians(angle[i]))*radiiY[i]; } else{ - px[i] = px[i-1]+cos(radians(angle[i]))*radiiX[i]; - py[i] = py[i-1]+sin(radians(angle[i]))*radiiY[i]; + px[i] = px[i-1] + cos(radians(angle[i]))*radiiX[i]; + py[i] = py[i-1] + sin(radians(angle[i]))*radiiY[i]; // Check collision of body - if (px[i] >= width-cellRadius[i]/2 || px[i] <= cellRadius[i]/2){ + if ((px[i] >= width-cellRadius[i]/2) || (px[i] <= cellRadius[i]/2)){ radiiX[i]*=-1; cellRadius[i] = random(1, 40); frequency[i]= random(-13, 13); } - if (py[i] >= height-cellRadius[i]/2 || py[i] <= cellRadius[i]/2){ + if ((py[i] >= height-cellRadius[i]/2) || (py[i] <= cellRadius[i]/2)){ radiiY[i]*=-1; cellRadius[i] = random(1, 40); frequency[i]= random(-9, 9); } } // Draw puff - ellipse(px[i], py[i], cellRadius[i], cellRadius[i]); + ellipse(px[i], py[i], cellRadius[i], cellRadius[i]); // Set speed of body - angle[i]+=frequency[i]; + angle[i] += frequency[i]; } // Set velocity of head - headX+=speedX; - headY+=speedY; + headX += speedX; + headY += speedY; // Check boundary collision of head - if (headX >= width-cellRadius[0]/2 || headX <=cellRadius[0]/2){ + if ((headX >= width-cellRadius[0]/2) || (headX <=cellRadius[0]/2)){ speedX*=-1; } - if (headY >= height-cellRadius[0]/2 || headY <= cellRadius[0]/2){ + if ((headY >= height-cellRadius[0]/2) || (headY <= cellRadius[0]/2)){ speedY*=-1; } } diff --git a/java/examples/Topics/Motion/Reflection1/Reflection1.pde b/java/examples/Topics/Motion/Reflection1/Reflection1.pde index 2bdc30d6f..082153485 100644 --- a/java/examples/Topics/Motion/Reflection1/Reflection1.pde +++ b/java/examples/Topics/Motion/Reflection1/Reflection1.pde @@ -17,7 +17,7 @@ float velocityX, velocityY; void setup(){ size(640, 360); - frameRate(30); + fill(128); baseX1 = 0; baseY1 = height-150; @@ -32,8 +32,7 @@ void setup(){ directionY = random(0.1, 0.99); // normalize direction vector - float directionVectLength = sqrt(directionX*directionX + - directionY*directionY); + float directionVectLength = sqrt(directionX*directionX + directionY*directionY); directionX /= directionVectLength; directionY /= directionVectLength; }