Browse Source

add some more random

master
Fr3deric 6 years ago
parent
commit
7cd2b268a9
  1. 31
      lindenmayer-system/lindenmeyer-system.html

31
lindenmayer-system/lindenmeyer-system.html

@ -133,8 +133,9 @@ function sierpinski() { @@ -133,8 +133,9 @@ function sierpinski() {
* Barnsley Fern
************************************************/
function barnsley_fern() {
function barnsley_fern(num_defects) {
project.clear();
if(typeof num_defects == 'undefined') { num_defects = 0 };
var r = generate(
'iIXF'.split(''),
[
@ -144,13 +145,30 @@ function sierpinski() { @@ -144,13 +145,30 @@ function sierpinski() {
6
);
var n = 0;
while(n < num_defects) {
var rnd = Math.floor(Math.random()*r.length);
if((r[rnd] != '[') & (r[rnd] != ']')) {
r[rnd] = '*';
if(Math.random() > 0.9) {
if(Math.random() < 0.5) {
r.splice(rnd, 0, '+');
} else {
r.splice(rnd, 0, '-');
}
}
n += 1;
}
}
console.log(r);
var p = draw(
r,
{
'F': draw_forward(),
'i': draw_set_dir(Math.PI),
'I': draw_angle_init(25/180 * Math.PI + Math.random() * 0.2),
'I': draw_angle_init(25/180 * Math.PI),
'*': draw_angle_add((Math.random() - 0.5) * 0.01),
'-': draw_angle_turn(-1),
'+': draw_angle_turn(1),
'[': draw_state_push(),
@ -221,10 +239,14 @@ window.onload = function() { @@ -221,10 +239,14 @@ window.onload = function() {
window.globals.sierpinski();
});
document.getElementById('barnsley_fern').addEventListener('click', function() {
document.getElementById('barnsley-fern').addEventListener('click', function() {
window.globals.barnsley_fern();
});
document.getElementById('barnsley-fern-defects').addEventListener('click', function() {
window.globals.barnsley_fern(150);
});
}
</script>
@ -236,7 +258,8 @@ window.onload = function() { @@ -236,7 +258,8 @@ window.onload = function() {
<button id="sierpinski2">sierpinski2</button>
<button id="sierpinski2-defects">sierpinski2 (random defects)</button>
<button id="sierpinski">sierpinski</button>
<button id="barnsley_fern">barnsley_fern</button>
<button id="barnsley-fern">barnsley fern</button>
<button id="barnsley-fern-defects">barnsley fern (random defects)</button>
<hr>
<canvas id="canvas" style="width: 500px; height: 500px;" resize></canvas>
</body>

Loading…
Cancel
Save