Skip to content

Commit

Permalink
Limit number of derivations per ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
dmrib committed Mar 28, 2021
1 parent f5c8954 commit 3faa47b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<title>L-systems</title>
<style> body {padding: 0; margin: 0;} </style>
<script src="libs/p5.min.js"></script>
<script src="libs/stringbuffer.js"></script>
<script src="libs/lsystem.js"></script>
<script src="libs/turtle.js"></script>
<script src="src/stringbuffer.js"></script>
<script src="src/lsystem.js"></script>
<script src="src/turtle.js"></script>
<script src="src/scenes.js"></script>
<script src="src/sketch.js"></script>
</head>
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions src/scenes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 600,
decay: 4,
Expand All @@ -20,6 +21,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 600,
decay: 4,
Expand All @@ -34,6 +36,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 600,
decay: 2,
Expand All @@ -49,6 +52,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 100,
decay: 2,
Expand All @@ -63,6 +67,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 50,
decay: 2,
Expand All @@ -77,6 +82,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 50,
decay: 2,
Expand All @@ -91,6 +97,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 50,
decay: 2,
Expand All @@ -105,6 +112,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 50,
decay: 2,
Expand All @@ -119,6 +127,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 50,
decay: 2,
Expand All @@ -133,6 +142,7 @@ const SCENES = [
'-': '-',
'+': '+'
},
maxDerivations: 5,
angle: 90,
distance: 50,
decay: 2,
Expand Down
62 changes: 38 additions & 24 deletions src/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const BACKGROUND_COLOR = [0, 0, 0];

// define L-system state
let clicks = 1;
let currentScene = 0;
let lsystem;

Expand Down Expand Up @@ -40,36 +41,20 @@ function draw()
}


/**
* p5.js key pressed callback.
*/
function keyPressed()
{
// right arrow is pressed: move to next scene
if (keyCode === RIGHT_ARROW)
{
// get next scene index
currentScene + 1 === SCENES.length ? currentScene = 0 : currentScene++;

// setup next scene
loadScene();

// call p5.js drawing function
redraw();
}
}


/**
* p5.js mouse clicked callback.
*/
function mouseClicked()
{
// derivate next L-system state
lsystem.derive();
if (clicks === SCENES[currentScene].maxDerivations)
{
nextScene();
clicks = 0;
}

// call p5.js drawing function
redraw();
else
nextDerivation();
clicks += 1;
}


Expand All @@ -93,3 +78,32 @@ function loadScene()
scene.decay
);
}


/**
* Forward sketch to next scene.
*/
function nextScene()
{
// get next scene index
currentScene + 1 === SCENES.length ? currentScene = 0 : currentScene++;

// setup next scene
loadScene();

// call p5.js drawing function
redraw();
}


/**
* Get L-system next derivation.
*/
function nextDerivation()
{
// derivate next L-system state
lsystem.derive();

// call p5.js drawing function
redraw();
}
File renamed without changes.
File renamed without changes.

0 comments on commit 3faa47b

Please sign in to comment.