Commit c1270918 authored by Ladislav Hano's avatar Ladislav Hano 💬
Browse files

Add separate folder for petal designs

parent 838a4872
Loading
Loading
Loading
Loading

petals/flower.pde

0 → 100644
+100 −0
Original line number Diff line number Diff line
import java.lang.reflect.InvocationTargetException;

class CrumplyPetal extends Petal {
  float extent = 5.0f;
  int steps = 20;

  CrumplyPetal(int x, int y, color c) {
    super(x, y, c);
  }

  void render() {
    stroke(#355691);
    fill(#6b88b7ff);
    beginShape(LINES);
    for (float angle = 0; angle < TWO_PI; angle += TWO_PI / steps) {
      float r = noise(angle);
      float x = r * cos(angle);
      float y = r * sin(angle);
      vertex(x, y);
    }
    endShape();
  }
}

class Petal {
  PVector pos;
  float r;
  color c;

  
  Petal(int x, int y, color c) {
    pos = new PVector(x, y);
    r = random(15, 20);
    this.c = c;
  }
  
  void render() {
    fill(c, 200);
    circle(pos.x, pos.y, r);
  }
}

class BubbleFlower<T extends Petal> extends Flower {
  float extent = 10;
  int petal_count = 15;
  ArrayList<T> petals;
  //private final Class<T> petalClass;

  color petal_pallete[] = { #355691, #6b88b7ff, #ad2831, #640d14, #D7BCE8, #8884FF, #ECE4B7, #e0a957ff };

  BubbleFlower(Class<T> petalClass) {
    //this.petalClass = petalClass;

    petals = new ArrayList<T>();
    color c = petal_pallete[(int)random(petal_pallete.length)];
    for (int i = 0; i < petal_count; i++) {
      float angle = random(TWO_PI);
      float r = random(extent);
      float x = r * cos(angle);
      float y = r * sin(angle);
      
      try {
        T newPetal = petalClass
          .getConstructor(int.class, int.class, color.class)
          .newInstance((int)x, (int)y, c);
        petals.add(newPetal);
      } catch (NoSuchMethodException e) {
        System.err.println("Error: Class T must have a constructor (int, int, color)");
        e.printStackTrace();
      } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
        System.err.println("Error creating petal instance.");
        e.printStackTrace();
      }
    }
  }

  void render (PVector pos, PVector vel) {
    float theta = vel.heading() + radians(90);
    
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(theta);
    for (Petal petal : petals) {
      petal.render();
    }
    popMatrix();
  }
}

abstract class Flower {
  abstract void render(PVector pos, PVector vel);
}

void setup() {
  size(1280, 720);
  background(#ECE4B7);
  BubbleFlower<CrumplyPetal> flower = new BubbleFlower<>(CrumplyPetal.class);
  PVector pos = new PVector(width / 2, height / 2);
  flower.render(pos, pos);
}