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

Suppose $P_1=(x_1,y_1)$, $P_2=(x_2,y_2)$ are two points.

Also suppose that we have a rectangle which we just know the value of its sides $a$ and $b$.

I am looking for some kind of formulation which can show whether $P_1$ and $P_2$ are inside of rectangle or not.

share|cite|improve this question
What exactly do you mean by formulation? Are you looking for an algorithm? – Aryabhata Mar 21 '12 at 18:31
I am looking for a mathematical relation between a rectangle properties and points positions. – sepideh Mar 21 '12 at 18:45
Suppose $a$ is $1$, $b$ is $100$, $P_1 = (0, 0)$, and $P_2 = (10, 10)$. What are you asking in this case? Are you asking whether or not there exists a $1$ by $100$ rectangle containing both $P_1$ and $P_2$? – Tanner Swett Mar 21 '12 at 18:49
@sepideh: Many people interested in game programming think of a rectangle as having sides parallel to the edges of the screen. Is this what you mean by rectangle, or can a rectangle have arbitrary orientation? – André Nicolas Mar 21 '12 at 18:59
@sepideh: If we don't know where the rectangle is (you only gave us the side lengths) how can we know whether the points are inside it? – Ross Millikan Mar 21 '12 at 20:28

Without knowing the position of the rectangle, it is impossible to tell whether any particular point lies inside of it. The most one can say (as the commenters pointed out), is that the conditions $$|x_1-x_2|\le a \quad \text{and } \ |y_1-y_2|\le b$$ are necessary for the two points to belong to the same (axes-aligned) rectangle $a\times b$. It is also sufficient if you are allowed to move the rectangle.

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.