x,y coordinates as part of a quadrant

70 Views Asked by At

I'm having some problem with my program. what I want to implement is a way to understand a point(x,y) in which quadrant is located.

The concept of a quadrant that I'm trying to implement is the following:

Quardant division image

I have a rectangle, from which I have the coordinates of all the angles and the edges. given another point in the space, in the 2D space, I would like to know where is located between the top, bottom, left and right.

Thank you in advance.

3

There are 3 best solutions below

4
On BEST ANSWER

Okay, So the center, upper left, lower right are: $(c_x, c_y)$, $(ul_x, ul_y)$ and $(lr_x, lr_y)$ and $c_x \approx \frac {ul_x + lr_x}2$ and $c_y\approx \frac {ul_y + lr_y}2$ (give or take a pixel).

We'll let $h = ul_y - lr_y$ and $w = lr_x - ul_x$.

You have two lines. The one going from the lower left hand corner to the upper right hand corner has the equation $(y-c_y) = \frac hw\times (x-c_x)$. We can rewrite this as $y = \frac hw x + (c_y - \frac hwc_x)$. We can further rewrite this as $y = mx + b$ where $m = \frac hw$ and $b = c_y - \frac hwc_x$ if we want.

The other line, top left corner to lower right, has the equation $(y-c_y) = -\frac hw (x-c_x)$ which we can rewrite that as $y = - \frac hw x +(c_y + \frac hwc_x$ or as $y = -mx + d$ where $d = c_y +\frac hwc_x$.

So...

Take a point $(x_0,y_0)$ and do this test.

1) Is $y_0 \ge mx_0 + b$ and $y_0 \ge -mx_0 + d$? If so $(x_0,y_0)$ is in the top quadrant (because $(x_0,y_0)$ is above both the lines).

2) Is $y_0 \ge mx_0 + b$ and $y_0 < -mx_0 + d$? If so $(x_0,y_0)$ is in the left quadrant (because $(x_0,y_0)$ is above [to the left] of the line going up but below [to the left] of the line going down.)

3) Is $y_0 < mx_0 + b$ and $y_0 \ge -mx_0 + d$? If so $(x_0,y_0)$ is in the right quadrant.

4) Is $y_0 < mx_0+ b$ and $y < -mx_0 + d$? If so $(x_0,y_0)$ is in the bottom quadrant.

(this assumes, of course that, the $x$ coordinates go left to right and the $y$ coordinates go down to up. Actually they probably go up to down in which case 1) = bottom; 2) = right; 3)= left and 4) = top.)

8
On

We need to define the boundary lines as for example for a rectangle centered at the origin

  • $y=ax$
  • $y=-ax$

then for $y\neq \pm ax$ we have

  • right $\implies x>0 \land y<ax \land y>-ax$
  • left $\implies x<0 \land y>ax \land y<-ax$
  • up $\implies y>0 \land x<y/a \land x>-y/a$
  • down $\implies y<0 \land x>y/a \land x<-y/a$

For a rectangle centered at $C(a,b)$ with height $2h$ and width $2w$ the boundary lines are

  • $y-b=\frac h w(x-a)$
  • $y-b=-\frac h w(x-a)$

then for $y\neq \pm ax$ we have

  • right $\implies x>a \land y-b>\frac h w(x-a) \land y-b<-\frac h w(x-a)$
  • left $\implies x<a \land y-b<\frac h w(x-a) \land y-b>-\frac h w(x-a)$
  • up $\implies y>0 \land x-a<\frac w h(y-b) \land x-a>-\frac w h(y-b)$
  • down $\implies y<0 \land x-a>\frac w h(y-b) \land x-a<-\frac w h(y-b)$
0
On

How might you determine which quadrant of the plane relative to the coordinate axes a point lies in? You’d examine the signs of its coordinates, which are the signed distances of the point from the coordinate axes. You can use the same idea with any pair of intersecting lines. If you represent a line by the equation $ax+by+c=0$, then plugging the coordinates of an arbitrary point into the expression on the left-hand side of this equation produces a number that’s proportional to the signed distance of the point to the line. The sign of this value tells you on which side of the line the point lies, using the direction of the normal vector $(a,b)$ to the line as a reference: if the value is positive, the normal vector points toward the half-plane in which the point lies; if negative, the normal points away from the half-plane. Each of your four “quadrants” then corresponds to a different combination of the two signs of these values for the extensions of the diagonals. Effectively, you’re applying an affine transformation that maps the diagonals of the rectangle to the coordinate axes.

If you work in homogeneous coordinates, a line can be represented by a three-element vector, and plugging a point into the left-hand side of the line’s equation becomes a dot product computation. Let’s say that the homogeneous coordinates of the center are $\mathbf c = (c_x,c_y,1)$, the upper-right corner $\mathbf r = (r_x,r_y,1)$ and upper-left corner $\mathbf l = (l_x,l_y,1)$. Compute $\mathbf d_r = \mathbf c\times\mathbf r$ and $\mathbf d_l = \mathbf l\times\mathbf c$ (pay attention to the order here—it matters!). To find the region in which a point $\mathbf p=(p_x,p_y,1)$ lies, examine the signs of $\mathbf p\cdot\mathbf d_l$ and $\mathbf p\cdot\mathbf d_u$: $$\begin{align} \mathbf p\cdot\mathbf d_r \gt 0, \mathbf p\cdot\mathbf d_l \gt 0 &\implies \text{TOP} \\ \mathbf p\cdot\mathbf d_r \gt 0, \mathbf p\cdot\mathbf d_l \lt 0 &\implies \text{LEFT} \\ \mathbf p\cdot\mathbf d_r \lt 0, \mathbf p\cdot\mathbf d_l \gt 0 &\implies \text{RIGHT} \\ \mathbf p\cdot\mathbf d_r \lt 0, \mathbf p\cdot\mathbf d_l \lt 0 &\implies \text{BOTTOM}. \end{align}$$ If either dot product is zero, then $\mathbf p$ lies on that line. This assumes the standard mathematical convention of a right-handed coordinate system. If you have a left-handed coordinate system instead, the roles of TOP and BOTTOM will be swapped.

For another geometric interpretation of this computation, observe that the sign of the scalar triple product $\mathbf p\cdot\mathbf d_r = \mathbf p\cdot\mathbf c\times\mathbf r$ tells you if the three points are listed in counterclockwise or clockwise order, which in turn tells you on which side of the line through $\mathbf c$ and $\mathbf r$ the point $\mathbf p$ lies (and similarly for the other diagonal).