Skip to content

Commit

Permalink
start
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnkunstman committed May 31, 2020
1 parent f6eee42 commit 7f2cd3d
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# GLU-corona-simulation
Minimalistic infection simulation based on very few variables.

npm run tsc //compile .ts files to .js files
143 changes: 143 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
"use strict";
//
// classes
//
var World = /** @class */ (function () {
function World(width, height, denisty, framerate) {
this.width = width;
this.height = height;
this.denisty = denisty;
this.personCount = Math.round(width * height * denisty);
console.log("person count:" + this.personCount);
this.temp = 0;
this.persons = [];
this.framerate = 1000 / framerate;
}
World.prototype.init = function () {
var canvas = document.createElement('canvas');
canvas.id = "canvas";
canvas.width = this.width;
canvas.height = this.height;
canvas.style.zIndex = "1";
canvas.style.position = "absolute";
canvas.style.border = "1px solid";
var body = document.getElementsByTagName("body")[0];
body.appendChild(canvas);
for (var a = 0; a < this.personCount; a++) {
var person = new Person(randomIntFromInterval(0, this.width), randomIntFromInterval(0, this.height), this.width, this.height, this.infection, "uninfected");
this.persons.push(person);
}
this.persons[0].state = "infected";
this.animationStep();
};
World.prototype.animationStep = function () {
var _this = this;
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, this.width, this.height);
for (var a = 0; a < this.persons.length; a++) {
for (var b = 0; b < this.persons.length; b++) {
if (this.persons[a].state == "infected") {
if (this.persons[a].xPosition == this.persons[b].xPosition) {
if (this.persons[a].yPosition == this.persons[b].yPosition) {
this.persons[b].state = "infected";
}
}
}
}
}
for (var a = 0; a < this.persons.length; a++) {
this.persons[a].move();
this.persons[a].draw(ctx);
}
this.temp++;
setTimeout(function () {
_this.animationStep();
console.log(_this.temp);
}, this.framerate);
};
;
return World;
}());
var Infection = /** @class */ (function () {
function Infection(duration, mortality, reach) {
this.duration = duration;
this.mortality = mortality;
this.reach = reach;
}
return Infection;
}());
var Mobility = /** @class */ (function () {
function Mobility(speed, distance) {
this.speed = speed;
this.distance = distance;
}
return Mobility;
}());
var Person = /** @class */ (function () {
function Person(xPosition, yPosition, xPositionMax, yPositionMax, infection, state) {
if (state === void 0) { state = "uninfected"; }
this.xPosition = xPosition;
this.yPosition = yPosition;
this.infection = infection;
this.xPositionMax = xPositionMax;
this.yPositionMax = yPositionMax;
this.state = state;
}
Person.prototype.move = function () {
if (this.state != "deceased") {
this.xPosition = this.xPosition - randomIntFromInterval(-10, 10);
this.yPosition = this.yPosition - randomIntFromInterval(-10, 10);
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;
}
}
};
Person.prototype.setState = function (state) {
this.state = state;
};
Person.prototype.draw = function (ctx) {
ctx.beginPath();
ctx.arc(this.xPosition, this.yPosition, this.infection.reach, 0, 360);
ctx.stroke();
if (this.state == "uninfected") {
ctx.fillStyle = "blue";
}
if (this.state == "infected") {
ctx.fillStyle = "orange";
}
if (this.state == "recovered") {
ctx.fillStyle = "green";
}
if (this.state == "deceased") {
ctx.fillStyle = "red";
}
ctx.fill();
};
return Person;
}());
//
// helper functiona
//
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
//
// let's do it...
//
var speed = 30;
var world = new World(200, 200, 0.01, speed);
var infection = new Infection(10, 0.05, 5);
var mobility = new Mobility(1, 10);
world.infection = infection;
world.mobility = mobility;
world.init();
163 changes: 163 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//
// classes
//
class World {
public width: number
public height: number
public denisty: number
public infection: Infection
public mobility: Mobility
public framerate: number
private personCount: number;
private persons: any;
public temp: number; //temp
constructor(width: number, height: number, denisty: number, framerate: number) {
this.width = width
this.height = height
this.denisty = denisty
this.personCount = Math.round(width * height * denisty);
console.log("person count:" + this.personCount)
this.temp = 0;
this.persons = [];
this.framerate = 1000 / framerate;
}
init() {
let canvas = document.createElement('canvas');
canvas.id = "canvas";
canvas.width = this.width;
canvas.height = this.height;
canvas.style.zIndex = "1";
canvas.style.position = "absolute";
canvas.style.border = "1px solid";
let body = document.getElementsByTagName("body")[0];
body.appendChild(canvas);
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, "uninfected");
this.persons.push(person);
}
this.persons[0].state = "infected";
this.animationStep();
}
animationStep() {
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")
{
if (this.persons[a].xPosition == this.persons[b].xPosition)
{
if (this.persons[a].yPosition == this.persons[b].yPosition)
{
this.persons[b].state = "infected";
}
}
}
}
}
for (let a: number = 0; a < this.persons.length; a++) {
this.persons[a].move();
this.persons[a].draw(ctx);
}
this.temp++;
setTimeout(() => {
this.animationStep()
console.log(this.temp)
}, this.framerate);
};
}
class Infection {
public duration: number
public mortality: number
public reach: 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 state: string;
constructor(xPosition: number, yPosition: number, xPositionMax: number, yPositionMax: number, infection: Infection, state: string = "uninfected") {
this.xPosition = xPosition
this.yPosition = yPosition
this.infection = infection
this.xPositionMax = xPositionMax;
this.yPositionMax = yPositionMax;
this.state = state
}
move() {
if (this.state != "deceased") {
this.xPosition = this.xPosition - randomIntFromInterval(-10, 10);
this.yPosition = this.yPosition - randomIntFromInterval(-10, 10);
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;
}
}
}
setState(state: string) {
this.state = state;
}
draw(ctx: any) {
ctx.beginPath();
ctx.arc(this.xPosition, this.yPosition, this.infection.reach, 0, 360);
ctx.stroke();
if (this.state == "uninfected") {
ctx.fillStyle = "blue";
}
if (this.state == "infected") {
ctx.fillStyle = "orange";
}
if (this.state == "recovered") {
ctx.fillStyle = "green";
}
if (this.state == "deceased") {
ctx.fillStyle = "red";
}
ctx.fill();
}

}
//
// helper functiona
//
function randomIntFromInterval(min: number, max: number) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
//
// let's do it...
//
let speed: number = 30;
let world = new World(200, 200, 0.01, speed)
let infection = new Infection(10, 0.05, 5)
let mobility = new Mobility(1, 10);
world.infection = infection;
world.mobility = mobility;
world.init();
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>

<html>

<head>
<title>Minimalistic infection simulation based on very few variables.</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<script src="app.js"></script>
</body>

</html>
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "glu-cc-typescript-oop",
"version": "1.0.0",
"description": "Corona Challenge in TypeScript - object oriented approach",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"tsc": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/martijnkunstman/GLU-cc-typescript-oop.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/martijnkunstman/GLU-cc-typescript-oop/issues"
},
"homepage": "https://github.com/martijnkunstman/GLU-cc-typescript-oop#readme",
"dependencies": {
"typescript": "^3.9.3"
}
}
6 changes: 6 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
html,
body {
margin: 0;
height: 100%;
overflow: hidden
}
Loading

0 comments on commit 7f2cd3d

Please sign in to comment.