-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirusPopulation.inl
180 lines (138 loc) · 5 KB
/
VirusPopulation.inl
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
177
178
179
180
/* Interactive Gym Environment and Educational Kit (iGeek) @version 0.x
@link https://github.com/KabukiStarship/iGeek.git
@file /viruspopulation.inl
@author Cale McCollough <https://cookingwithcale.org>
@license Copyright (C) 2015-2023 Kabuki Starship <kabukistarship.com>;
All right reserved (R). This Source Code Form is subject to the terms of the
Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
this file, You can obtain one at <https://mozilla.org/MPL/2.0/>. */
#include <_Config.h>
#include "VirusPopulation.h"
namespace HomoWorld {
VirusPopulation::VirusPopulation(Host thisHost) {
host = thisHost;
viruses = new Virus[DefaultPopulationSize];
virus_count = DefaultPopulationSize;
Dimension host_dimensions = host.getPreferredSize();
FPD x, y, hostWidth = host_dimensions.width,
hostHeight = host_dimensions.height;
for (ISC i = 0; i < DefaultPopulationSize; i++) {
x = Math.random() * (FPD)hostWidth;
y = Math.random() * (FPD)hostHeight;
viruses[i] = new Virus(x, y);
}
}
// Adds the virus to the population.
void VirusPopulation::addVirus(Virus virus) {
ISC virus_count = viruses.length, i;
Virus[] virus = new Virus[virus_count + 1];
for (i = 0; i < virus_count; i++) virus[i] = viruses[i];
virus[i] = virus;
}
// Returns the virus at the specified index.
Virus VirusPopulation::getVirus(ISC index) {
if (index < 0 || index > virus_count) return null;
return viruses[index];
}
// Selects a random virus from the population.
ISC VirusPopulation::selectRandomVirus() {
if (virus_count == 0) return -1;
ISC n = 0, maxTries = 2 * virus_count, index;
do {
index = (ISC)(Math.random() * (virus_count - 1));
if (viruses[index] != null) return index;
++n;
} while (n < maxTries);
// Generate list of non-null indexes.
ISC populatedIndecies[] = new ISC[virus_count], i = 0;
for (index = 0; index < virus_count; index++) {
if (viruses[index] != null) {
populatedIndecies[i] = index;
i++;
}
}
index = (ISC)(Math.random() * (virus_count - 1));
return index;
}
/* Function returns a value greater that -1 if this population contains
polygon.
@return Returns -1 if the population doesn't contain polygon. Else it
will return the index of the Virus.
*/
ISC VirusPopulation::Contains(GeneticPolygon polygon) {
for (ISC i = 0; i < virus_count; i++) {
Virus currentVirus = viruses[i];
if (currentVirus != null &&
currentVirus.containsGeneticPolygon(polygon)) {
// Then the virus has occupies the cell
return i;
}
}
return -1;
}
// Function that selects the virus
ISC VirusPopulation::SelectVirus(ISC mouseX, ISC mouseY) {
for (ISC i = 0; i < virus_count; i++) {
// Print (i + ", "); // Debug info
Virus current = viruses[i];
if (current != null && current.containsPoint(mouseX, mouseY)) return i;
}
return -1;
}
// Returns the number of viruses.
ISC VirusPopulation::GetNumViruses() { return virus_count; }
// Updates the virus population with the new .
void VirusPopulation::Update() {
Dimension hostSize = host.getPreferredSize();
ISC width = hostSize.width, height = hostSize.height;
for (ISC i = 0; i < virus_count; i++) viruses[i].updatePosition(width, height);
for (ISC i = 0; i < virus_count; i++) {
ISC j;
for (j = 0; j < i; j++) viruses[i].collideWith(viruses[j]);
for (j++; j < virus_count; j++) viruses[i].collideWith(viruses[j]);
}
}
void VirusPopulation::Draw(Graphics g) {
Dimension host_dimensions = host.PreferredSize();
for (ISC i = 0; i < virus_count; i++) {
Virus virus = viruses[i];
ISC currentX = (ISC)virus.getX(), currentY = (ISC)virus.getY();
if (currentX + virus.Width() >= 0 && currentY + virus.Height() >= 0 &&
currentX <= host_dimensions.width && currentY <= host_dimensions.height) {
virus.draw(g);
}
}
boxCollidingViruses(g);
// for (ISC i=0; i < virus_count; i++)
//{
// for (ISC j=0; j < virus_count-1 viruses[i]
//}
}
void VirusPopulation::BoxCollidingViruses(Graphics& g) {
for (ISC i = 0; i < virus_count; i++) {
Virus a = viruses[i];
ISC j;
for (j = 0; j < i; j++) {
Virus b = viruses[j];
if (a.intersects(b)) BoxTwoViruses(g, a, b);
}
for (j++; j < virus_count; j++) {
Virus b = viruses[j];
if (a.intersects(b)) BoxTwoViruses(g, a, b);
}
}
}
void VirusPopulation::BoxTwoViruses(Graphics& g, Virus& a, Virus& b) {
ISC left = (ISC)((a.getX() < b.getX()) ? a.getX() : b.getX()),
top = (ISC)((a.getY() < b.getY()) ? a.getY() : b.getY()),
right = (ISC)((a.rightEdge() > b.rightEdge()) ? a.rightEdge()
: b.rightEdge()),
bottom = (ISC)((a.bottomEdge() > b.bottomEdge()) ? a.bottomEdge()
: b.bottomEdge());
g.setColor(new Color(1.0f, 0.0f, 0.0f, 0.5f));
g.drawRect(left - 2, top - 2, right - left + 2, bottom - top + 2);
a.drawCircle(g, Color.blue);
b.drawCircle(g, Color.green);
}
} //< namespace _
}