/
dragonCurveLogic.js
152 lines (130 loc) · 4.97 KB
/
dragonCurveLogic.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
console.log(
"THE DRAGON CURVE DRAWING FUNCTION" + "\n" +
"BY NICHOLAS BERNHARD" + "\n" +
"COPYRIGHT 2019" + "\n" +
"LIFE, AH, FINDS A WAY."
);
let directionArray = [2];
// up = 0 right = 1 down = 2 left = 3
let iterationValueTable = [
{x: 600, y: 300, lineLength: 200, spoken: "FIRST"},
{x: 700, y: 300, lineLength: 200, spoken: "SECOND"},
{x: 700, y: 600, lineLength: 200, spoken: "THIRD"},
{x: 600, y: 600, lineLength: 100, spoken: "FOURTH"},
{x: 350, y: 650, lineLength: 100, spoken: "FIFTH", gap: 45},
{x: 250, y: 550, lineLength: 75, spoken: "SIXTH", gap: 50},
{x: 300, y: 350, lineLength: 50, spoken: "SEVENTH", gap: 50},
{x: 400, y: 250, lineLength: 40, spoken: "EIGHTH", gap: 10},
{x: 600, y: 250, lineLength: 25, spoken: "NINTH"},
{x: 750, y: 350, lineLength: 19, spoken: "TENTH"},
{x: 650, y: 600, lineLength: 13, spoken: "ELEVENTH", gap: 11},
{x: 600, y: 700, lineLength: 9, spoken: "TWELFTH"},
{x: 350, y: 700, lineLength: 7, spoken: "THIRTEENTH"},
{x: 235, y: 600, lineLength: 5, spoken: "FOURTHEENTH"},
{x: 300, y: 375, lineLength: 3, spoken: "FIFTEENTH"},
{x: 400, y: 275, lineLength: 2, spoken: "SIXTEENTH"},
{x: 600, y: 275, lineLength: 1.5, spoken: "SEVENTEENTH"},
{x: 700, y: 375, lineLength: 1, spoken: "EIGHTEENTH"}
];
let originalX = 0;
let originalY = 0;
let x = 0;
let y = 0;
let lineLength = 0;
let currentIteration = 1;
let maxIterations = 0;
function getById(id) {
return document.getElementById(id);
};
function whenClicked(id, functionToRun) {
getById(id).addEventListener("click", functionToRun);
}
let dragonCurveCanvas = getById("dragonCurveCanvas");
let ctx = dragonCurveCanvas.getContext("2d");
ctx.strokeStyle = "red";
ctx.fillStyle = "red";
function runDragonCurve() {
let rotatedArray = [];
function rotateAndUnshift(item) {
if (item < 3) {
rotatedArray.unshift(item + 1);
} else if (item === 3) {
rotatedArray.unshift(0);
}
}
directionArray.forEach(rotateAndUnshift);
directionArray = directionArray.concat(rotatedArray);
function drawDragonCurve(item) {
ctx.beginPath();
ctx.moveTo(x, y);
if (item === 0) {
y = (y - lineLength);
} else if (item === 1) {
x = (x + lineLength);
} else if (item === 2) {
y = (y + lineLength);
} else if (item === 3) {
x = (x - lineLength);
}
ctx.lineTo(x, y);
// ctx.rect(x, y, lineLength, lineLength);
ctx.stroke();
// function renderSquares() {
// let squareXPos = 0;
// let squareYPos = 0;
// if (item === 0) {
// squareXPos = x;
// squareYPos = y;
// } else if (item === 1) {
// squareXPos = (x - lineLength);
// squareYPos = y;
// } else if (item === 2) {
// squareXPos = (x - lineLength);
// squareYPos = (y - lineLength);
// } else if (item === 3) {
// squareXPos = x;
// squareYPos = (y - lineLength);
// }
// ctx.fillRect(squareXPos, squareYPos, lineLength, lineLength);
// }
// renderSquares();
}
directionArray.forEach(drawDragonCurve);
x = originalX;
y = originalY;
};
function dragonCurveWhileLoop() {
currentIteration = 1;
ctx.clearRect(0, 0, dragonCurveCanvas.width, dragonCurveCanvas.height);
let iterationSelectElement = getById("iterationSelect");
let selectedIteration = iterationSelectElement[iterationSelectElement.selectedIndex].value;
let selectedIterationToInteger = parseInt(selectedIteration);
if (isNaN(selectedIterationToInteger) === false) {
maxIterations = selectedIterationToInteger + 1;
x = iterationValueTable[selectedIterationToInteger- 1].x;
y = iterationValueTable[selectedIterationToInteger - 1].y;
originalX = iterationValueTable[selectedIterationToInteger - 1].x;
originalY = iterationValueTable[selectedIterationToInteger - 1].y;
lineLength = iterationValueTable[selectedIterationToInteger - 1].lineLength;
while (currentIteration < maxIterations) {
runDragonCurve();
currentIteration = (currentIteration + 1);
}
getById("whichIteration").innerHTML = iterationValueTable[selectedIterationToInteger - 1].spoken;
getById("iterationExponent").innerHTML = selectedIterationToInteger;
getById("asInteger").innerHTML = Math.pow(2, selectedIterationToInteger);
getById("informationDiv").style.visibility = "visible";
} else {
console.error("Did not select a number");
}
// getById("printout").value = JSON.stringify(directionArray);
originalX = 0;
originalY = 0;
x = 0;
y = 0;
lineLength = 0;
currentIteration = 1;
maxIterations = 0;
directionArray = [2];
}
whenClicked("runDragonCurve", dragonCurveWhileLoop);