Search code examples
javagroupinghierarchical-clusteringvecmath

“Group” vecmath Point objects based on the distance between them


I have a load of vecmath Point objects (Point3d FWIM) which I would like to “group” based on the distance between them. I can probably write the code for this from scratch (I’ve done similar tasks in excel), but I like the idea of using existing libraries where possible. The problem is I can’t find any libraries like this.

I haven’t thought through the exact algorithm fully, but I hope I’ve done enough for the question not to be deleted. Please bear with me, I'm still new here at the time of this post.

I imagine the grouping would work as follows:

  1. decide the distanceLimit
  2. loop 1: for each Point, calculate the distance to each other Point
  3. Make a "Set"
  4. loop 2: for each Point

    • if the next Point is within the distanceLimit of any previously considered Points up to i, add it to current "Set"

    • Else make a new "Set".

Edit: ah the power of verbalising one's ideas. The above doesn't the capture the situation where points 1 and 2 are between one and two distanceLimits apart and initiate separate "sets", and point 3 crops up halfway between them meaning that all three should be in one set really. Need to think about this some more!

I’m also not sure yet what data structures I should really use for the input and output (ArrayLists? Sets?). Ideally I am looking for an existing library that does this or similar; if you’re confident there isn’t one, then any suggestions for the algorithm or the actual code would be more than welcome.


Solution

  • After lots more googling, I found that:

    • what I was trying to do is called clustering;
    • this did exactly what I was trying to do; I was impressed with how well it worked for me.