Skip to content

Commit

Permalink
Rename scenes to grammars codewise
Browse files Browse the repository at this point in the history
  • Loading branch information
dmrib committed Mar 28, 2021
1 parent 3c9e84f commit 825dd9c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ This is a simple implementation of a [L-systems](https://en.wikipedia.org/wiki/L

Given a ruleset, it will derive axioms and render instructions using a Turtle drawer.

| Rule | Effect |
|------|-------------------------------------------------------|
| F | Draw a line of `stepSize` and translate to end point. |
| f | Move to the end point without drawing. |
| + | Rotate by `angle` |
| - | Rotate by -`angle` |

Made when I was reading [The Nature of Code](https://natureofcode.com/).

### How to run
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<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/grammars.js"></script>
<script src="src/sketch.js"></script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion src/scenes.js → src/grammars.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const SCENES = [
const GRAMMARS = [
{
axiom: 'F-F-F-F',
ruleset: {
Expand Down
50 changes: 25 additions & 25 deletions src/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let chango;

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


Expand All @@ -41,8 +41,8 @@ function setup()
// stop drawing loop
noLoop();

// draw initial scene
loadScene();
// draw initial grammar
loadGrammar();

// clean background
background(BACKGROUND_COLOR);
Expand All @@ -60,7 +60,7 @@ function draw()
// draw current L-system state
turtle.draw(lsystem.state);

// display scene info
// display grammar info
displayInfo();
}

Expand All @@ -70,7 +70,7 @@ function draw()
*/
function mouseClicked()
{
if (iteration === SCENES[currentScene].maxDerivations)
if (iteration === GRAMMARS[currentGrammar].maxDerivations)
{
iteration = 1;
nextScene();
Expand All @@ -85,37 +85,37 @@ function mouseClicked()


/**
* Scene setup.
* Grammar setup.
*/
function loadScene()
function loadGrammar()
{
// load scene parameters
scene = SCENES[currentScene];
// load grammar parameters
grammar = GRAMMARS[currentGrammar];

// restart L-system
lsystem = new LSystem(scene.axiom, scene.ruleset);
lsystem = new LSystem(grammar.axiom, grammar.ruleset);

// restart turtle drawer
turtle = new Turtle(
scene.x,
scene.y,
scene.distance,
radians(scene.angle),
scene.decay
grammar.x,
grammar.y,
grammar.distance,
radians(grammar.angle),
grammar.decay
);
}


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

// setup next scene
loadScene();
// setup next grammar
loadGrammar();

// call p5.js drawing function
redraw();
Expand Down Expand Up @@ -148,7 +148,7 @@ function displayInfo()
fill(INFO_COLOR);
stroke(INFO_COLOR);
textSize(MEDIUM);
text(`(${currentScene + 1}/${SCENES.length})`, 50, (windowHeight * 0.2))
text(`(${currentGrammar + 1}/${GRAMMARS.length})`, 50, (windowHeight * 0.2))

fill(ENTRY_COLOR);
stroke(ENTRY_COLOR);
Expand All @@ -158,7 +158,7 @@ function displayInfo()
fill(INFO_COLOR);
stroke(INFO_COLOR);
textSize(MEDIUM);
text(`${SCENES[currentScene].axiom}`, 50, (windowHeight * 0.35))
text(`${GRAMMARS[currentGrammar].axiom}`, 50, (windowHeight * 0.35))

fill(ENTRY_COLOR);
stroke(ENTRY_COLOR);
Expand All @@ -168,7 +168,7 @@ function displayInfo()
fill(INFO_COLOR);
stroke(INFO_COLOR);
textSize(MEDIUM);
text(`(${iteration}/${SCENES[currentScene].maxDerivations})`, 50, (windowHeight * 0.5))
text(`(${iteration}/${GRAMMARS[currentGrammar].maxDerivations})`, 50, (windowHeight * 0.5))

fill(ENTRY_COLOR);
stroke(ENTRY_COLOR);
Expand All @@ -186,12 +186,12 @@ function displayInfo()
text(`Ruleset:`, 50, (windowHeight * 0.75))

let i = 0.8;
for (let rule in SCENES[currentScene].ruleset)
for (let rule in GRAMMARS[currentGrammar].ruleset)
{
fill(INFO_COLOR);
stroke(INFO_COLOR);
textSize(MEDIUM);
text(`${rule}: ${SCENES[currentScene].ruleset[rule]}`, 50, (windowHeight * i));
text(`${rule}: ${GRAMMARS[currentGrammar].ruleset[rule]}`, 50, (windowHeight * i));
i += 0.05;
}
}

0 comments on commit 825dd9c

Please sign in to comment.