Skip to content

Commit

Permalink
Intermediate commit for Nearest Neighbor computation. Distance calcul…
Browse files Browse the repository at this point in the history
…ation wrong, debugging not proper, needs polishing
  • Loading branch information
muraliavarma committed Feb 1, 2013
1 parent e082e6e commit 3e339fa
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 12 deletions.
31 changes: 26 additions & 5 deletions murali_varma_hw2/Creature.pde
@@ -1,6 +1,8 @@
Creature[] creatures;

class Creature {
int idx;

float posX;
float posY;

Expand All @@ -9,18 +11,37 @@ class Creature {

float radius = 10;

Creature() {
ArrayList neighbors;

Creature(int i) {
idx = i;
posX = random(1);
posY = random(1);
velX = 0.01 - random(0.02);
velY = 0.01 - random(0.02);
velX = 0.001 - random(0.002);
velY = 0.001 - random(0.002);

neighbors = new ArrayList();
}

void draw() {
if (idx == 0) {
println (neighbors);
arc(SCREEN_WIDTH * posX, SCREEN_HEIGHT * posY, FLOCK_CENTERING_RADIUS * SCREEN_WIDTH, FLOCK_CENTERING_RADIUS * SCREEN_HEIGHT, 0, 2 * PI);
}
if (idx == 0) {
fill (100, 0, 0);
}
else if (neighbors.contains(0)) {
fill (0, 100, 0);
}
else {
fill (255);
}
arc(SCREEN_WIDTH * posX, SCREEN_HEIGHT * posY, radius, radius, 0, 2 * PI);
}

void update() {
neighbors = getNeighbors(FLOCK_CENTERING_RADIUS);
applyForces();
posX += velX;
posY += velY;
Expand Down Expand Up @@ -57,7 +78,7 @@ class Creature {

}

Creature[] getNeighbors(float radius) {
return null;
ArrayList getNeighbors(float radius) {
return getNearestNeighbors(idx, radius);
}
};
28 changes: 28 additions & 0 deletions murali_varma_hw2/Neighbors.pde
Expand Up @@ -21,4 +21,32 @@ void computeNeighborGrids() {
}
}

}

ArrayList getNearestNeighbors(int idx, float radius) {
ArrayList ret = new ArrayList();
int x = int(creatures[idx].posX/radius);
int y = int(creatures[idx].posY/radius);

//this is valid only for reflecting walls
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (x + i < 0 || y + j < 0 || x + i >= 1.0/radius || y + j >= 1.0/radius) {
continue;
}
ArrayList cellCreatures = (ArrayList)flockCenterGrid.get((x + i) + "," + (y + j));
if (cellCreatures == null) {
continue;
}
for (int k = 0; k < cellCreatures.size(); k++) {
Creature cellCreature = creatures[int(cellCreatures.get(k).toString())];
float diffX = cellCreature.posX - creatures[idx].posX;
float diffY = cellCreature.posY - creatures[idx].posY;
if (diffX * diffX + diffY * diffY <= FLOCK_CENTERING_RADIUS * FLOCK_CENTERING_RADIUS) {
ret.add(cellCreature.idx);
}
}
}
}
return ret;
}
Binary file modified murali_varma_hw2/build-tmp/murali_varma_hw2$Creature.class
Binary file not shown.
Binary file modified murali_varma_hw2/build-tmp/murali_varma_hw2.class
Binary file not shown.
61 changes: 55 additions & 6 deletions murali_varma_hw2/build-tmp/source/murali_varma_hw2.java
Expand Up @@ -51,7 +51,7 @@ public void drawBackground() {
public void initCreatures() {
creatures = new Creature[NUM_CREATURES];
for (int i = 0; i < NUM_CREATURES; i++) {
creatures[i] = new Creature();
creatures[i] = new Creature(i);
}
}

Expand All @@ -78,6 +78,8 @@ public void draw() {
Creature[] creatures;

class Creature {
int idx;

float posX;
float posY;

Expand All @@ -86,18 +88,37 @@ class Creature {

float radius = 10;

Creature() {
ArrayList neighbors;

Creature(int i) {
idx = i;
posX = random(1);
posY = random(1);
velX = 0.01f - random(0.02f);
velY = 0.01f - random(0.02f);
velX = 0.001f - random(0.002f);
velY = 0.001f - random(0.002f);

neighbors = new ArrayList();
}

public void draw() {
if (idx == 0) {
println (neighbors);
arc(SCREEN_WIDTH * posX, SCREEN_HEIGHT * posY, FLOCK_CENTERING_RADIUS * SCREEN_WIDTH, FLOCK_CENTERING_RADIUS * SCREEN_HEIGHT, 0, 2 * PI);
}
if (idx == 0) {
fill (100, 0, 0);
}
else if (neighbors.contains(0)) {
fill (0, 100, 0);
}
else {
fill (255);
}
arc(SCREEN_WIDTH * posX, SCREEN_HEIGHT * posY, radius, radius, 0, 2 * PI);
}

public void update() {
neighbors = getNeighbors(FLOCK_CENTERING_RADIUS);
applyForces();
posX += velX;
posY += velY;
Expand Down Expand Up @@ -134,8 +155,8 @@ public void applyForces() {

}

public Creature[] getNeighbors(float radius) {
return null;
public ArrayList getNeighbors(float radius) {
return getNearestNeighbors(idx, radius);
}
};

Expand All @@ -162,6 +183,34 @@ public void computeNeighborGrids() {
}
}

}

public ArrayList getNearestNeighbors(int idx, float radius) {
ArrayList ret = new ArrayList();
int x = PApplet.parseInt(creatures[idx].posX/radius);
int y = PApplet.parseInt(creatures[idx].posY/radius);

//this is valid only for reflecting walls
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (x + i < 0 || y + j < 0 || x + i >= 1.0f/radius || y + j >= 1.0f/radius) {
continue;
}
ArrayList cellCreatures = (ArrayList)flockCenterGrid.get((x + i) + "," + (y + j));
if (cellCreatures == null) {
continue;
}
for (int k = 0; k < cellCreatures.size(); k++) {
Creature cellCreature = creatures[PApplet.parseInt(cellCreatures.get(k).toString())];
float diffX = cellCreature.posX - creatures[idx].posX;
float diffY = cellCreature.posY - creatures[idx].posY;
if (diffX * diffX + diffY * diffY <= FLOCK_CENTERING_RADIUS * FLOCK_CENTERING_RADIUS) {
ret.add(cellCreature.idx);
}
}
}
}
return ret;
}
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "murali_varma_hw2" };
Expand Down
2 changes: 1 addition & 1 deletion murali_varma_hw2/murali_varma_hw2.pde
Expand Up @@ -36,7 +36,7 @@ void drawBackground() {
void initCreatures() {
creatures = new Creature[NUM_CREATURES];
for (int i = 0; i < NUM_CREATURES; i++) {
creatures[i] = new Creature();
creatures[i] = new Creature(i);
}
}

Expand Down

0 comments on commit 3e339fa

Please sign in to comment.