forked from trpomoais2017/fg02-lsystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
117 lines (105 loc) · 3.65 KB
/
script.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
"use strict";
const width = 1000;
const height = 600;
var point, axiom, newF, task, canvas, ctx;
var stack = [];
function getData() {
let pointX = parseInt(document.getElementById("pointX").value);
let pointY = parseInt(document.getElementById("pointY").value);
let startAngle = parseInt(document.getElementById("startAngle").value);
point = { x: pointX, y: pointY, a: startAngle };
axiom = document.getElementById("axiom").value.split("");
newF = document.getElementById("newF").value.split("");
task = { q: parseInt(document.getElementById("q").value), n: parseInt(document.getElementById("n").value), s: parseInt(document.getElementById("s").value) }
canvas = document.getElementById("canvas");
ctx = canvas.getContext('2d');
stack = [];
}
function transform() {
for (var i = 0; i < task.n; i++) {
let result = "";
for (var j = 0; j < axiom.length; j++) {
if (axiom[j] === "F") {
result += newF;
}
else
result += axiom[j];
}
axiom = result;
}
}
function draw() {
for (var i = 0; i < axiom.length; i++) {
switch (axiom[i]) {
case "F":
point.x += task.s * Math.cos(point.a * Math.PI / 180);
point.y += task.s * Math.sin(point.a * Math.PI / 180);
ctx.lineTo(point.x, point.y);
break;
case "[":
stack.push({ x: point.x, y: point.y, a: point.a });
break;
case "]":
var tmp = stack.pop();
point.x = tmp.x;
point.y = tmp.y;
point.a = tmp.a;
ctx.moveTo(point.x, point.y);
break;
case "+":
point.a -= task.q;
break;
case "-":
point.a += task.q;
break;
}
}
ctx.stroke();
}
function run() {
getData();
transform();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo(point.x, point.y);
draw();
}
function scaling() {
canvas.height = height;
canvas.width = width;
ctx.clearRect(0, 0, canvas.width, canvas.height);
var scaleVal = document.getElementById("scaleRange").value;
ctx.scale(scaleVal, scaleVal);
run();
}
function clearData() {
document.getElementById("pointX").value = "";
document.getElementById("pointY").value = "";
document.getElementById("startAngle").value = "";
document.getElementById("axiom").value = "";
document.getElementById("newF").value = "";
document.getElementById("q").value = "";
document.getElementById("n").value = "";
document.getElementById("s").value = "";
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function snowflake() {
document.getElementById("pointX").value = 500;
document.getElementById("pointY").value = 300;
document.getElementById("startAngle").value = 0;
document.getElementById("axiom").value = "[F]+[F]+[F]+[F]+[F]+[F]";
document.getElementById("newF").value = "F[+FF][-FF]FF[+F][-F]FF";
document.getElementById("q").value = 60;
document.getElementById("n").value = 3;
document.getElementById("s").value = 2;
}
function bush() {
document.getElementById("pointX").value = 200;
document.getElementById("pointY").value = 500;
document.getElementById("startAngle").value = -80;
document.getElementById("axiom").value = "F";
document.getElementById("newF").value = "-F+F+[+F-F-]-[-F+F+F]";
document.getElementById("q").value = 22.5;
document.getElementById("n").value = 4;
document.getElementById("s").value = 10;
}