Calculate the center point of the highest density of xy points

139 Views Asked by At

I have a square grid (200 x 200). I would like users to be able to vote on their next desired position of an object in this space. When a user votes I add the XY position to an array.

Currently to find the "winning" position I just take an average of X and an average of Y, but what I've found is the winning position tends to gravitate around the middle of the area (naturally as it's an average!).

Average

White = votes, Yellow = "winning" XY point

What I'd prefer is if it found the densest area of votes, and chose a point in the middle of that.

I.e single scattered votes don't pull the winning position too far from the dense area like an average does.

Is that possible?

Example array

[ { x: 39, y: 28 },
  { x: 69, y: 33 },
  { x: 51, y: 51 },
  { x: 14, y: 63 },
  { x: 75, y: 140 },
  { x: 171, y: 68 },
  { x: 140, y: 53 },
  { x: 173, y: 150 },
  { x: 123, y: 179 },
  { x: 103, y: 150 },
  { x: 145, y: 119 },
  { x: 92, y: 85 },
  { x: 58, y: 49 },
  { x: 28, y: 65 },
  { x: 41, y: 39 },
  { x: 46, y: 41 },
  { x: 49, y: 51 },
  { x: 43, y: 55 },
  { x: 35, y: 48 },
  { x: 29, y: 31 },
  { x: 68, y: 22 },
  { x: 58, y: 39 } ]
1

There are 1 best solutions below

1
On

You could fit your data to a normal distribution and use the generated mean as your winning point. That wouldn't handle e.g. 2 isolated clumps too well though, the mean would be somewhere in the middle of the two clumps.

Fitting data to a distribution is a standard thing to do in statistics and there's much you can read on the topic. There's also likely to be several libraries for you to use with your chosen language, so you won't need to understand every detail if you just want something that works.