[Math] How to find on which outer side of the rectangle falls the point

analytic geometrycomputational geometrygeometry

Qt has a class QRect which tells whether the point is inside the rectangle or not.

Now, the problem is to find out on which outer side (out of four) of the rectangle does the point lie.

The equation that I already have is:

C++ code:

double m              = (y2 - y1) / (x2 - x1);
double equationResult = (newY - y1) - ((m * newX) + (m * x1));

if (equationResult < 0)
      return "in";
else
      return "out";

where newX and newY are the points which we are supposed to check.

Assumption:
The point lies on the right hand side of the rectangle.

So, with this assumption in mind when I supplied the end points of the top of the rectangle, the result I got was "out".
Even with the end points of the right side, the result I got was "out".

So, if more than one sides are going to give the same output, how am I supposed to know the position of the point?

Best Answer

Why not : $\ \mathrm{in}:=(x_1\le x \le x_2) \land (y_1\le y \le y_2)\ $ ?

(supposing that $x_1\le x_2$ and $y_1\le y_2$ : i.e. the rectangle is first 'normalized')

Let's illustrate the 9 regions (larger values of $y$ are at the bottom) :

regions

But (from David's discussion +1) perhaps that you are rather interested by these four regions :

enter image description here

with $\ x_c=\dfrac {x_1+x_2}2,\ y_c=\dfrac {y_1+y_2}2$

Related Question