# Magnetic attraction algorithm

The algorithm used to move monitors to the nearest one in “position dialog” is explained here.

Segment equation is used. Let’s explain segment equation in order to understand the proccess:

If you have two points, p0, p1 ∈ ℝ 2, the equation of straight line between p0 and p1 is:

r = v · t + p0

where v = p1 - p0, and t ∈ [0,1].

Algorithm calculates equation of the segment between monitor centers. Then, it gets intersection points with edges of monitors:

If v = c1 - c0, c0 and c1 are monitors centers, the equation between them is r = v · t + c0.

where t1 is the parameter of the mentioned equation to get the intersection point with the c1 monitor.

where t2 the parameter with the c0 monitor.

t1, t2 ∈ [0,1].

If t2<t1, the monitors are separated, otherwise the monitors are overlapped.

With the previous information we get:

r1 = v· t1 + c0

r2 = v· t2 + c0

Iff t2<t1, c0 monitor must be moved, and its displacement is provided by r = r1-r2