Experiments in Data Visualization









void ofApp::calculatePositions(){
//Note: Using Flocking Algorithm
//PART 1 - Calculate Center of Mass
string location = result[thingIGrabbed]["location"].asString();
ofPoint center;
int numberOfPoints = 0;
for (int i : nodeMap[location]) {
center += pos[i];
numberOfPoints++;
}
center = center / numberOfPoints;
ofPoint cohesion_velocity = (center - pos[thingIGrabbed])/1.0f;
//Part 2 - Calculate Distance between each node and itself
ofPoint displacement;
for (int i : nodeMap[location]) {
if (i != thingIGrabbed) {
float dist = ofDist(pos[i].x, pos[i].y, mouseX, mouseY);
if(dist < 10.0){
displacement = displacement - (pos[i] - ofPoint(mouseX, mouseY));
}
}
}
ofPoint separation_velocity;
separation_velocity = displacement;
//Part 3 - Add Velocity
ofPoint alignment_velocity;
if (previousGrabbedPosition.x != 0 || previousGrabbedPosition.y != 0) {
// v = (x2 - x1)/t ==> but we assign time = 1
vel[thingIGrabbed] = ofPoint(mouseX, mouseY) - previousGrabbedPosition;
ofPoint avgVel;
for (int i : nodeMap[location]) {
avgVel += vel[i];
}
alignment_velocity = avgVel / (numberOfPoints + 1) / 1.0f;
}
previousGrabbedPosition = ofPoint(mouseX, mouseY);
ofPoint swarmV = -cohesion_velocity + separation_velocity + alignment_velocity;
for (int i : nodeMap[location]) {
pos[i] += swarmV;
}
}