We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › trouble calculating centroid
Page Index Toggle Pages: 1
trouble calculating centroid (Read 549 times)
trouble calculating centroid
Feb 5th, 2009, 7:42pm
 
Hi

I 've been struggling implementing a calculation for a centroid - I had this working with another applet but with the one I'm currently working with, something just isn't working right.

the sketch is here : http://www.upsdn.ca/processing/animal_particles_06d/applet/

you can see a big ugly red circle in the top left hand corner which should be located at the centroid of all those little circles jumping around.

here's the code:

Quote:
Vector3D centroid () {
// ArrayList tempParticles = new ArrayList();


    float neighbordist = 500000.0f;
    Vector3D sum = new Vector3D(0,0,0);   // Start with empty vector to accumulate all locations
    int count = 0;

    for (int i = 1; i < z.animals.size(); i++) {
      Animal currentAnimal = (Animal) z.animals.get(i-1);      
      Animal other = (Animal) z.animals.get(i);
//      println(other.getPosition().getX());
      float d = distance(currentAnimal.position,other.position);

      if ((d > 0) && (d < neighbordist)) {
//        println(other.getX());
        sum.add(other.position); // Add location
        count++;
        
      }
    }
    if (count > 0) {
//      println("yes?");
println(sum.getY());
      sum.div((float)count);
    }
//    println(sum.getX());
    return sum;
  }
  
  float distance(Vector3D v1, Vector3D v2) {

    float dx = v1.getX() - v2.getX();
    float dy = v1.getY() - v2.getY();
    float dz = v1.getZ() - v2.getZ();
    return (float)Math.sqrt(dx * dx + dy * dy + dz * dz);

  }



can't figure out what's wrong. I think I have tried everything I cold possibly think of. if you have any ideas, I would really appreciate it...

thanks
Re: trouble calculating centroid
Reply #1 - Feb 5th, 2009, 7:55pm
 
Ok, maybe I'm missing something, but I can't figure out why you're calculating the centroid in the way that you are.

If I try to distill that piece of code into english, here's what I come up with:

For each successive pair of Animals in the array, that is with adjacent array positions, calculate the distance between them.  If the distance is less than a very large threshhold, add the second animal's position to the "sum" vector, and count how many such positions have been put into "sum".  At the end, scalar-divide "sum" by that count.

While this should be, in practice, close to the average position of all the animals it won't ever be their actual average position because animal[0] won't ever have its position accounted for.  Why not just this:

Vector3D centroid () {
   Vector3D sum = new Vector3D(0,0,0);
   for (int i = 0; i < z.animals.size(); i++)
   {  
     Animal a = (Animal) z.animals.get(i);  
     sum.add(a.position); // Add location
   }
   return sum.div((float)z.animals.size());
 }

Re: trouble calculating centroid
Reply #2 - Feb 5th, 2009, 8:10pm
 
it never completely made sense to me...but sometimes things just go over my head and I accept that..bad practice.

so i just put your code in and got a "Cannot return a void result".

but I broke it into two lines and that seemed to work. but this all still doesnt address why sum is still returning 0. No matter how much I try to debug I can't find where things are not working.

my gut says that there's something wrong with the sum.add(Vector3D) line...I can check to see that a.getX() is actually returning values but there's no real way to see whether the full vector is working, is there?
Re: trouble calculating centroid
Reply #3 - Feb 5th, 2009, 8:18pm
 
switched to sum.addToThis(Vector3D) and it worked...thanks for the help
Page Index Toggle Pages: 1