In my project, I have a 200 x 200 square grid where users can vote on the next desired position of an object within this space. Each time a user submits their vote, the XY position is added to an array.
Currently, to determine the "winning" position, I calculate the average X and Y coordinates. However, I have noticed that the winning position tends to cluster around the middle of the area (as expected with averaging).
https://i.sstatic.net/o4osS.png
In the diagram: White represents votes, Yellow represents the "winning" XY point
Instead of relying solely on averages, I would like the system to identify the densest area of votes and choose a point in the center of that cluster.
This way, isolated or scattered votes will not heavily influence the final winning position as it does with averaging.
Is there a way to implement this functionality?
Example data 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 } ]