howard-lee-generative

Rex Lee's generative flower

By Howard Lee

function setup() {
  createCanvas(1700, 800);
  //frameRate(5);
  noLoop();
}
 
let shapes = ["flower"];
 
let colors = ["pink", "orange", "lightgrey", "lightblue","lavender"];
 
function pick(inputArray) {
  return inputArray[Math.floor(inputArray.length * Math.random())];
}
 
function buildArray(n, fillFunction) {
  let outputArray = [];
  for (let i = 0; i < n; i++) {
    outputArray.push(fillFunction(i));
  }
  return outputArray;
}
 
function randomRange(min, max) {
  return min + (max - min) * Math.random();
}
 
class RandomShape {
  constructor(x,maxX,y,c) {
    //this.color = pick(colors);
    this.color = c;
    this.shape = pick(shapes);
    this.xposition = randomRange(x,x+maxX);
    this.yposition = randomRange(y, y+399);
    this.size = randomRange(51, 51);
  }
  draw() {
    if (this.color == "black" && !(this.shape == "ellipse")) {
      this.xposition = 266;
      this.size = 400;
    }
    if (this.yposition>144) {}
 
    if (this.shape == "rect") {
      rect(this.xposition, this.yposition, this.size, this.size);
    } else if (this.shape == "ellipse") {
      ellipse(this.xposition, this.yposition, this.size, this.size);
    } else if (this.shape == "flower") {
      translate(this.xposition, this.yposition);
      fill(163, 152, 163);
      rect(35, 20, 14, 135, 20);
      rect(5, 64, 55, 25, 20);
      rect(30, 64, 55, 25, 20);
      //petal
      //fill(this.color);
       if (this.color=="random"){fill(pick(colors))} else {fill(this.color);}
      rect(0, 20, this.size, this.size, 20);
      rect(30, 20, this.size, this.size, 20);
      fill(191, 170, 191);
      rect(30, 35, 25, 25, 10);
      resetMatrix();
    } else {
      triangle(
        this.xposition,
        this.yposition,
        this.xposition + this.size,
        this.yposition + this.size,
        this.xposition - this.size,
        this.yposition + this.size
      );
    }
    fill(this.color);
  }
}
 
//check buildArray on the wiki to make 10 objects
// buildArray can use an arrow function; check arrow function on the wiki
 
let myShapes = buildArray(144, (i) => new RandomShape(0,250,0,"pink"));
 
let myShapes2 = buildArray(177, (i) => new RandomShape(275,800,0,"random"));
 
// i => new RandomShape()
// is the same as
// function makeShape () {return new RandomShape()}
 
function draw() {
  background(270, 178, 17);
  //let shape1 = new RandomShape();
 // shape1.draw();
  // use forEach to do something with every item in an array
  // forEach can also use an arrow function
  console.log(myShapes);
  myShapes.forEach((x) => x.draw());
  myShapes2.forEach((x) => x.draw());
}

  • howard-lee-generative.txt
  • Last modified: 2022/01/24 00:12
  • by renick