coronavirus-simulator

Coronavirus spread simulator
git clone http://git.hanabi.in/repos/coronavirus-simulator.git
Log | Files | Refs | LICENSE

commit 368b8797413cb8e82e502c3e841b36cf6acccd4c
Author: Agastya Chandrakant <me@hanabi.in>
Date:   Fri, 17 Dec 2021 10:13:31 +0000

Create project

Diffstat:
A.gitignore | 2++
ALICENSE | 14++++++++++++++
Aindex.html | 13+++++++++++++
Aindex.js | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apackage.json | 13+++++++++++++
Aperson.js | 16++++++++++++++++
6 files changed, 187 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +node_modules +\ No newline at end of file diff --git a/LICENSE b/LICENSE @@ -0,0 +1,14 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. + diff --git a/index.html b/index.html @@ -0,0 +1,13 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <title>Coronavirus simulation 1</title> + </head> + <body style="width: 100vw; height: 100vh; margin: 0; padding: 0;"></body> + <script src="https://unpkg.com/matter-js@0.14.2/build/matter.min.js"></script> + <script src="https://unpkg.com/p5@1.0.0/lib/p5.min.js"></script> + <script src="./person.js"></script> + <script src="./index.js"></script> +</html> diff --git a/index.js b/index.js @@ -0,0 +1,129 @@ +const h = document.body.clientHeight; +const w = document.body.clientWidth; + +const people = 140; + +const { Engine, Render, World, Bodies, Body } = Matter; +let engine; +let y = 0; + +let boxes; + +let arrS = [], + arrI = [], + arrR = []; + +function setup() { + createCanvas(w, h); + engine = Engine.create(); + world = engine.world; + world.gravity.y = 0; + boxes = []; + for (let i = 1; i <= people; i++) { + boxes.push( + Bodies.circle( + 10 + Math.random() * (w - 10), + 10 + Math.random() * (h - 10), + 10, + { + restitution: 1, + friction: 0, + frictionAir: 0, + frictionStatic: 0 + } + ) + ); + } + boxes.forEach(box => { + const prob = random() > 0.98 ? true : false; + box.status = prob ? 'I' : 'S'; + const xDir = Math.random() > 0.5 ? 1 : -1; + const yDir = Math.random() > 0.5 ? 1 : -1; + Body.setVelocity(box, { + x: xDir * random(), + y: yDir * random() + }); + }); + wallOptions = { + isStatic: true, + restitution: 1, + friction: 0, + frictionAir: 0, + frictionStatic: 0 + }; + leftWall = Bodies.rectangle(5, h / 2, 10, h, wallOptions); + rightWall = Bodies.rectangle(w - 5, h / 2, 10, h, wallOptions); + bottomWall = Bodies.rectangle(w / 2, h - 5, w, 10, wallOptions); + topWall = Bodies.rectangle(w / 2, 5, w, 10, wallOptions); + World.add(engine.world, [...boxes, leftWall, rightWall, bottomWall, topWall]); + Engine.run(engine); + console.log(boxes[0]); + stroke(255, 255, 255); +} + +function draw() { + background(51, 51, 51); + fill(0, 0, 0); + noStroke(); + rectMode(CENTER); + ellipseMode(CENTER); + rect(leftWall.position.x, leftWall.position.y, 10, h); + rect(rightWall.position.x, rightWall.position.y, 10, h); + rect(bottomWall.position.x, bottomWall.position.y, w, 10); + rect(topWall.position.x, topWall.position.y, w, 10); + boxes.forEach(box => { + noFill(); + if (box.status == 'R') fill(0, 255, 0); + if (box.status == 'S') fill(255, 255, 255); + if (box.status == 'I') fill(255, 0, 0); + circle(box.position.x, box.position.y, 20); + if (box.status == 'I') { + const index = box.id; + time = ((Math.random() * 10000) % 10) * 1000 + 3000; + setTimeout(function() { + world.bodies.filter(body => body.id == index)[0].status = 'R'; + }, time); + } + }); + for (let i = 0; i < boxes.length; i++) { + for (j = 0; j < boxes.length; j++) { + if (i != j) { + const { collided, bodyA, bodyB } = Matter.SAT.collides( + boxes[i], + boxes[j] + ); + if (collided) { + if (bodyA.status == 'I' && bodyB.status != 'R') bodyB.status = 'I'; + if (bodyB.status == 'I' && bodyA.status != 'R') bodyA.status = 'I'; + } + } + } + } + let [S, I, R] = [0, 0, 0]; + boxes.forEach(box => { + if (box.status == 'S') S++; + if (box.status == 'I') I++; + if (box.status == 'R') R++; + }); + arrS.push(S); + arrI.push(I); + arrR.push(R); + for (let i = 0; i < arrS.length; i++) { + noFill(); + // fill(255); + // circle(i, h - arrS[i] - 10, 5); + fill('rgba(255, 0, 0, 0.3)'); + circle(10 + i, h - arrI[i] - 10, 5); + rect(10 + i, h - 10 - arrI[i] / 2, 1, arrI[i]); + if (arrR[i]) { + fill('rgba(0, 255, 0, 0.3)'); + circle(10 + i, h - people + arrR[i] - 10, 5); + rect(10 + i, h - people - 10 + arrR[i] / 2, 1, arrR[i]); + } + } + + y += 1; + if (I == 0) { + noLoop(); + } +} diff --git a/package.json b/package.json @@ -0,0 +1,13 @@ +{ + "name": "corona", + "version": "1.0.0", + "description": "Simulating Coronavirus spread", + "main": "index.js", + "author": "Agastya", + "license": "BSD-3-Clause", + "private": false, + "dependencies": { + "matter-js": "^0.14.2", + "p5": "^1.0.0" + } +} diff --git a/person.js b/person.js @@ -0,0 +1,16 @@ +class Person { + constructor() { + this.x = 10 + Math.random() * (w - 10); + this.y = 10 + Math.random() * (h - 10); + this.velocity = {}; + this.velocity.x = Math.random() * 5; + this.velocity.y = Math.random() * 5; + } + show() { + if (this.x < 10.2 || this.x > w - 10.2) this.velocity.x = -this.velocity.x; + if (this.y < 10.2 || this.y > h - 10.2) this.velocity.y = -this.velocity.y; + this.x = this.x + this.velocity.x; + this.y = this.y + this.velocity.y; + circle(this.x, this.y, 10); + } +}