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

If I have a rectangle { x = 25, y = 135, width = 30, height = 15 }, and a random point { x = 235, y = 320 }, how can I find the point along the edge of the rectangle that is between the center of the rectangle, and the random point?

For example if I had the rectangle { x = 5, y = 30, width = 10, height = 20 } and I had the random point { x = 10, y = 55 }, the point I would be looking for is { 10, 30 }. How can I figure this out mathematically however?


share|cite|improve this question
Where is the rectangle located in the plane? Is the bottom left corner at $(0,0)$, for example? – Tara B Mar 3 '13 at 0:32

All you have to do is get the equations for the sides of the rectangle and the equation of the line though the center of the rectangle and the random point. Then find where this line intersects the line for the sides of the rectangle by equating the equations. Then eliminate the intersection points which are actually outside the rectangle but on the line if you extend the sides. Checking the solutions part is easier if you write the equations of the lines using vectors/parametric equation and normalize the parameter $t$ so that the intersection must be in $t \in [0, 1]$.

share|cite|improve this answer

What you want to do is to find the crossing points of the line segment from the center and the given random point and 4 line segments of the rectangle. You can apply this algorithm to each pair of line segments to check if they are crossed.

share|cite|improve this answer

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.