From 91471de8d987f6f7f93ce5e6b92e9075fd60a252 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Sat, 16 Mar 2013 22:29:58 -0400 Subject: [PATCH] reworking Reflection1 to use PVector --- .../Topics/Motion/Reflection1/Reflection1.pde | 127 +++++++++--------- 1 file changed, 63 insertions(+), 64 deletions(-) diff --git a/java/examples/Topics/Motion/Reflection1/Reflection1.pde b/java/examples/Topics/Motion/Reflection1/Reflection1.pde index 082153485..730e0ed0b 100644 --- a/java/examples/Topics/Motion/Reflection1/Reflection1.pde +++ b/java/examples/Topics/Motion/Reflection1/Reflection1.pde @@ -1,4 +1,4 @@ - /** +/** * Non-orthogonal Reflection * by Ira Greenberg. * @@ -7,121 +7,120 @@ * vector. */ -float baseX1, baseY1, baseX2, baseY2; +PVector base1; +PVector base2; float baseLength; -float[] xCoords, yCoords; -float ellipseX, ellipseY, ellipseRadius = 6; -float directionX, directionY; -float ellipseSpeed = 3.5; -float velocityX, velocityY; -void setup(){ +PVector[] coords; + +PVector position; +float r = 6; +PVector direction; +float speed = 3.5; +PVector velocity; + +void setup() { size(640, 360); fill(128); - baseX1 = 0; - baseY1 = height-150; - baseX2 = width; - baseY2 = height; + base1 = new PVector(0, height-150); + base2 = new PVector(width, height); + createGround(); // start ellipse at middle top of screen - ellipseX = width/2; - - // calculate initial random direction - directionX = random(0.1, 0.99); - directionY = random(0.1, 0.99); + position = new PVector(width/2, 0); - // normalize direction vector - float directionVectLength = sqrt(directionX*directionX + directionY*directionY); - directionX /= directionVectLength; - directionY /= directionVectLength; + // calculate initial random direction + direction = PVector.random2D(); + + velocity = new PVector(); } -void draw(){ +void draw() { // draw background fill(0, 12); noStroke(); rect(0, 0, width, height); - // calculate length of base top - baseLength = dist(baseX1, baseY1, baseX2, baseY2); - xCoords = new float[ceil(baseLength)]; - yCoords = new float[ceil(baseLength)]; - - // fill base top coordinate array - for (int i=0; i width-ellipseRadius){ - ellipseX = width-ellipseRadius; - directionX *= -1; + if (position.x > width-r) { + position.x = width-r; + direction.x *= -1; } // left - if (ellipseX < ellipseRadius){ - ellipseX = ellipseRadius; - directionX *= -1; + if (position.x < r) { + position.x = r; + direction.x *= -1; } // top - if (ellipseY < ellipseRadius){ - ellipseY = ellipseRadius; - directionY *= -1; + if (position.y < r) { + position.y = r; + direction.y *= -1; // randomize base top - baseY1 = random(height-100, height); - baseY2 = random(height-100, height); + base1.y = random(height-100, height); + base2.y = random(height-100, height); + createGround(); + } +} + +void createGround() { + // calculate length of base top + baseLength = PVector.dist(base1, base2); + + // fill base top coordinate array + coords = new PVector[ceil(baseLength)]; + for (int i=0; i