-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp5-dragon-curve.js
65 lines (53 loc) · 1.54 KB
/
p5-dragon-curve.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
// requires p5.js (last tested on: v1.11.1)
function setup() {
const redDragonCurve = [];
const greenDragonCurve = [];
const blueDragonCurve = [];
const purpleDragonCurve = [];
createCanvas(750, 750);
redDragonCurve.push(new Move(5, 0));
redDragonCurve.push(new Move(0, 5));
greenDragonCurve.push(new Move(0, 5));
greenDragonCurve.push(new Move(-5, 0));
blueDragonCurve.push(new Move(-5, 0));
blueDragonCurve.push(new Move(0, -5));
purpleDragonCurve.push(new Move(0, -5));
purpleDragonCurve.push(new Move(5, 0));
for (let i = 0; i < 15; i++) {
doubleAndRotate(redDragonCurve);
doubleAndRotate(greenDragonCurve);
doubleAndRotate(blueDragonCurve);
doubleAndRotate(purpleDragonCurve);
}
background(200);
drawCurve(redDragonCurve, "red", 375, 375);
drawCurve(greenDragonCurve, "green", 375, 375);
drawCurve(blueDragonCurve, "blue", 375, 375);
drawCurve(purpleDragonCurve, "purple", 375, 375);
}
function drawCurve(curve, color, startX, startY) {
let currentX = startX;
let currentY = startY;
for (let i = 0; i < curve.length; i++) {
let oldX = currentX;
let oldY = currentY;
currentX += curve[i].xDir;
currentY += curve[i].yDir;
stroke(color);
line(oldX, oldY, currentX, currentY);
}
}
function doubleAndRotate(curve) {
for (let i = curve.length - 1; i >= 0; i--) {
curve.push(curve[i].rotated());
}
}
class Move {
constructor(startXDir, startYDir) {
this.xDir = startXDir;
this.yDir = startYDir;
}
rotated() {
return new Move(-this.yDir, this.xDir);
}
}