# How to find this line length and targeted point coordinates based on other points?

First of all I'm beginner in "advanced" math. For this reason I don't know how to compute this problem.

Consider we have a generic rectangle with width W and height H. Also, consider that inside the bounds of that rectangle exists a point B and a point M, in any coordinates inside the rectangle. Knowing this, how can we find the coordinates of a point T which lies in a line MBT and that also stays on the edge of that rectangle? The reason to find the coordinates of T is that I will can draw a line between B and T.

To help understand the problem, see this figure: example

To try to solve this, I attempted some stuff:

1. I imagined that the rectangle is living inside a circle;
2. the center of the circle coincides with the center of the rectangle, then we have the radius of the circle dividing the diagonal of the rectangle by 2;
3. Using the center point C, we can create a triangle MCB, which also have all it's sides known from distance of points formula;
4. By using it's sides is possible to find the angle at the vertice B using the cosine's law;
5. From the angle X above, we can get it's adjacent X' by subtracting it from 180 degree;
6. With the angle X', then, is possible to find the length of the line from B to a point T2 formed by the triangle CBT2 (as illustrated below), again using cosine's law;
7. After getting the length of the line BT2 called L, is possible to find the coordinates of the point T2 using the way described here: Finding a point along a line a certain distance away from another point! (being L, the related distance).

illustration

Those steps are really messy and I was only about to get the coordinate of T2 instead of T. I could go further to find T taking the fact that T is between B and T2 or even creating more triangles and so on.

Then, as I have to use those calculations in programming, are those worth it? If not (and probably not) what a fine way to find the coordinates of the point T in this scenario?

First assume that the vertices of the rectangle are $$(0,0)$$, $$(W,0)$$, $$(W,H)$$ and $$(0,H)$$. If $$B=(b_1,b_2)$$ and $$M=(m_1,m_2)$$, then the ray $$MB$$ (you need the ray in order to ensure that the points are in the order $$M,B,T$$ and not $$T,M,B$$) has parametric equation $$M+\lambda (B-M)$$, where $$\lambda \in \mathbb{R}^+$$, that is, the points of the ray are the ones of the form $$(m_1+\lambda (b_1-m_1),m_2+\lambda (b_2-m_1))$$.
Now you can find the intersection of the ray with each of the lines that contain the sides of the rectangle, if the point of intersection $$S=(s_1,s_2)$$ verifies $$0\leq s_1\leq W$$ and $$0\leq s_2\leq H$$, then you can take $$T=S$$, else, you just compute the other intersections (one of them will give you the point). For example, the point of intersection of the ray $$MB$$ with the line $$y=0$$ (if it exists) has $$m_2+\lambda (b_2-m_2)=0$$, so $$\lambda =\frac{m_2}{m_2-b_2}$$, it doesn't exists if $$m_2=b_2$$ (in that case the line $$BM$$ is parallel to the $$x$$-axis) or $$m_2 (in that case the ray "goes upwards").

If the vertices of the rectangle have given coordinates (the sides are not necessarily parallel to the coordinate axes), you can use the same idea intersecting ray $$MB$$ with each of the lines containing the sides (the equations won't be that nice, but it still works).