Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|cite|improve this question
...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
up vote 0 down vote accepted

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.

share|cite|improve this answer
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. – Abin Aug 26 '11 at 4:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.