Need help optimizing this piece of code
in
Programming Questions
•
1 year ago
this function is from a script i wrote based of of jose sanchez his tutorials. its a flocking script.
this function is drawing lines between them when there within a distance of 20 pixels. but it also calculates distance between particle number 1 and particle number 1, wich is unnecessary.
and also if it already calculated the dist between particle number 1 and number 2, it does it again by calculating the dist between particle number 2 and number 1.
void lineBetween(){
for (int i = 0; i < ballCollection.size(); i++){
Ball other = (Ball) ballCollection.get(i);
float distance = loc.distanceTo(other.loc);
if(distance > 0 && distance < 20){
stroke(255);
strokeCap(SQUARE);
strokeWeight(2);
line(loc.x,loc.y,other.loc.x,other.loc.y);
}
}
}
if i would do something like
if(loc.x != other.loc.x){
line(loc.x,loc.y,other.loc.x,other.loc.y);
}
would this help ?
1