2
$\begingroup$

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

  • 0
    Its a perfect square of 30 by 30 pixels. Point Q moves around the square. Need to find the point "p" in coordinates.2011-08-25

1 Answers 1

0

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.

  • 0
    Excellent!!. This formula worked like a charm. I am not good at math, so I was trying for brut-force method. Programming cannot be without maths. Thanks a ton, Thijs Laarhoven for suggesting these equation.2011-08-26