-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtle.js
77 lines (67 loc) · 1.83 KB
/
turtle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Turtle commands renderer abstraction.
*/
class Turtle
{
/**
* Constructor.
*
* @constructor
*
* @param {Number} x - initial 'x' coordinate
* @param {Number} y - initial 'y'
* @param {Number} distance - distance between each steps
* @param {Number} angle - rotation angle
* @param {Number} decay - decay rate between steps
*/
constructor(x, y, distance, angle, decay)
{
// set starting point
this.start = createVector(x, y);
// store parameters
this.distance = distance
this.angle = angle
this.decay = decay
// setup initial step size
this.stepSize = this.distance;
}
/**
* Draws sequence of turtle commands.
*
* @param {string} sequence - sequence of turtle commands
*/
draw(sequence)
{
// set line color
stroke(TURTLE_COLOR);
strokeWeight(2);
// push transformation matrix
push();
// initialize in centered position
translate(windowWidth / 2, windowHeight / 2);
// draw sequence
for(let step of sequence)
{
switch(step)
{
case 'F':
line(0, 0, this.stepSize, 0);
translate(this.stepSize, 0);
break;
case 'f':
translate(this.stepSize, 0);
break;
case '+':
rotate(this.angle);
break;
case '-':
rotate(-this.angle);
break;
}
}
// pop transformation matrix
pop();
// decay step size
this.stepSize /= this.decay;
}
}