For Manhattan distance:
Simply take the middle x-coordinate and the middle y-coordinate of the 3 points as the target. By middle I mean the median of the sorted coordinates.
Example:
Input: (1,5), (2,4), (3,6)
The x-points are 1
, 2
and 3
(sort to 1
, 2
, 3
). The middle one is 2
.
The y-points are 5
, 4
and 6
(sort to 4
, 5
, 6
). The middle one is 5
.
Thus the point that minimizes the distance is (2,5)
.
Proof:
If we were to start at the above mentioned point and move in any direction, the distance to the point we're moving towards would decrease and the distance to the other 2 points would increase, thus every move by 1 would cause a total decrease of 1 but a total increase of 2. Once we've moved past the last point, all 3 distances would increase, thus the total increase will be 3 with no decrease. Thus any move causes an increase in distance, thus the above point is optimal.
For Euclidean distance:
(I realize now that you said minimum moves, thus this is probably not what you want)
This is somewhat more complicated. It involves minimizing this equation:
sqrt((x-x1)^2 + (y-y1)^2) + sqrt((x-x2)^2 + (y-y2)^2) + sqrt((x-x3)^2 + (y-y3)^2)
Because of the sqrt
's, one cannot simplify the equation by saying that x
and y
are distinct.
However, the greedy approach is likely to work:
- Pick any point (maybe the one that minimizes the Manhattan distance is a good start).
- While one of the neighbouring points of the picked point results in lower euclidean distance, pick that point.