From e5cb7eed836d80e017a19ea3ef04469a6646a84a Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Mon, 18 Mar 2013 16:16:24 -0400 Subject: [PATCH] a new proposed example demonstrating morphing from one shape to another --- java/examples/Topics/Motion/Morph/Morph.pde | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 java/examples/Topics/Motion/Morph/Morph.pde diff --git a/java/examples/Topics/Motion/Morph/Morph.pde b/java/examples/Topics/Motion/Morph/Morph.pde new file mode 100644 index 000000000..1c6a65f81 --- /dev/null +++ b/java/examples/Topics/Motion/Morph/Morph.pde @@ -0,0 +1,63 @@ +/** + * Morph. + * + * Changing one shape into another by interpolating + * vertices from one to another + */ + +ArrayList circle = new ArrayList(); +ArrayList square = new ArrayList(); + +ArrayList morph = new ArrayList(); + +boolean state = false; + +void setup() { + size(640, 360); + + for (int angle = 0; angle < 360; angle += 9) { + PVector v = PVector.fromAngle(radians(angle-135)); + v.mult(100); + circle.add(v); + morph.add(new PVector()); + } + + for (int x = -50; x < 50; x += 10) { + square.add(new PVector(x, -50)); + } + for (int y = -50; y < 50; y += 10) { + square.add(new PVector(50, y)); + } + for (int x = 50; x > -50; x -= 10) { + square.add(new PVector(x, 50)); + } + for (int y = 50; y > -50; y -= 10) { + square.add(new PVector(-50, y)); + } +} + +void draw() { + background(51); + + for (int i = 0; i < circle.size(); i++) { + PVector v1; + if (state) v1 = circle.get(i); + else v1 = square.get(i); + PVector v2 = morph.get(i); + v2.lerp(v1, 0.1); + float d = PVector.dist(v1, v2); + if (d < 0.01) { + state = !state; + } + } + + translate(width/2, height/2); + beginShape(); + noFill(); + stroke(255); + for (PVector v : morph) { + vertex(v.x, v.y); + } + endShape(CLOSE); +} +