Saturday, January 16, 2021

Virus Infection Simulator

Was talking to my S-I-L about some school projects and an idea for this came up. Out of scope from what her project required, but in scope for something to do on a day off!

Direct link to github html preview.

I started by defining a list of "cities" - x, y coordinates are simply distributed in a circle around the center of the screen.

People is an array of persons. A person is in a city (thus having that city's x, y location as a base point. The person wanders based on sin/cos curves.

e.g. lets say the person wanders around the city. Cities are arranged in a circle. we can describe a function given a time to determine the location of a person as they wander the city.

It's  not hard to think this through. A "person" will need some stats. Direction, speed, wandering constants, home city. "Cities" will have constants too - size, location. We can consider population density. Maybe some cities have mandates that reduce infection rate.

//Basic wandering idea... 

//lets give a person a random direction, clockwise or anti

persondirection=random()-0.5

//wander factor is how often the person will head to the center of a town while they run around.

wanderfactor=random()*4+2

//Given a number of cities, we can distribute cities in a circle.

cityx=sin(2*pi*personcitynumber/citycount)*cityspread

cityy=cos(2*pi*personcitynumber/citycount)*cityspread

//lets have the person walk in a circle in the direction/speed of their random pick. Time is a variable we can increment every frame. Offset is a random constant per person. It's so that that everyone doesn't seem to start at the same point at the same time.

wanderingx=sin(time*persondirection+offset)*citysize

wanderingy=cos(time*persondirection+offset)*citysize

//we can make the person wander a little more than just in a perfect circle. Sin/2 is -0.5 to 0.5. Add 0.5 and it's 0 to 1. 

centervisit=0.5+sin(time*wanderfactor+offset)/2

//add the city coordinates with the wandering x,y multiplied by the wander factor, and they'll be moving around a city location, moving to the center and back out to their normal ring around the city.

personx cityx centervisit wanderingx

persony cityy centervisit wanderingy

Fleshed out a little more, the wandering patterns looked pretty decent. Throw these values in an object per person, make an array of people, and city variables, and add some more properties like what city the person is moving to, and progress there.

See the source here

https://github.com/beomagi/html-tinkers/blob/master/virus-sim/virus-sim.html

Edit: added some color, event log and graph. See the git repo for future changes.

Thought: should make this an interactive game. Cities can have money saved that decreases if under lockdown. People can have a happiness factor that depends on safeguards and how they are doing in relation to other cities... 

2 comments:

  1. Hmm...do u add in the recovered + a mortality mortality rate?
    Everyone cant all be infected at once...
    Well that's my positive outlook on it...unless
    It's a zombie invasion :)

    ReplyDelete
    Replies
    1. Hmm, zombie invasion....
      I like that idea. You have good brrraaaaaiinnss!

      Delete