-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
179 lines (173 loc) · 5.7 KB
/
index.html
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<html>
<head>
<meta charset="utf-8">
<title>Automata</title>
</head>
<body style="padding: 0px; margin: 0px">
<canvas id="c" width=600 height=600></canvas>
<script src="./FiniteAutomata.js"></script>
<script>
function renderNFA(ctx, nfa, width, height, title = "", input = "") {
ctx.fillStyle="lightgrey";
ctx.textAlign="center";
ctx.textBaseline="middle";
var fontArgs = ctx.font.split(' ');
ctx.font = '60px ' + fontArgs[fontArgs.length - 1];
ctx.fillText(
title,
width/2,
height/2
);
ctx.fillStyle="black";
ctx.strokeStyle="black";
const position = {};
const states = nfa.computate([nfa.startState], input);
for (let i in nfa.states) {
position[nfa.states[i]] = {};
position[nfa.states[i]].x = -Math.cos(Math.PI*2*i/nfa.states.length) * 240 + width/2;
position[nfa.states[i]].y = Math.sin(Math.PI*2*i/nfa.states.length) * 240 + height/2;
}
for (let q in nfa.transition) {
for (let a in nfa.transition[q]) {
let start = position[q];
for (let i in nfa.transition[q][a]) {
let end = position[nfa.transition[q][a][i]];
let mid = {
x: (start.x + end.x)/2,
y: (start.y + end.y)/2
}
ctx.beginPath();
ctx.moveTo(start.x, start.y);
ctx.quadraticCurveTo((mid.x*3+width/2)/4, (mid.y*3+height/2)/4, end.x, end.y);
ctx.stroke();
ctx.closePath();
var headlen = 10; // length of head in pixels
var angle = Math.atan2(end.y-(mid.y*3+height/2)/4,end.x-(mid.x*3+width/2)/4);
ctx.beginPath();
ctx.moveTo(
end.x-15*Math.cos(angle),
end.y-15*Math.sin(angle)
);
ctx.lineTo(
end.x-20*Math.cos(angle-Math.PI/18),
end.y-20*Math.sin(angle-Math.PI/18)
);
ctx.lineTo(
end.x-20*Math.cos(angle+Math.PI/18),
end.y-20*Math.sin(angle+Math.PI/18)
);
ctx.closePath()
ctx.fill();
}
}
}
for (let q in nfa.transition) {
for (let a in nfa.transition[q]) {
let start = position[q];
for (let i in nfa.transition[q][a]) {
let end = position[nfa.transition[q][a][i]];
let mid = {
x: (start.x + end.x)/2,
y: (start.y + end.y)/2
}
let textPos = {
x: (mid.x*7 + width/2)/8,
y: (mid.y*7 + height/2)/8
}
ctx.fillStyle="white";
ctx.beginPath();
ctx.arc(
textPos.x,
textPos.y,
10, 0, Math.PI*2
);
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.fillStyle="black";
var fontArgs = ctx.font.split(' ');
ctx.font = '10px ' + fontArgs[fontArgs.length - 1];
ctx.fillText(
a==""?"' '":a,
textPos.x,
textPos.y
)
}
}
}
for (let i in nfa.states) {
ctx.beginPath();
ctx.arc(
position[nfa.states[i]].x,
position[nfa.states[i]].y,
15, 0, Math.PI*2
);
ctx.fillStyle=states.includes(nfa.states[i])?(nfa.finalStates.includes(nfa.states[i])?"#ea8080":"#80deea"):"white";
ctx.fill();
ctx.closePath();
ctx.stroke();
if (nfa.startState==nfa.states[i]) {
ctx.beginPath();
ctx.moveTo(
position[nfa.states[i]].x,
position[nfa.states[i]].y - 20,
);
ctx.lineTo(
position[nfa.states[i]].x + 3,
position[nfa.states[i]].y - 25,
);
ctx.lineTo(
position[nfa.states[i]].x - 3,
position[nfa.states[i]].y - 25,
);
ctx.closePath();
ctx.fillStyle="black";
ctx.fill();
}
if (nfa.finalStates.includes(nfa.states[i])) {
ctx.beginPath();
ctx.arc(
position[nfa.states[i]].x,
position[nfa.states[i]].y,
10, 0, Math.PI*2
);
ctx.closePath();
ctx.stroke();
}
}
ctx.fillStyle="black";
var fontArgs = ctx.font.split(' ');
ctx.font = "30px " + ' ' + fontArgs[fontArgs.length - 1];
ctx.fillText(
input,
width/2,
height - 30
);
}
let nfa = NFA.parseExp('');
const canvas = document.getElementById("c");
const ctx = canvas.getContext("2d");
const width = canvas.width;
const height = canvas.width;
let input = "";
let title = "";
renderNFA(ctx, nfa, width, height, title, input);
window.onkeydown = (e) => {
if (e.keyCode == 8) input = input.slice(0,-1);
else if (e.keyCode == 13) {
title = input;
nfa = NFA.parseExp(input);
input = "";
}
ctx.clearRect(0, 0, width, height);
renderNFA(ctx, nfa, width, height, title, input);
}
window.onkeypress = (e) => {
let str = String.fromCharCode(e.keyCode);
if (/[a-zA-Z0-9(|)*\.]/.test(str)) input = input+str;
ctx.clearRect(0, 0, width, height);
renderNFA(ctx, nfa, width, height, title, input);
};
</script>
</body>
</html>