-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlob.pde
More file actions
176 lines (144 loc) · 3.89 KB
/
Blob.pde
File metadata and controls
176 lines (144 loc) · 3.89 KB
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
class Blob{
private float minX, minY, maxX, maxY;
private int id;
public int lifeSpanConstant = 200;
private int lifeSpan = lifeSpanConstant;
private int distConstant = 35, distConst2 = 5;
private int lifeSpeed = 2;
public boolean isTaken = false, isAlone = true;
public Blob(float x, float y){
minX = x;
minY = y;
maxX = x;
maxY = y;
}
public Blob(float x, float y, int id){
minX = x;
minY = y;
maxX = x;
maxY = y;
this.id = id;
}
public void add(int x, int y){
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
public PVector getCenter(){
float cx = minX + (maxX - minX)/2;
float cy = minY + (maxY - minY)/2;
return new PVector(cx, cy);
}
public void display(){
stroke(0);
fill(255);
strokeWeight(2);
rectMode(CORNERS);
rect(minX, minY, maxX, maxY);
}
public void display(int i){
stroke(0);
fill(255, 130);
strokeWeight(2);
rectMode(CORNERS);
rect(minX, minY, maxX, maxY);
textAlign(CENTER);
textSize(34);
fill(0);
text(i, minX + (maxX - minX)/2, (minY + (maxY - minY)/2) + 10);
}
public void displayId(){
stroke(0);
fill(255, 130);
strokeWeight(2);
rectMode(CORNERS);
rect(minX, minY, maxX, maxY);
textAlign(CENTER);
textSize(34);
fill(0);
text(this.id, minX + (maxX - minX)/2, (minY + (maxY - minY)/2) + 10);
}
public void displayIdAndLifeSpan(){
stroke(0);
fill(255, 130);
strokeWeight(2);
rectMode(CORNERS);
rect(minX, minY, maxX, maxY);
textAlign(CENTER);
textSize(34);
fill(0);
text(this.id, minX + (maxX - minX)/2, (minY + (maxY - minY)/2) + 10);
text(this.lifeSpan, minX + (maxX - minX)/2, minY);
}
public void displayIdAndPosition(){
stroke(0);
fill(255, this.lifeSpan);
strokeWeight(2);
rectMode(CORNERS);
rect(minX, minY, maxX, maxY);
textAlign(CENTER);
textSize(34);
fill(0);
text(this.id, minX + (maxX - minX)/2, (minY + (maxY - minY)/2) + 10);
text("(" + (int) this.getCenter().x + ", ", minX + 30 , minY);
text((int) this.getCenter().y + ")", minX + 110, minY);
}
public float getMinX(){
return minX;
}
public float getMaxX(){
return maxX;
}
public float getMinY(){
return minY;
}
public float getMaxY(){
return maxY;
}
public boolean isNear(int x, int y){// from the nearest edge of the blob and the given x, y "clamping"
float cx = max(min(x, maxX), minX);
float cy = max(min(y, maxY), minY);
if(distSq(cx, cy, x, y) < distConstant*distConstant){
return true;
}else return false;
}
public boolean isNear(Blob b){// from the center of the given blob and the center of "this" blob
PVector myCenter = this.getCenter();
PVector bCenter = b.getCenter();
if(distSq(myCenter.x, myCenter.y, bCenter.x, bCenter.y) < distConst2*distConst2){
return true;
}else return false;
}
public void become(Blob newBlob){
this.minX = newBlob.getMinX();
this.maxX = newBlob.getMaxX();
this.minY = newBlob.getMinY();
this.maxY = newBlob.getMaxY();
}
public int getID(){
return this.id;
}
public void setID(int id){
this.id = id;
}
public void setLifeSpanConstant(int lifeConst){
this.lifeSpanConstant = lifeConst;
}
public void resetLife(){
this.lifeSpan = lifeSpanConstant;
}
public int getLifeSpan(){
return this.lifeSpan;
}
public float area(){
return (maxX - minX) * (maxY - minY);
}
public boolean isDead(){
this.lifeSpan -= lifeSpeed;
return (this.lifeSpan < 0);
}
private float distSq(float x1, float y1, float x2, float y2){ // as this function doesn't have sqrt so it is much faster than dist()
return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
}
}