I have a rectangle in $2D$ space which is determined by $2$ points (each in opposite vertice) $p_1(x,y)$ and $p_2(x,y)$ . How can I find out numerically if a other point $p(x,y)$ is lying inside plane of the rectangle?
-
How is your rectangle determined by the two points? – superAnnoyingUser Nov 18 '13 at 11:55
-
I'm not sure what you mean. It's determined just by those two points..? I'm sory for my maybe stupid question, I'm not math-genius. :) – Emetrop Nov 18 '13 at 12:02
-
1Do you mean that the two points define opposite vertices of the rectangle? And that the rectangle edges are parallel to the coordinate axes? – Daryl Nov 18 '13 at 12:11
-
Yes those points are in opposite vertices but the rectangle has not to be parallel with the coordinate axes. I didn't realize it! :) – Emetrop Nov 18 '13 at 12:24
2 Answers
Suppose
Left-Top vertex is $P_1(x_1,y_1)$, and
Right-Bottom vertex is $P_2(x_2,y_2)$
(in general, $2$ opposite vertices).
Point $P(x,y)$ is lying inside the rectangle, if
$$ \min\{x_1,x_2\} < x < \max\{x_1,x_2\} $$ $$ and $$ $$ \min\{y_1,y_2\} < y < \max\{y_1,y_2\}. $$
If $x_1<x_2$, and $y_1<y_2$ (Left-Bottom and Right-Top vertices), then $$ x_1<x<x_2 $$ $$ and $$ $$ y_1<y<y_2. $$
If edges of rectangle aren't parallel to coordinate axes, then $2$ points can't define unique rectangle (see image):
point $P$ belongs to blue rectangle, but not belongs to red rectangle.

Update (case of non-parallel to coordinate axes edges)
Points $P_1$ and $P_2$ determines diagonal of possible rectangle. Other vertices are on the circle (circumference) with diameter $P_1P_2$:

So, if $P$ is inside the circle with diameter $P_1P_2$, then it is possible to build such rectangle, that $P$ belongs to rectangle.
Denote
radius of the circle: $R = \dfrac{1}{2}\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$;
center of the circle: $x_o = \frac{x_1+x_2}{2}$, $y_o = \frac{y_1+y_2}{2}$,
Condition of possibility to build such rectangle: $$ (x-x_o)^2 + (y-y_o)^2 < R^2. $$
- 17,295
-
And is this solution functional for a rectangle which isn't parallel with coordinate axes? – Emetrop Nov 18 '13 at 12:27
-
@Emetrop, no (of sure). Only for rectangle with vertical/horizontal edges. – Oleg567 Nov 18 '13 at 12:42
-
@Emetrop, if rectangle's edges aren't parallel to coordinate axes, then $2$ points is too few for rectangle definition. – Oleg567 Nov 18 '13 at 12:44
-
Oh my god, I didn't realize it, should I make a new question or can I only edit answer? – Emetrop Nov 18 '13 at 12:46
-
I think this is what you are looking for:
if $p_1(x,y)$ and $p_2(x,y)$ determine a rectangle ($p_1(x) \ne p_2(x) \wedge p_1(y) \ne p_2(y)$) then the other point, $p(x,y)$, will be inside the rectangle if and only if $p(x)$ is between $p_1(x)$ and $p_2(x)$ and $p(y)$ is between $p_1(y)$ and $p_2(y)$.
- 133