Rotating and Scaling Multiple Stars

I'm trying to write a piece of code that rotates a group of 25 stars, and scales them randomly too.

Found good code for rotating PShapes, but scale doesn't seem to work for me. I always end up with scale somehow in the draw loop, which to my understanding means it is going to be run over and over (See results). I know this is a very basic question, but I'm still learning.

Thanks,

// A list of objects ArrayList polygons;

void setup() { size(640, 360, P3D);

// Make a PShape PShape star = createShape(); star.beginShape(); star.noStroke(); star.fill(0, 127); star.vertex(0, -50); star.vertex(14, -20); star.vertex(47, -15); star.vertex(23, 7); star.vertex(29, 40); star.vertex(0, 25); star.vertex(-29, 40); star.vertex(-23, 7); star.vertex(-47, -15); star.vertex(-14, -20); star.endShape(CLOSE);

// Make an ArrayList polygons = new ArrayList();

// Add a bunch of objects to the ArrayList // Pass in reference to the PShape // We coud make polygons with different PShapes for (int i = 0; i < 25; i++) { polygons.add(new Polygon(star)); } }

void draw() { background(255);

// Display and move them all for (Polygon poly : polygons) { scale(0.9); poly.display(); poly.move(); } } // A class to describe a Polygon (with a PShape)

class Polygon { // The PShape object PShape s; // The location where we will draw the shape float x, y; // Variable for simple motion float speed; float theta1 = 0;

Polygon(PShape s_) { x = random(width); // y = random(-500, -100); y = random(height); s = s_; // speed = random(2, 6); speed = 0; }

// Simple motion void move() { y += speed; if (y > height+100) { y = -100; } } //

// Draw the object void display() { pushMatrix(); translate(x, y); rotateZ(theta1); // scale(0.5); shape(s); theta1+=0.02; popMatrix(); } }

Tagged:

Answers

Sign In or Register to comment.