Hannah K-Project-10

sketch-34.js

// Creates a forest with trees

var terrain = []; // Values of tree terrain unknown
var terrainL = terrain.length; // Returns length of terrain
var j;
var horTreeOff = 1; // Hor tree offset so top of tree is centered on trunk
var vertTreeOff = 10; // Vert tree offset so top of tree is on top of trunk
var a;
var b;
var c;
var d;
var e;
var f;
var g;
var h;
var i;
var forest1 = [];
var forest1L = forest1.length;
var forest2 = [];
var forest2L = forest2.length;
var forest3 = [];
var forest3L = forest3.length;

function setup() {
    createCanvas(600, 400); 
}

function draw() {
    background(135, 206, 235);

    // Draws the sun
    fill(253, 184, 19);
    ellipse(0, 0, 100, 100);

    createForest1();
    createForest2();
    createForest3();
    placeTrees1();
    placeTrees2();
    placeTrees3();
    calculateAndRenderTerrain();
    placeTreesOnTerrain(); 

}

function createForest1() {
    var noiseScale = 0.001;
    var forestDetail = 0.0005;
    var forestSpeed = 0.0001;

    // 1st "layer" of forest
    for(a = 0; a < width; a++) {
        b = (a * forestDetail * 10 + millis() * forestSpeed / 4);
        c = map(noise(b), 0, 1, 0, height-250);
        stroke(139, 143, 143);
        line(a, c, a, height);
    }
}

function createForest2() {
    var noiseScale = 0.005;
    var forestDetail = 0.0001;
    var forestSpeed = 0.0001;

    // 2nd "layer" of forest
    for (d = 0; d < width; d++) {
        e = (d * forestDetail * 10 + millis() * forestSpeed / 2);
        f = map(noise(e), 0, 1, 0, height-75);
           stroke(139, 150, 50);
           line(d, f + 20, d, height);
    }    
}

function createForest3() {
    var noiseScale = 0.001;
    var forestDetail = 0.0005;
    var forestSpeed = 0.0005;

    // 3rd "layer" of forest
    for (g = 0; g < width; g++) {
        h = (g * forestDetail * 7 + millis() * forestSpeed);
        i = map(noise(h), 0, 1, 0, height-60);
           stroke(139, 175, 63);
           line(g, i + 75, g, height);
    }    
}

// Places trees on hills of terrain
function placeTreesOnTerrain() {
  for(j = 1; j < width; j++) {
    if(terrain[j] < terrain[j+1] & terrain[j] < terrain[j-1]) {
    drawTree();
    }
  }
}

function drawTree() {
  noStroke();
  fill(139,69,19);  
  rect(j, terrain[j]-vertTreeOff, 5, 15);
  fill(85,107,47);
  ellipse(j+horTreeOff, terrain[j]-vertTreeOff, 15, 15);
}
 
// Code from Week 7's Assignment
function calculateAndRenderTerrain() {
  eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};
  if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}
  k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--)
  {if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}
  ('c(6);b(4,6,4);9(a 3=0;3<d;3++){8[3]=e(h((3/7.0)+(g()/(7*4))),0,1,5*0.2,5);\
  f(3,8[3],3,5)}',18,18,'|||x|10|height|90|120|terrain|for|var|stroke|\
  noiseSeed|width|map|line|millis|noise'.split('|'),0,{}))
}

function placeTrees1() {
    for(a = 1; a < width; a++) {
        if(forest1[a] < forest1[a+1] & forest1[a] < forest1[a-1]) {
        drawTrees1();   
        }
    }
}

function drawTrees1() {
    noStroke();
    fill(139,69,19);  
    rect(a, forest1[a]-vertTreeOff, 5, 15);
    fill(85,107,47);
    ellipse(a+horTreeOff, forest1[a]-vertTreeOff, 15, 15);
}

function placeTrees2() {
    for(d = 1; d < width; d++) {
        if(forest2[d] < forest2[d+1] & forest2[d] < forest2[d-1]) {
        drawTrees2();   
        }
    }
}

function drawTrees2() {
    noStroke();
    fill(139,69,19);  
    rect(3, forest2[d]-vertTreeOff, 5, 15);
    fill(85,107,47);
    ellipse(d+horTreeOff, forest2[d]-vertTreeOff, 15, 15);

}

function placeTrees3() {
    for(g = 1; g < width; g++) {
        if(forest3[g] < forest3[g+1] & forest3[g] < forest3[g-1]) {
        drawTrees3();   
        }
    }
}

function drawTrees3() {
    noStroke();
    fill(139,69,19);  
    rect(3, forest3[g]-vertTreeOff, 5, 15);
    fill(85,107,47);
    ellipse(g+horTreeOff, forest3[g]-vertTreeOff, 15, 15);

}

This week’s project was definitely a bit of a struggle. I originally intended to create a forest with multiple layers of trees that appeared at the highest points of the created terrain (similar to an assignment we had to do for Week 7). The inspiration for a forest landscape was from this past summer, when I went camping with my family at Sequoia Kings Canyon. However, while there do not appear to be any bugs in my code, the trees are not showing up on the other “layers” of the forest (see below for a rough sketch of what I intended to create).
20161105_232633

Leave a Reply