1
+ /** Pasvorn Boonmark Copyright May 2, 2020
2
+ * People class to mimic random people behavior moving withing a 2D plane
3
+ * The first portion of a People's properties are constant definding boundary
4
+ * values that governs one's behaviors. The are defined are follow:
5
+ * MAXVEL = maximum velocity that one can move per frame - unit in pixel
6
+ * Blimit = boundary limit - use for detecting if a People is within contact
7
+ * range of another People
8
+ * get_well_day = when a People is sick, how many "days he will get better
9
+ * the value is in number of frame since one got sick
10
+ * MAXBOUND = govern a boundary that one travel. This is artificial number.
11
+ * according to US Transport survey, we travel 40 miles per day on an
12
+ * average. So this should reflect that - see www.bts.gov - 2017-05-31
13
+ * The following are color of a People based on one's status:
14
+ * - well_color when one is well
15
+ * - sick_color when one is sick
16
+ * - immune_color when one is immune
17
+ * The state diagram of a Person is as follow:
18
+ * Current State | next states | Trigger
19
+ * well sick | contact a sick People with 40% chance
20
+ * sick | immune | after sick for "get_well_day"
1
21
22
+ */
2
23
class People {
3
24
4
- final float MAXVEL = 10 ;
5
- final int blimit = 5 ;
25
+ final float MAXVEL = 3 ;
26
+ final int Blimit = 5 ;
6
27
final int get_well_day = 140 ;
7
28
final int MAXBOUND = 300 ;
8
29
final int well_color = 0x4FFFFFFF ;
9
30
final int sick_color = 0x4FFF0000 ;
10
31
final int immune_color = 0x8F00ff00 ;
32
+ final float sick_v_factor = 0.1 ;
11
33
12
34
int myindex;
35
+ boolean debug = false ;
13
36
float xlocation, ylocation;
14
37
float xbound[] = new float [2 ];
15
38
float ybound[] = new float [2 ];
@@ -22,55 +45,69 @@ class People {
22
45
boolean immune = false ;
23
46
int mycolor;
24
47
float vfactor = 1.0 ;
25
-
26
- People ( int index ) {
48
+
49
+ //
50
+ People ( int index ) {
51
+ // initialize a People's properties
27
52
xlocation = random (0 , width );
28
53
ylocation = random (9 , height );
54
+ // how fast one traveled
29
55
xvel = random (- MAXVEL , MAXVEL );
30
56
yvel = random (- MAXVEL , MAXVEL );
31
- float xspan = random (10 , MAXBOUND );
32
- float yspan = random (10 , MAXBOUND );
57
+
58
+ // Boundary a Person can be - essentially allocated 2xrandom number
59
+ float xspan = random (50 , MAXBOUND );
60
+ float yspan = random (50 , MAXBOUND );
33
61
xbound[0 ] = xlocation - xspan;
34
62
xbound[1 ] = xlocation + xspan;
35
63
ybound[0 ] = ylocation - yspan;
36
64
ybound[1 ] = ylocation + yspan;
37
65
mycolor = well_color;
38
66
myindex = index;
39
67
gotsick = 0 ;
40
- }
41
-
42
- void move (int framenumber ) {
68
+ }
69
+
70
+ // when a People moves - check to make sure one is within one's bound
71
+ void move (int framenumber ) {
43
72
getbetter(framenumber);
44
73
xlocation += xvel* vfactor;
45
74
ylocation += yvel* vfactor;
46
- if (xlocation > xbound[1 ] | xlocation < xbound[0 ] ) xvel *= - 1.0 ;
47
-
48
- if (ylocation > ybound[1 ] | ylocation < ybound[0 ]) yvel *= - 1.0 ;
49
-
50
- }
51
-
52
- boolean meet (People OtherPeople ) {
75
+ if (xlocation > xbound[1 ] | xlocation < xbound[0 ] )
76
+ xvel *= - 1.0 ;
77
+ if (ylocation > ybound[1 ] | ylocation < ybound[0 ])
78
+ yvel *= - 1.0 ;
79
+ }
80
+
81
+ // meet - when one People meets another People
82
+ boolean meet (People OtherPeople ) {
53
83
if (myindex == OtherPeople . myindex) return false ;
54
- boolean result = ( abs (xlocation - OtherPeople . xlocation) < 5 &
55
- abs (ylocation - OtherPeople . ylocation) < 5 );
56
- // if (result) println(" meet: ", myindex, OtherPeople.myindex);
84
+ boolean result = ( abs (xlocation - OtherPeople . xlocation) < Blimit &
85
+ abs (ylocation - OtherPeople . ylocation) < Blimit );
86
+ if (debug & result)
87
+ println (" meet: " , myindex, OtherPeople . myindex);
57
88
return result;
58
- }
59
-
60
- void contact (People OtherPeople ) {
61
- if ( sicked() ) {
89
+ }
90
+
91
+ // giving a chance, will a People gets sick
92
+ boolean get_sick_chance (float chance ) {
93
+ return random (0 ,1.0 ) < chance;
94
+ }
95
+
96
+ // contact - this is when contact is made. People can get sick here
97
+ void contact (People OtherPeople ) {
98
+ if ( sicked() ) {
62
99
if ( ! OtherPeople . sicked() ) {
63
- if ( random ( 0 , 1.0 ) < 0.4 ) {
64
- OtherPeople . getsick(framenumber);
65
- spreader++ ;
66
- }
100
+ if ( get_sick_chance( 0.4 ) ) {
101
+ OtherPeople . getsick(framenumber);
102
+ spreader++ ;
103
+ }
67
104
}
68
- } else if ( OtherPeople . sicked()) {
69
- if ( random ( 0 , 1.0 ) < 0.4 ) {
70
- getsick(framenumber);
71
- OtherPeople . spreader++ ;
72
- }
73
- }
105
+ } else if ( OtherPeople . sicked()) {
106
+ if ( get_sick_chance( 0.4 ) ) {
107
+ getsick(framenumber);
108
+ OtherPeople . spreader++ ;
109
+ }
110
+ }
74
111
}
75
112
76
113
void getsick (int framenumber ) {
@@ -80,7 +117,6 @@ class People {
80
117
sickedat = framenumber;
81
118
getwellat = framenumber + random (120 ,200 );
82
119
// println(myindex, " got sicked at ", framenumber);
83
- vfactor = 0.4 ;
84
120
}
85
121
}
86
122
@@ -104,6 +140,8 @@ class People {
104
140
105
141
void draw () {
106
142
fill (mycolor);
143
+ textSize (16 );
144
+ text (spreader,xlocation+ 5 , ylocation);
107
145
circle(xlocation,ylocation,10 );
108
146
}
109
147
0 commit comments