call(back)
Algorithms & Data StructuresmediumSign in to star problems

When Do the Chasing Points Meet?

N points sit in the plane, indexed 0..N-1 in a cycle: at every instant point i moves at speed 1 straight toward the current position of point (i + 1) mod N. All points move at the same time and continuously.

Return [T, X, Y]: the time T at which the points all meet, and where. Each case carries a reference answer, and the grader accepts a result within 0.02 of it on every component, so a careful simulation passes; aim for 1e-4 anyway. Numerical simulation is acceptable as long as it is accurate and fast.

points = [[0, 0], [10, 0]]
->  [5, 5, 0]      // two points walk straight at each other

points = [[0, 0], [2, 0], [2, 2], [0, 2]]
->  [2, 1, 1]      // a square spirals into its centre

2 <= N <= 200; coordinates are real numbers with absolute value at most 1000. The interviewer's version also allowed the answer NEVER; be ready to argue whether it can ever come up.

Hints

Hint 1

Time-step: for each point compute the unit vector toward its target from the current positions, then move every point by speed * dt at once. Updating points one at a time inside a step biases the result.

Hint 2

A fixed dt is either too slow or too coarse. Shrink the step as the points close in, for example dt = min(0.001, minDistance / 10), and stop when every point is within a small tolerance of every other.

Hint 3

For N = 2 the answer is the midpoint at distance / 2; for a regular polygon with side s, T = s / (1 - cos(2π / N)). Use these to check your simulation.

Topics

Asked at

Similar Problems6