Skip to content

Commit b7f1a90

Browse files
committed
Add . instruction for inverting angle, and jump instruction without drawing
1 parent 622d078 commit b7f1a90

File tree

3 files changed

+76
-10
lines changed

3 files changed

+76
-10
lines changed

Turtle.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
11
function Turtle(canvas, context, startPos, startAngle){
22

3+
const rad = (180/Math.PI);
4+
const sin = Math.sin;
5+
const cos = Math.cos;
6+
37
var x = startPos[0];
48
var y = startPos[1];
59

610
var stack = [];
711

8-
var angle = startAngle / (180/Math.PI);
12+
var angle = startAngle / rad;
13+
14+
var sign = 1;
915

1016
context.beginPath();
1117
context.moveTo(x, y);
1218

1319
function move(distance){
14-
x += Math.sin(angle) * distance;
15-
y -= Math.cos(angle) * distance;
20+
x += sin(angle) * distance;
21+
y -= cos(angle) * distance;
1622

1723
context.lineTo(x, y);
1824
}
1925

26+
function jump(distance){
27+
x += sin(angle) * distance;
28+
y -= cos(angle) * distance;
29+
30+
context.moveTo(x, y);
31+
}
32+
2033
function turn(a){
21-
angle += a / (180/Math.PI);
34+
angle += sign * a / rad;
35+
}
36+
37+
function invert(){
38+
sign = 0 - sign;
2239
}
2340

2441
function push(){
@@ -35,7 +52,9 @@ function Turtle(canvas, context, startPos, startAngle){
3552

3653
return {
3754
move: move
55+
,jump: jump
3856
,turn: turn
57+
,invert: invert
3958
,push: push
4059
,pop: pop
4160
};

l-systems.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ <h3>Drawing graphics with L-Systems</h3>
7474
<th>Result</th>
7575
</tr>
7676
<tr>
77-
<td><code>A-Z</code></td>
77+
<td><code>A-L</code></td>
7878
<td>Draw straight line of length defined by <em>Distance</em></td>
7979
</tr>
80+
<tr>
81+
<td><code>M-Z</code></td>
82+
<td>Jump forward by <em>Distance</em> without drawing</td>
83+
</tr>
8084
<tr>
8185
<td><code>a-z</code></td>
8286
<td>Do nothing: These symbols are only used in the replacement phase</td>
@@ -85,6 +89,10 @@ <h3>Drawing graphics with L-Systems</h3>
8589
<td><code>+ or -</code></td>
8690
<td>Turn by positive or negative <em>Angle</em></td>
8791
</tr>
92+
<tr>
93+
<td><code>.</code></td>
94+
<td>Invert the meaning of <code>+</code> and <code>-</code></td>
95+
</tr>
8896
<tr>
8997
<td><code>[ or ]</code></td>
9098
<td>

l-systems.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ function LSystem(){
268268
if(Date.now() - startTime > timeLimit){
269269
document.getElementById("timeLimitLabel").className += "alert ";
270270
setTimeout(function(){
271-
console.log(document.getElementById("timeLimitLabel").className);
272271
document.getElementById("timeLimitLabel").className = document.getElementById("timeLimitLabel").className.replace("alert ", '');
273272
}, 100);
274273
console.log("Replacing reached time limit after "+(Date.now() - startTime)+"ms.");
@@ -288,12 +287,47 @@ function LSystem(){
288287

289288
document.getElementById("replacementInfo").innerHTML += "<p>"+a+" of "+iterations+" replacement iterations, "+(Date.now() - startTime)+"ms</p>";
290289

290+
//ins = optimiseTurns(ins);
291+
// Turns out this doesn't speed up drawing at all
292+
291293
//document.body.className = document.body.className.replace("wait ", '');
292294

293295
return ins;
294296

295297
}
296298

299+
function optimiseTurns(ins){
300+
301+
var startTime = Date.now();
302+
303+
var out = "";
304+
var sum = 0;
305+
306+
for(i = 0; i < ins.length; i++){
307+
if(ins[i] == "+"){
308+
sum++;
309+
} else if(ins[i] == "-"){
310+
sum--;
311+
} else {
312+
if(sum > 0){
313+
while(sum--){
314+
out += "+";
315+
}
316+
} else if(sum < 0){
317+
while(sum++){
318+
out += "-";
319+
}
320+
}
321+
sum = 0;
322+
out += ins[i];
323+
}
324+
}
325+
326+
console.log("Reduced instruction length to "+Math.round(10000*out.length/ins.length)/100+"% in "+(Date.now() - startTime)+"ms.");
327+
328+
return out;
329+
}
330+
297331
function draw(){
298332

299333
//document.body.className += "wait ";
@@ -319,7 +353,6 @@ function LSystem(){
319353
if(Date.now() - startTime > timeLimit){
320354
document.getElementById("timeLimitLabel").className += "alert ";
321355
setTimeout(function(){
322-
console.log(document.getElementById("timeLimitLabel").className);
323356
document.getElementById("timeLimitLabel").className = document.getElementById("timeLimitLabel").className.replace("alert ", '');
324357
}, 100);
325358
console.log("Drawing reached time limit after "+(Date.now() - startTime)+"ms.");
@@ -335,16 +368,21 @@ function LSystem(){
335368
case "-":
336369
turtle.turn(-angle);
337370
break;
371+
case ".":
372+
turtle.invert();
373+
break;
338374
case "[":
339375
turtle.push();
340376
break;
341377
case "]":
342378
turtle.pop();
343379
break;
344380
default:
345-
if(ins[i].match(/[A-Z]/)){
381+
if(ins[i].match(/[A-L]/)){
346382
turtle.move(distance);
347383
moveCount++;
384+
} else if(ins[i].match(/[M-Z]/)){
385+
turtle.jump(distance);
348386
}
349387
break;
350388
}
@@ -513,6 +551,9 @@ function LSystem(){
513551
popup = document.getElementById("importPopup");
514552
document.getElementById("overlay").style.display = "flex";
515553
popup.style.display = "flex";
554+
555+
document.getElementById("importArea").focus();
556+
document.getElementById("importArea").select();
516557
}
517558

518559
document.getElementById("importButton").onclick = function(){
@@ -544,8 +585,6 @@ function LSystem(){
544585

545586
if(instructions){
546587
draw();
547-
} else {
548-
console.log("foo");
549588
}
550589
}
551590

0 commit comments

Comments
 (0)