# Compute intersection coordinates of two vectors moving on different speeds

The red dot is moving at speed 400 in direction of the green dot.
I want to move the yellow dot to the coordinates of the red dot after the time needed for the yellow dot to reach the position.

In other words, I want the yellow dot to intercept the red one to make them overlap before the red dot reaches the green dot coordinates.

I'm using this function to get the distance between dots:

function getDistance(a, b) {
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}


And this one to normalize the distances taking into account the speeds:

function normalizeDistance(distance, speed) {
return distance / speed;
}


But I can't find out how to obtain the information I need.

I logically know the coordinates of the 3 dots and I know that the red dot is going to head in the coordinates of the green one.
The green dot is static.

How can I do?

Edit:

To clarify, here's simpler examples for the same problem:

• I was thinking I could take all the points in the red-green segment and test against each of them. But this would be very compute intensive and far from clean... – Fez Vrasta Jul 5 '17 at 14:37
• You can certainly narrow "all points" down to "red point, green point, take a guess based on how wrong those results are, repeat until error is acceptably small." – Niet the Dark Absol Jul 5 '17 at 15:17

Let us describe the red dot's position at time $t$ by

$$x_R(t) = R_0 + 400t\vec{R}$$

where $R_0$ is the initial position of the red dot and $\vec{R}$ is the direction it is moving in. Similarly we can do this for the yellow dot with

$$x_Y(t) = Y_0 + 1000t\vec{Y}$$

I will asssume $R_0, Y_0, \vec{R}, \vec{Y} \in \mathbb{R}^2$ by your drawing. If we want the yellow dot to collide with the red dot, we must satisfy

$$R_0 + 400\lambda \vec{R} = Y_0 + 1000\lambda\vec{Y}, \quad \lambda \in \mathbb{R}^+$$ $$\Rightarrow R_{0_1} + 400\lambda r_1 = Y_{0_1} + 1000\lambda y_1 \text{ AND } R_{0_2} + 400\lambda r_2 = Y_{0_2} + 1000\lambda y_2$$ where I have used $R_0 = (R_{0_1}, R_{0_2}), Y_0 = (Y_{0_1}, Y_{0_2}), \vec{R} = (r_1, r_2),$ and $\vec{Y} = (y_1, y_2)$. Assuming that the red dot and yellow dot will collide before the red dot reaches the green dot, you just need to satisfy one of the above. Let us do it for the first component:

$$R_{0_1} + 400\lambda r_1 = Y_{0_1} + 1000\lambda y_1$$ $$\Rightarrow \frac{R_{0_1} - Y_{0_1}}{1000y_1 - 400r_1} = \lambda$$

We now know where the red and yellow dot will intercept. Therefore you just need to compute $x_Y(\lambda)$ and $x_R(\lambda)$ to get their intersection coordinates. Note though that $x_Y(\lambda) = x_R(\lambda)$ since they are colliding at time $\lambda$.

However, we must check when they collide. First let us calculate the time it takes for the red dot to collide with the green dot:

$$\sqrt{\frac{(R_{0_1} - G_{0_1})^2 + (R_{0_2} - G_{0_2})^2}{r_1^2 + r_2^2}} = \lambda^\prime$$

Now we have the following situations:

$$\lambda < 0 \text{ Red and yellow dot do not intersect}$$ $$0 \leq \lambda < \lambda^\prime \text{ The red and yellow dot collide before it hits the red}$$ $$0 \leq \lambda^\prime < \lambda \text{ The red and yellow dot collide after the red passes the green}$$

I will leave it up to you to fill out the missing code for calculating $\lambda^\prime$ and comparing the result to $\lambda$.

Relevant Matlab code:

//Returns the vector intersection where the red and yellow point collide
function intersection = getIntersection(r0, y0, R, Y) {
lambda = (r0[1] - y0[1]) / (1000 * Y[1] - 400 * R[1]);
intersection = (r0 + 400 * lambda * R );
}

• Thank you so much! I'm sorry but I'm not that good with math so I don't really know if your solution will work or not... I'll have to find a way to convert your formulas into pseudo-code computations 🤔 – Fez Vrasta Jul 5 '17 at 19:37
• Edited it to include Matlab code. r0, y0 are the initial position vectors and R, Y are their direction vectors. – David Jul 5 '17 at 19:43
• Thank you! What happens if they can't collide before the red dot reaches the green one? – Fez Vrasta Jul 5 '17 at 19:45
• What I gave you technically computes the intersection of the two lines. I have just assumed that they will collide before the red dot reaches the green dot. – David Jul 5 '17 at 19:47
• I have edited the answer once more. Read over it and let me know if you have any questions. – David Jul 5 '17 at 19:58