-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
362 lines (346 loc) · 12.1 KB
/
app.ts
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
// classes
//
class World {
public width: number
public height: number
public density: number
public infection: Infection
public mobility: Mobility
public framerate: number
public personSize: number
private personCount: number
private persons: any
private graphData: any = []
private first: number = 0
private animationRuns = false
public chart: Chart
constructor(width: number, height: number, density: number, framerate: number, personSize: number) {
this.width = width
this.height = height
this.density = density
this.persons = []
this.framerate = 1000 / framerate
this.personSize = personSize
}
init() {
let body = document.getElementById("container")
let canvas = document.createElement('canvas')
canvas.id = "canvas"
canvas.width = this.width
canvas.height = this.height
canvas.style.border = "1px solid"
body.appendChild(canvas)
let canvasChart: any = document.createElement('canvas')
canvasChart.id = "chart"
canvasChart.width = 700
canvasChart.height = 700
canvasChart.style.border = "1px solid"
body.appendChild(canvasChart)
this.chart = new Chart(canvasChart, {
type: 'line',
data: {
datasets: [{
barPercentage: 1,
lineTension: 0,
pointRadius: 0,
label: 'uninfected',
data: [],
backgroundColor: '#0000ff'
},
{
barPercentage: 1,
lineTension: 0,
pointRadius: 0,
label: 'infected',
data: [],
backgroundColor: '#ffaf00'
},
{
barPercentage: 1,
lineTension: 0,
pointRadius: 0,
label: 'deceased',
data: [],
backgroundColor: '#ff0000'
},
{
barPercentage: 1,
lineTension: 0,
pointRadius: 0,
label: 'recovered',
data: [],
backgroundColor: '#00ff00',
}]
},
options: {
tooltips: { enabled: false },
hover: { mode: null },
animation: {
duration: 0
},
elements: {
line: {
cubicInterpolationMode: 'monotone'
}
},
responsiveAnimationDuration: 0,
responsive: false,
scales: {
xAxes: [{
stacked: true,
}],
yAxes: [{
stacked: true,
}]
}
}
})
this.load()
}
load() {
this.first = 0
this.persons = []
this.graphData = []
this.personCount = this.width * this.height * 0.0001 * this.density
for (let a: number = 0; a < this.personCount; a++) {
let person = new Person(randomIntFromInterval(0, this.width), randomIntFromInterval(0, this.height), this.width, this.height, this.infection, this.mobility, "uninfected", this.personSize)
this.persons.push(person)
}
if (!this.animationRuns) {
this.animationStep()
this.animationRuns = true
}
}
animationStep() {
this.first++
if (this.first == 2) {
this.persons[0].state = "infected"
}
//
let uninfected = 0
let infected = 0
let deceased = 0
let recovered = 0
//
var canvas: any = document.getElementById("canvas")
var ctx = canvas.getContext("2d")
ctx.clearRect(0, 0, this.width, this.height)
for (let a: number = 0; a < this.persons.length; a++) {
for (let b: number = 0; b < this.persons.length; b++) {
if (this.persons[a].state == "infected") {
let dist = Math.sqrt(Math.pow((this.persons[a].xPosition - this.persons[b].xPosition), 2) + Math.pow((this.persons[a].yPosition - this.persons[b].yPosition), 2))
if (dist < this.persons[a].infection.reach + this.persons[a].size) {
if (this.persons[b].state == "uninfected") {
this.persons[b].state = "infected"
}
}
}
}
if (this.persons[a].state == "uninfected") {
uninfected++
}
if (this.persons[a].state == "infected") {
infected++
}
if (this.persons[a].state == "deceased") {
deceased++
}
if (this.persons[a].state == "recovered") {
recovered++
}
}
if (this.first > 2 && infected > 0) {
this.graphData.push({ "uninfected": uninfected, "infected": infected, "deceased": deceased, "recovered": recovered })
}
//convert data
let labels = []
let uninfectedArray = []
let infectedArray = []
let deceasedArray = []
let recoveredArray = []
for (let a = 0; a < this.graphData.length; a++) {
labels.push(a + "")
uninfectedArray.push(this.graphData[a].uninfected)
infectedArray.push(this.graphData[a].infected)
deceasedArray.push(this.graphData[a].deceased)
recoveredArray.push(this.graphData[a].recovered)
}
let xaxisstep = 20
labels = convertArrayLenght(labels, xaxisstep)
uninfectedArray = convertArrayLenght(uninfectedArray, xaxisstep)
infectedArray = convertArrayLenght(infectedArray, xaxisstep)
deceasedArray = convertArrayLenght(deceasedArray, xaxisstep)
recoveredArray = convertArrayLenght(recoveredArray, xaxisstep)
this.chart.data.labels = labels
this.chart.data.datasets[0].data = uninfectedArray
this.chart.data.datasets[1].data = infectedArray
this.chart.data.datasets[2].data = deceasedArray
this.chart.data.datasets[3].data = recoveredArray
this.chart.update()
for (let a: number = 0; a < this.persons.length; a++) {
this.persons[a].stateChange()
this.persons[a].move()
this.persons[a].draw(ctx)
}
setTimeout(() => {
this.animationStep()
}, this.framerate)
}
}
class Infection {
public duration: number
public mortality: number
public reach: number
//public exposure: number
//public icnubation: number
constructor(duration: number, mortality: number, reach: number) {
this.duration = duration
this.mortality = mortality
this.reach = reach
}
}
class Mobility {
public speed: number
public distance: number
constructor(speed: number, distance: number) {
this.speed = speed
this.distance = distance
}
}
class Person {
public xPosition: number
public yPosition: number
public xPositionMax: number
public yPositionMax: number
public infection: Infection
public mobility: Mobility
public state: string
private infectionCounter: number = 0
private moveStep: number = 0
private size: number
private xMove: number
private yMove: number
constructor(xPosition: number, yPosition: number, xPositionMax: number, yPositionMax: number, infection: Infection, mobility: Mobility, state: string = "uninfected", personSize: number) {
this.xPosition = xPosition
this.yPosition = yPosition
this.infection = infection
this.xPositionMax = xPositionMax
this.yPositionMax = yPositionMax
this.mobility = mobility
this.state = state
this.size = personSize
}
move() {
if (this.state != "deceased") {
this.xPosition = this.xPosition + this.xMove
this.yPosition = this.yPosition + this.yMove
if (this.xPosition < 0) {
this.xPosition = this.xPositionMax + this.xPosition
}
if (this.xPosition > this.xPositionMax) {
this.xPosition = this.xPosition - this.xPositionMax
}
if (this.yPosition < 0) {
this.yPosition = this.yPositionMax + this.yPosition
}
if (this.yPosition > this.yPositionMax) {
this.yPosition = this.yPosition - this.yPositionMax
}
}
}
stateChange() {
// if target is reached create a new target
if (this.moveStep == 0) {
let angle = Math.random() * Math.PI * 2
this.xMove = Math.sin(angle) * this.mobility.speed
this.yMove = Math.cos(angle) * this.mobility.speed
}
this.moveStep++
if (this.moveStep > this.mobility.distance / this.mobility.speed) {
this.moveStep = 0
}
//
if (this.infectionCounter > this.infection.duration) {
this.state = "recovered"
}
if (this.state == "infected") {
this.infectionCounter++
let d = Math.random()
if (d < this.infection.mortality / this.infection.duration) {
this.state = "deceased"
}
}
}
draw(ctx: any) {
ctx.beginPath()
ctx.arc(this.xPosition, this.yPosition, this.size, 0, 360)
ctx.strokeStyle = "rgba(0,0,0,5)"
ctx.stroke()
if (this.state == "uninfected") {
ctx.fillStyle = "rgba(0,0,255,0.5)"
}
if (this.state == "infected") {
ctx.fillStyle = "rgba(255,175,0,0.5)"
}
if (this.state == "recovered") {
ctx.fillStyle = "rgba(0,255,0,0.5)"
}
if (this.state == "deceased") {
ctx.fillStyle = "rgba(255,0,0,0.5)"
}
ctx.fill()
if (this.state == "infected") {
ctx.beginPath()
ctx.arc(this.xPosition, this.yPosition, this.infection.reach, 0, 360)
ctx.fillStyle = "rgba(255,175,0,0.5)"
ctx.fill()
}
}
}
//
// helper functions
//
function randomIntFromInterval(min: number, max: number) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
function convertArrayLenght(inputArray: any, length: number): any {
//return inputArray
length = length - 1
let myNewArray: any = []
for (let a = 0; a < length; a++) {
myNewArray.push(inputArray[Math.floor(a / length * inputArray.length)])
}
myNewArray.push(inputArray[Math.ceil(inputArray.length - 1)])
return myNewArray
}
//
// let's do it...
//
let framerate: number = 30
let personSize: number = 6
let world = new World(700, 700, 10, framerate, personSize) // width, height, density, framerate, personSize
let infection = new Infection(200, 0.3, 12) // duration, mortality, reach
let mobility = new Mobility(1, 13) // speed, distance
world.infection = infection
world.mobility = mobility
world.init()
//
// dat.gui
//
let gui = new dat.GUI()
let slider1 = gui.add(world, 'density').min(1).max(20).step(1)
let slider2 = gui.add(world, 'personSize').min(2).max(10).step(1)
let slider3 = gui.add(world.infection, 'duration').min(50).max(500).step(50)
let slider4 = gui.add(world.infection, 'mortality').min(0).max(1).step(0.1)
let slider5 = gui.add(world.infection, 'reach').min(1).max(20).step(1)
let slider6 = gui.add(world.mobility, 'speed').min(0).max(2).step(0.1)
let slider7 = gui.add(world.mobility, 'distance').min(1).max(50).step(1)
slider1.onChange(function () { world.load() })
slider2.onChange(function () { world.load() })
slider3.onChange(function () { world.load() })
slider4.onChange(function () { world.load() })
slider5.onChange(function () { world.load() })
slider6.onChange(function () { world.load() })
slider7.onChange(function () { world.load() })
console.log("ok");