Browse Source

add barnsley fern

master
Fr3deric 5 years ago
parent
commit
24f263a2ca
  1. 40
      lindenmayer-system/lindenmeyer-system.html
  2. 49
      lindenmayer-system/lindenmeyer-system.js

40
lindenmayer-system/lindenmeyer-system.html

@ -129,6 +129,40 @@ function sierpinski() { @@ -129,6 +129,40 @@ function sierpinski() {
}
/************************************************
* Barnsley Fern
************************************************/
function barnsley_fern() {
project.clear();
var r = generate(
'IXF'.split(''),
[
['X'.split(''), 'F+[[X]-X]-F[-FX]+X'.split('')],
['F'.split(''), 'FF'.split('')]
],
6
);
console.log(r);
var p = draw(
r,
{
'F': draw_forward(),
'I': draw_angle_init(25/180 * Math.PI),
'-': draw_angle_turn(-1),
'+': draw_angle_turn(1),
'[': draw_state_push(),
']': draw_state_pop()
}
);
p.scale(2);
p.translate(new Point(200, 200));
p.strokeWidth = 1;
p.strokeColor = '#ff00ff';
}
/************************************************
*
************************************************/
@ -136,6 +170,7 @@ function sierpinski() { @@ -136,6 +170,7 @@ function sierpinski() {
globals.dragon_curve = dragon_curve;
globals.sierpinski2 = sierpinski2;
globals.sierpinski = sierpinski;
globals.barnsley_fern = barnsley_fern;
globals.get_svg = function() {
return project.exportSVG({asString: true});
@ -185,6 +220,10 @@ window.onload = function() { @@ -185,6 +220,10 @@ window.onload = function() {
window.globals.sierpinski();
});
document.getElementById('barnsley_fern').addEventListener('click', function() {
window.globals.barnsley_fern();
});
}
</script>
@ -196,6 +235,7 @@ window.onload = function() { @@ -196,6 +235,7 @@ 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>
<hr>
<canvas id="canvas" style="width: 500px; height: 500px;" resize></canvas>
</body>

49
lindenmayer-system/lindenmeyer-system.js

@ -43,25 +43,26 @@ function generate(start, rules, iterations) { @@ -43,25 +43,26 @@ function generate(start, rules, iterations) {
}
function draw_initstate(state) {
function draw_initstate(p, state) {
if(!('dir' in state)) {
state.dir = 0;//Math.PI/2;
}
if(!('curpath' in state)) {
p.addChild(new paper.Path());
state.curpath = p.lastChild;
}
}
function draw_forward() {
return function(p, state) {
draw_initstate(state);
if(p.children.length == 0) {
p.addChild(new paper.Path());
}
var pp = p.lastChild;
if(pp.length == 0) {
pp.add(new paper.Point(0, 0));
draw_initstate(p, state);
if(state.curpath.segments.length == 0) {
state.curpath.add(new paper.Point(0, 0));
console.log('---------------------------');
}
var lastp = pp.segments[pp.segments.length - 1].point;
pp.add(lastp.add(
var lastp = state.curpath.segments[state.curpath.segments.length - 1].point;
state.curpath.add(lastp.add(
new paper.Point(Math.sin(state.dir), Math.cos(state.dir))
));
}
@ -70,7 +71,7 @@ function draw_forward() { @@ -70,7 +71,7 @@ function draw_forward() {
function draw_turn(angle) {
return function(p, state) {
draw_initstate(state);
draw_initstate(p, state);
state.dir += angle;
}
}
@ -78,7 +79,7 @@ function draw_turn(angle) { @@ -78,7 +79,7 @@ function draw_turn(angle) {
function draw_angle_turn(fac) {
return function(p, state) {
draw_initstate(state);
draw_initstate(p, state);
state.dir += state.angle * fac;
}
}
@ -98,6 +99,30 @@ function draw_angle_add(delta) { @@ -98,6 +99,30 @@ function draw_angle_add(delta) {
}
function draw_state_push() {
return function(p, state) {
if(typeof state.stack == 'undefined') {
state.stack = new Array();
}
state.stack.push({
pos: state.curpath.segments[state.curpath.segments.length - 1].point,
dir: state.dir,
});
}
}
function draw_state_pop() {
return function(p, state) {
var s = state.stack.pop();
state.dir = s.dir;
p.addChild(new paper.Path());
state.curpath = p.lastChild;
state.curpath.add(s.pos);
}
}
function draw(word, actions) {
var p = new paper.CompoundPath();
var state = {};

Loading…
Cancel
Save