-
Notifications
You must be signed in to change notification settings - Fork 1
/
50per_stop_script.js
270 lines (231 loc) · 8.57 KB
/
50per_stop_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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
(function() {
const recoverTime = 8; // 감염 후 완치까지 걸리는 시간(초)
const totalCount = 200; // 전체 사람 수
const stop_ratio = 0.5; // 멈춰있는 비율
const stopCount = totalCount * stop_ratio; // 멈춘 사람 수
const moveCount = totalCount - stopCount; // 움직이는 사람 수
const speed = 1; // 움직이는 속도
const radius = 5; //반지름
const healthy_color = '#b3bccb';
const sick_color = '#dd002f';
const recovered_color = 'blue';
let healthyCount = 0; //건강한 사람 수
let sickCount = 0; //감염자 수
let recoveredCount = 0; //완치자 수
const healthyBar = document.querySelector('.healthy .bar');
const sickBar = document.querySelector('.sick .bar');
const recoveredBar = document.querySelector('.recovered .bar');
const healthyLabelCount = document.querySelector('.healthy .count');
const sickLabelCount = document.querySelector('.sick .count');
const recoveredLabelCount = document.querySelector('.recovered .count');
const simulationBtn = document.querySelector('.simulation-btn');
const canvasContainer = document.querySelector('.canvas-container');
const canvas = document.querySelector('.canvas');
const context = canvas.getContext('2d');
// 그래프 그릴 캔버스
const canvas2 = document.querySelector('.graph-canvas');
const context2 = canvas2.getContext('2d');
const circleAngle = Math.PI * 2;
let move_balls = []; //움직이는 공들
let stop_balls = []; //멈춰있는 공들
let all_balls = []; //전체 공들
let rafId;
let stop;
class Ball {
constructor(info) {
this.x = info.x;
this.y = info.y;
this.nextX = this.x;
this.nextY = this.y;
this.angle = info.angle;
this.color = info.color;
this.draw();
}
infected() {
const self = this;
this.color = '#dd002f';
setTimeout(function() {
self.recover();
}, recoverTime * 1000);
}
recover() {
this.color = '#1f71ff';
}
draw() {
context.fillStyle = this.color;
context.beginPath();
context.arc(this.x, this.y, radius, 0, circleAngle, false);
context.closePath();
context.fill();
}
}
function toRadian(d) {
return d * Math.PI / 180;
}
function toDegree(r) {
return r * 180 / Math.PI;
}
function hitTest(ball1, ball2) {
let value;
const dx = ball1.nextX - ball2.nextX;
const dy = ball1.nextY - ball2.nextY;
const dist = dx * dx + dy * dy;
if (dist <= radius * 2 * (radius * 2)) {
value = true;
}
return value;
}
function checkCollision() {
let ball;
let testBall;
for (let i = 0; i < all_balls.length; i++) {
ball = all_balls[i];
for (let j = i + 1; j < all_balls.length; j++) {
testBall=all_balls[j];
if (hitTest(ball, testBall)) {
if (ball.color === '#dd002f' && testBall.color === '#b3bccb') {
testBall.infected();
}
if (testBall.color === '#dd002f' && ball.color === '#b3bccb') {
ball.infected();
}
const angle1 = ball.angle;
const angle2 = testBall.angle;
ball.angle = angle2;
testBall.angle = angle1;
}
}
}
}
function checkCount() {
let ball;
let healthyCount = 0;
let sickCount = 0;
let recoveredCount = 0;
for (let i = 0; i < totalCount; i++) {
ball = all_balls[i];
switch (ball.color) {
case '#b3bccb':
healthyCount++;
break;
case '#dd002f':
sickCount++;
break;
case '#1f71ff':
recoveredCount++;
break;
}
}
healthyLabelCount.innerHTML = healthyCount;
sickLabelCount.innerHTML = sickCount;
recoveredLabelCount.innerHTML = recoveredCount;
drawGraph(recoveredCount, healthyCount, sickCount);
if (sickCount === 0) {
stop = true;
}
}
let graphX = 0;
function drawGraph(recoveredCount, healthyCount, sickCount) {
let recoveredHeight = recoveredCount / totalCount * canvas2.height;
let healthyHeight = healthyCount / totalCount * canvas2.height;
let sickHeight = sickCount / totalCount * canvas2.height;
context2.fillStyle = '#1f71ff'; //완치
context2.fillRect(graphX, 0, 1, recoveredHeight);
context2.fillStyle = '#b3bccb'; //건강
context2.fillRect(graphX, recoveredHeight, 1, healthyHeight);
context2.fillStyle = '#dd002f'; //감염
context2.fillRect(graphX, recoveredHeight + healthyHeight, 1, sickHeight);
graphX++;
}
function stop_canLocate(ball) {
let value = true;
for (let i = 0; i < stop_balls.length; i++) {
if (hitTest(ball, stop_balls[i])) {
value = false;
}
}
return value;
}
function move_canLocate(ball) {
let value = true;
for (let i = 0; i < move_balls.length; i++) {
if (hitTest(ball, move_balls[i])) {
value = false;
}
}
return value;
}
function init() {
all_balls = [];
stop_balls = [];
move_balls = [];
stop = false;
canvasContainer.classList.remove('stop');
context2.clearRect(0, 0, canvas2.width, canvas2.height);
graphX = 0;
let stop_ball;
let move_ball;
// stop_balls[] 만들기
for (let i = 0; i < stopCount; i++) {
let stop_positionOK = false;
while (!stop_positionOK) {
stop_ball = new Ball({
x: radius * 2 + Math.floor(Math.random() * (canvas.width - radius * 3)),
y: radius * 2 + Math.floor(Math.random() * (canvas.height - radius * 3)),
angle: Math.round(Math.random() * 360),
color: '#b3bccb'
});
stop_positionOK = stop_canLocate(stop_ball);
}
stop_balls.push(stop_ball);
all_balls.push(stop_ball);
}
// move_balls[] 만들기
for (let i = 0; i < moveCount; i++) {
let move_positionOK = false;
while (!move_positionOK) {
move_ball = new Ball({
x: radius * 2 + Math.floor(Math.random() * (canvas.width - radius * 3)),
y: radius * 2 + Math.floor(Math.random() * (canvas.height - radius * 3)),
angle: Math.round(Math.random() * 360),
color: '#b3bccb'
});
move_positionOK = move_canLocate(move_ball);
}
move_balls.push(move_ball);
all_balls.push(move_ball);
}
all_balls[Math.floor(Math.random() * totalCount)].infected();
loop();
}
function loop() {
context.clearRect(0, 0, canvas.width, canvas.height);
let move_ball;
let stop_ball;
for (let i = 0; i < moveCount; i++) {
move_ball = move_balls[i];
if (move_ball.x > canvas.width - radius || move_ball.x < radius) {
move_ball.angle = 180 - move_ball.angle;
} else if (move_ball.y > canvas.height - radius || move_ball.y < radius) {
move_ball.angle = 360 - move_ball.angle;
}
move_ball.x += Math.cos(toRadian(move_ball.angle)) * speed;
move_ball.y += Math.sin(toRadian(move_ball.angle)) * speed;
move_balls[i].draw();
move_ball.nextX = move_ball.x + Math.cos(toRadian(move_ball.angle)) * speed;
move_ball.nextY = move_ball.y + Math.sin(toRadian(move_ball.angle)) * speed;
}
for(let j=0; j<stopCount; j++) {
stop_ball = stop_balls[j];
stop_balls[j].draw();
}
checkCollision();
checkCount();
rafId = requestAnimationFrame(loop);
if (stop) {
cancelAnimationFrame(rafId);
canvasContainer.classList.add('stop');
}
}
simulationBtn.addEventListener('click', init);
})();