# find the point at which the line joining two points intersects the given square

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

-
...construct the linear equation corresponding to the appropriate side of the square, and the line through two points, and then solve the two simultaneous equations? – J. M. Aug 25 '11 at 18:09
Latin square tag doesn't fit. Not sure why rational points either. You can't find an equation for the intersection without knowing the sides or vertices of the square - how big a square is it and what orientation? The basic procedure would be to work out first which side the line would first intersect (draw lines from centre of squares to corners and extend, work out which quadrant you are in) and then use the formula for the intersection of your line with the relevant side. Note that in general a line through a square – Mark Bennet Aug 25 '11 at 18:10
Note that in general a line through a square will intersect all sides (as extended), two of which will be (unextended) sides of the square. A line through the centre will intersect opposite (unextended) sides in two points which are symmetrically related. – Mark Bennet Aug 25 '11 at 18:15
Its a perfect square of 30 by 30 pixels. Point Q moves around the square. Need to find the point "p" in coordinates. – Abin Aug 25 '11 at 18:26

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.