sketch
/* Miranda Luong
Section E
mluong@andrew.cmu.edu
Project-10: Generative Landscape
*/
var trees = [];
var hillSpeed = 0.00005;
var hillDetail = 0.005;
function setup() {
createCanvas(640, 240);
// create an initial collection of 30 trees
for (var i = 0; i < 30; i++){
//sets random birthplaceX for trees
var rx = random(width);
trees[i] = makeTree(rx);
}
frameRate(100);
}
function draw() {
background(66, 131, 244);
//sun
noStroke();
fill(225, 145, 90);
ellipse(width-55, 40, 45, 45);
drawHill();
updateAndDisplay();
removeOutOfView();
addTrees();
}
function drawHill(){
noStroke();
fill('green');
beginShape();
vertex(0, height);
for (var x = 0; x < width; x++){
var h = (x * hillDetail)+ (millis() * hillSpeed);
var y = map(noise(h), 0, 1, 0, height);
vertex(x, y);
}
vertex(width,height);
endShape();
}
function updateAndDisplay(){
// Update the trees's positions, and display them.
for (var i = 0; i < trees.length; i++){
trees[i].move();
trees[i].display();
}
}
function removeOutOfView(){
// If a tree has dropped off the left edge,
// remove it from the array. Copy all the buildings
// we want to keep into a new array.
var treesToKeep = [];
for (var i = 0; i < trees.length; i++){
if (trees[i].x + trees[i].breadth > 0) {
treesToKeep.push(trees[i]);
}
}
trees = treesToKeep; // remember the surviving buildings
}
function addTrees() {
// With a very tiny probability, add a new tree to the end.
var newTreeLikelihood = 0.007;
if (random(0,1) < newTreeLikelihood) {
trees.push(makeTree(width));
}
}
// method to update position of tree every frame
function treeMove() {
this.x += this.speed;
}
// draw the tree
function treeDisplay() {
var totalBushHeight = this.nBush * this.bushHeight
noStroke();
push();
translate(this.x, height-100);
for (var i = this.nBush; i > 1; i = i - 1) {
fill(14, 100-(i*5), 39);
triangle(0, i * this.bushHeight, this.breadth, i * this.bushHeight, this.breadth/2, (i-1)*(this.bushHeight-5));
}
fill('brown');
rect(this.breadth / 4 + 2.5,totalBushHeight,5,8)
pop();
}
function makeTree(birthLocationX) {
var tree = {x: birthLocationX,
breadth: 20,
speed: -1.0,
nBush: round(random(2,5)),
move: treeMove,
display: treeDisplay,
bushHeight: 10}
return tree;
}
This was a really challenging exercise. Sadly, there was a lot I wanted to do but just couldn’t seem to figure out. Ideally, the shape of the hills would’ve matched the position of the trees but because the X placement of the trees from the beginning was random, I couldn’t create a linkage by chronological order of birth (a for loop would’ve been great for that).
I think I definitely have a better understanding of objects and creating generative landscape images, a far cry from past projects generative art.
Original Sketch