[Math] find the point at which the line joining two points intersects the given square

geometry

Could anyone point me to some equation to find the point p(s1,s2), which is the point of intersection of a line segment with points Q(x1,y1), which is outside the square and the center of the Square C(x2,y2).

Help appreciated.
Here's the image for reference

http://tinypic.com/r/f37y8j/7

Best Answer

If $|x_1 - x_2| > |y_1 - y_2|$, then the line will intersect the square in one of the sides, left or right. Then if $x_1 > x_2$, the point of intersection is on the left, so the point will be some $(x,y)$ with $x = x_2 - 15$. Since it is on the line $(x_2,y_2) + \lambda (x_2 - x_1, y_2 - y_1)$ we know that for some $\lambda$, $x_2 - 15 = x_2 + \lambda(x_2 - x_1)$ and $y = y_2 + \lambda(y_2 - y_1)$. The first equality gives you $\lambda = 15/(x_1 - x_2)$ so that the second equality gives you $y$, i.e. $y = y_2 - 15\frac{y_1 - y_2}{x_1 - x_2}$. So the point of intersection is $(x_2 - 15, y_2 - 15\frac{y_1 - y_2}{x_1 - x_2})$.

You can do the same for $x_1 < x_2$, when you get $x = x_2 + 15$, while for $|x_1 - x_2| < |y_1 - y_2|$ you know that the intersection is either at the top or at the bottom of the square, giving a similar analysis.