Browse Source

generate whole tree at once instead of recursing

master
Fr3deric 5 years ago
parent
commit
add82a9cf0
  1. 100
      baumgen.js

100
baumgen.js

@ -57,70 +57,70 @@ var baum = { @@ -57,70 +57,70 @@ var baum = {
};
function gen_branch(level=0, shape=null, invertAngle=false) {
var baum = {};
var fac = 1;
var lfac = 1;
if(level > 0) {
lfac = 1.2;
//fac = 1 / (level*2);
fac = 0.5;
}
function gen_tree(pos) {
var baum = new Branch();
baum.length = (20 + Math.random() * 20) * lfac;
baum.length = (20 + Math.random() * 20);
if(shape == null) {
if(Math.random() < 0.5) {
shape = "kite";
} else {
shape = "triangle";
}
var shape;
if(Math.random() < 0.5) {
shape = "kite";
} else {
shape = "triangle";
}
baum.crown = {shape: shape};
if(shape == "kite") {
baum.crown.length = (70 + Math.random() * 30) * fac;
baum.crown.length = (70 + Math.random() * 30);
baum.crown.width = 0.4 + Math.random() * 0.2;
baum.crown.bisect = 0.3 + Math.random() * 0.1;
} else if(shape == "triangle") {
baum.crown.length = (70 + Math.random() * 30) * fac;
baum.crown.length = (70 + Math.random() * 30);
baum.crown.width = 0.4 + Math.random() * 0.2;
//baum.crown.bisect = 0.3 + Math.random() * 0.1;
}
if(level == 0) {
baum.angle = Math.PI / 2;
} else if(level % 2 == 1) {
//if(Math.random() < 0.5) {
if(invertAngle) {
baum.angle = Math.PI / 4 + Math.random() * 0.45;
} else {
baum.angle = Math.PI - Math.PI / 4 - Math.random() * 0.45;
baum.angle = Math.PI / 2; // + Math.random() * 0.2 - 0.1;
if((shape == "kite") && (Math.random() < 0.5)) {
var b = {
length: baum.length * 0.4,
angle: Math.PI / 5 + Math.random() * 0.45,
crown: {
shape: "kite",
length: baum.crown.length * 0.4,
width: baum.crown.width,
bisect: baum.crown.bisect,
//bisect: 0.5,
}
};
baum.forks = [{
pos: 0.2 + Math.random() * 0.4,
branch: new Branch(b),
}];
baum.draw(pos);
var rectified = false;
var i=0;
while(baum.collides_crown(baum.forks[0].branch)) {
baum.forks[0].branch.angle -= 0.01;
baum.draw(pos);
rectified = true;
i += 1;
if(i>50) {
var c = new Path.Circle({center:pos, radius: 2, strokeWidt: 1, strokeColor: '#ff0000'});
//break;
}
}
} else {
baum.angle = Math.PI/2 + (Math.random() * 0.6 - 0.3);
}
if(level == 0) {
baum.forks = [];
for(var i=0; i<Math.floor(Math.random() * 3); i++) {
var inv = (level % 2 == 0) && (i % 2 == 1);
baum.forks.push({
pos: 0.2 + Math.random() * 0.4,
branch: gen_branch(level+1, shape, inv),
});
}
} else if(level == 1) {
baum.forks = [];
if(Math.random() < 0.5) {
var inv = (level % 2 == 0) && (i % 2 == 1);
baum.forks.push({
pos: 0.2 + Math.random() * 0.7,
branch: gen_branch(level+1, shape, inv),
});
if(rectified) {
baum.forks[0].branch.angle -= Math.random() * 0.1;
var c = new Path.Circle({center:pos, radius: 2, strokeWidt: 1, strokeColor: '#ff0000'});
baum.draw(pos);
}
}
baum.draw(pos);
return baum;
}
@ -204,6 +204,9 @@ class Branch { @@ -204,6 +204,9 @@ class Branch {
}
}
collides_crown(other) {
return this._crown.intersects(other._crown);
}
}
@ -215,8 +218,7 @@ window.onload = function() { @@ -215,8 +218,7 @@ window.onload = function() {
for(var x=0; x<5; x++) {
for(var y=0; y<5; y++) {
var br = new Branch(gen_branch());
br.draw(new Point(150 + x*160, 150 + y*160));
var br = new Branch(gen_tree(new Point(50 + x*80, 150 + y*160)));
}
}

Loading…
Cancel
Save