yiler-huang-generative

Yiler Huang's Generative Flower

By Yiler Huang

This is my generative flower. The code will generates a small flower and a cross flower in random positions. The color of the cross flower is random. The small flowerwill be red if it's on the bottom half of the picture; it will be blue if it's on the top half of the picture.

這是我的生成花朵。該代碼會在隨機位置生成一朵小花和一朵十字花。十字花的顏色是隨機的。小花如果在圖片的上半部分則為紅色;在圖片的下半部分則為藍色。

function setup() {
  createCanvas(1700, 800);
  noLoop();
}
 
 
function randomRange (max, min) {
  return Math.floor(min + ((max-min) * Math.random()));
}
 
 
function buildPosition (startP, time){
  let positionsArray = [];
  for (let i = 0; i < time; i ++){
    positionsArray.push(startP * i)
  }
  //console.log(positionsArray)
  return positionsArray
}
 
 
let colors = [
  "darkBlue",
  "darkRed"
];
 
 
function buildArray (n, fillFunction) {
  let outputArray = [];
  for (let i = 0; i < n; i++) {
    outputArray.push(fillFunction(i));
  }
  return outputArray;
}
 
 
class MainPFlower {
  constructor(color, scale) {
    this.positionx = buildPosition(90, 18)[randomRange(buildPosition(90, 18).length, 0)];
    this.positiony = buildPosition(90, 10)[randomRange(10, 0)];
    this.size = 40.5;
    this.space = 22.5;
    this.leafSpace = 23;
    this.fSpace = 90;
    this.scale = scale;
    if (color != "random"){
      this.color = color;
    }
    else if (color == "random"){
      this.color = colors[randomRange(colors.length,0)];
    }
    if (this.positiony > buildPosition(90, 10)[randomRange(5, 0)]){
      this.color = colors[1];
    }
    else {
      this.color = colors[0];
    }
    //console.log(this.color);
    return this.positionx && this.color && this.positiony;
  }
  draw() {
      strokeWeight(1.8);
      stroke(0);
      fill("lightGreen");
      ellipse(
      this.positionx,
      this.positiony + this.leafSpace * this.scale,
      75 * this.scale,
      25 * this.scale)
      fill(this.color);
      ellipse(
        this.positionx + this.space * this.scale, 
        this.positiony,
        this.size * this.scale
      );
      ellipse(
        this.positionx - this.space * this.scale,
        this.positiony,
        this.size * this.scale
      );
      ellipse(
        this.positionx,
        this.positiony - this.space * this.scale,
        this.size * this.scale
      );
      ellipse(
        this.positionx,
        this.positiony + this.space * this.scale,
        this.size * this.scale
      );
      fill(237, 207, 38);
      ellipse(
        this.positionx, 
        this.positiony,
        this.size + 10 * this.scale
      );
    fill("lightGreen");
      ellipse(
      this.positionx + this.fSpace * this.scale,
      this.positiony + this.leafSpace * this.scale,
      75 * this.scale,
      25 * this.scale)
      fill(this.color);
      ellipse(
        this.positionx + (this.fSpace * this.scale) + this.space * this.scale, 
        this.positiony,
        this.size * this.scale
      );
      ellipse(
        this.positionx + (this.fSpace * this.scale) - this.space * this.scale,
        this.positiony,
        this.size * this.scale
      );
      ellipse(
        this.positionx + (this.fSpace * this.scale),
        this.positiony - this.space * this.scale,
        this.size * this.scale
      );
      ellipse(
        this.positionx + (this.fSpace * this.scale),
        this.positiony + this.space * this.scale,
        this.size * this.scale
      );
      fill(237, 207, 38);
      ellipse(
        this.positionx + (this.fSpace * this.scale), 
        this.positiony,
        this.size + 10 * this.scale
    );
    fill("lightGreen");
      ellipse(
      this.positionx - this.fSpace * this.scale,
      this.positiony + this.leafSpace * this.scale,
      75 * this.scale,
      25 * this.scale)
      fill(this.color);
      ellipse(
        this.positionx - (this.fSpace * this.scale) + this.space * this.scale, 
        this.positiony,
        this.size * this.scale
      );
      ellipse(
        this.positionx - (this.fSpace * this.scale) - this.space * this.scale,
        this.positiony,
        this.size * this.scale
      );
      ellipse(
        this.positionx - (this.fSpace * this.scale),
        this.positiony - this.space * this.scale,
        this.size * this.scale
      );
      ellipse(
        this.positionx - (this.fSpace * this.scale),
        this.positiony + this.space * this.scale,
        this.size * this.scale
      );
      fill(237, 207, 38);
      ellipse(
        this.positionx - (this.fSpace * this.scale), 
        this.positiony,
        this.size + 10 * this.scale
    );
    fill("lightGreen");
      ellipse(
      this.positionx,
      this.positiony+ (this.fSpace * this.scale) + this.leafSpace * this.scale,
      75 * this.scale,
      25 * this.scale)
      fill(this.color);
      ellipse(
        this.positionx + this.space * this.scale, 
        this.positiony + (this.fSpace * this.scale),
        this.size * this.scale
      );
      ellipse(
        this.positionx - this.space * this.scale,
        this.positiony + (this.fSpace * this.scale),
        this.size * this.scale
      );
      ellipse(
        this.positionx,
        this.positiony + (this.fSpace * this.scale) - this.space * this.scale,
        this.size * this.scale
      );
      ellipse(
        this.positionx,
        this.positiony + (this.fSpace * this.scale) + this.space * this.scale,
        this.size * this.scale
      );
      fill(237, 207, 38);
      ellipse(
        this.positionx, 
        this.positiony+ (this.fSpace * this.scale),
        this.size + 10 * this.scale
    );
    fill("lightGreen");
      ellipse(
      this.positionx,
      this.positiony - (this.fSpace * this.scale) + this.leafSpace * this.scale,
      75 * this.scale,
      25 * this.scale)
      fill(this.color);
      ellipse(
        this.positionx + this.space * this.scale, 
        this.positiony - (this.fSpace * this.scale),
        this.size * this.scale
      );
      ellipse(
        this.positionx - this.space * this.scale,
        this.positiony - (this.fSpace * this.scale),
        this.size * this.scale
      );
      ellipse(
        this.positionx,
        this.positiony - (this.fSpace * this.scale) - this.space * this.scale,
        this.size * this.scale
      );
      ellipse(
        this.positionx,
        this.positiony - (this.fSpace * this.scale) + this.space * this.scale,
        this.size * this.scale
      );
      fill(237, 207, 38);
      ellipse(
        this.positionx, 
        this.positiony - (this.fSpace * this.scale),
        this.size + 10 * this.scale
    );
  }
}
 
 
class ArrayFlower {
  constructor(color, scale) {
    this.positionx = Number(buildPosition(90, 20)[randomRange(buildPosition(90, 20).length, 0)]);
    this.positiony = Number(buildPosition(90, 10)[randomRange(10, 0)]);
    this.size = 40.5;
    this.space = 22.5;
    this.leafSpace = 23;
    this.scale = scale;
    if (color != "random"){
      this.color = color;
    }
    else if (color == "random"){
      this.color = colors[randomRange(colors.length,0)];
    }
    if (this.color == "darkBlue"){
      this.positiony = Number(buildPosition(90, 10)[randomRange(10, 5)]);
    }
    else if (this.color == "darkRed"){
      this.positiony = Number(buildPosition(90, 10)[randomRange(5, 0)]);
    }
    //console.log(this.color)
    return this.positionx && this.color && this.positiony;
  }
  draw() {
      strokeWeight(1.8);
      stroke(0);
      fill("lightGreen");
      ellipse(
      this.positionx,
      this.positiony + this.leafSpace * this.scale,
      75 * this.scale,
      25 * this.scale)
      fill(this.color);
      ellipse(
        this.positionx + this.space * this.scale, 
        this.positiony,
        this.size * this.scale
      );
      ellipse(
        this.positionx - this.space * this.scale,
        this.positiony,
        this.size * this.scale
      );
      ellipse(
        this.positionx,
        this.positiony - this.space * this.scale,
        this.size * this.scale
      );
      ellipse(
        this.positionx,
        this.positiony + this.space * this.scale,
        this.size * this.scale
      );
      fill(237, 207, 38);
      ellipse(
        this.positionx, 
        this.positiony,
        this.size * this.scale + 10
    );
  }
}
 
 
class BigFlower{
  constructor() {
    this.positionx = 400;
    this.positiony = 400;
    this.size = 40.5 * 6;
    this.space = 22.5 * 6;
    this.leafSpace = 23 * 6;
  }
  draw() {
      stroke(200);
      strokeWeight(5);
      fill(220);
      ellipse(
      this.positionx,
      this.positiony + this.leafSpace,
      75 * 6,
      25 * 6);
      ellipse(
        this.positionx + this.space, 
        this.positiony,
        this.size,
        this.size
      );
      ellipse(
        this.positionx - this.space,
        this.positiony,
        this.size,
        this.size
      );
      ellipse(
        this.positionx,
        this.positiony - this.space,
        this.size,
        this.size
      );
      ellipse(
        this.positionx,
        this.positiony + this.space,
        this.size,
        this.size
             );
      ellipse(
        this.positionx, 
        this.positiony,
        this.size + 10 * 6,
        this.size + 10 * 6
    );
  }
}
 
 
class Emphasis {
  constructor(flowerSpace) {
    this.size = 40.5;
    this.space = 22.5;
    this.leafSpace = 23
    this.pColor = "undifined"
    this.mColor = "undifined"
    this.flowerSpace = flowerSpace
  }
  draw() {
    for (let i = 0; i < 20 ; i ++){
      for (let y =0; y < 10; y++){
      this.positionx = i * this.flowerSpace;
      this.positiony = y * this.flowerSpace;
      if (i >= 10){
        this.pColor = 212;
        this.mColor = 204;
      }
      else if (i < 11) {
        this.pColor = 140 + i * 8;
        this.mColor = 60 + i * 16;
      }
      noStroke();
      fill(this.mColor);
      ellipse(
      this.positionx,
      this.positiony + this.leafSpace,
      75,
      25);
      fill(this.pColor);
      ellipse(
        this.positionx + this.space, 
        this.positiony,
        this.size
      );
      ellipse(
        this.positionx - this.space,
        this.positiony,
        this.size
      );
      ellipse(
        this.positionx,
        this.positiony - this.space,
        this.size
      );
      ellipse(
        this.positionx,
        this.positiony + this.space,
        this.size
             );
      fill(this.mColor);
      ellipse(
        this.positionx, 
        this.positiony,
        this.size + 10,
        this.size + 10
    );
      }
    }
  }
}
 
 
let MP = new MainPFlower("random", 1);
 
 
let randomFlowers = buildArray (1, rf => new ArrayFlower ("random", 1));
 
 
let bigF = new BigFlower();
 
 
function draw() {
  background(220);
  let testNE = new Emphasis(90);
  bigF.draw();
  testNE.draw();
  MP.draw();
  randomFlowers.forEach(r => r.draw());
}

  • yiler-huang-generative.txt
  • Last modified: 2022/01/24 00:14
  • by renick