Get the four corners of a rectangle

1.9k Views Asked by At

Illustration of the problem

I have a boundary given ($xMin$, $yMin$, $xMax$, $yMax$) and the two points of a reference line of a rectangle. The begin point is at $(x_b, y_b)$ and the end point is at $(x_e, y_e)$. This reference line tells the rectangle's angle and length.

If we transform the reference line so that it is parallel to the x-axis and its begin point is on the left of the end point, the outer side is above the inner side.

Illustration of the transformed sample

I need to get the four corners of the rectangle. The parallel sides is then categorized as outer side or inner side. So the four sides is represented by:

  • begin point of outer side: $(x_{ob}, y_{ob})$
  • begin point of inner side: $(x_{ib}, y_{ib})$
  • end point of outer side: $(x_{oe}, y_{oe})$
  • end point of inner side: $(x_{ie}, y_{ie})$

The width can now be computed as distance between the two begin or end points.

The perpendicular sides' slope is negative reciprocal of the reference line's slope: $$m=-\frac{x_e-x_b}{y_e-y_b}$$

The begin side has equation of (eq1): $$y=-\frac{x_e-x_b}{y_e-y_b}(x-x_b)+y_b$$ which is the same for the end side (eq2): $$y=-\frac{x_e-x_b}{y_e-y_b}(x-x_e)+y_e$$

After this, I am lost to what to do next. I know that I need to use the boundary values to substitute from eq1 and eq2.

2

There are 2 best solutions below

0
On

Lets define

  • begin point of outer side: $B_o = (x_{ob}, y_{ob})$
  • begin point of inner side: $B_i = (x_{ib}, y_{ib})$
  • end point of outer side: $E_o = (x_{oe}, y_{oe})$
  • end point of inner side: $E_i = (x_{ie}, y_{ie})$

The slopes of the segments $\overline{B_oE_o}$ and $\overline{B_iE_i}$ have to be equal

$$m = \dfrac{y_{oe} - y_{ob}}{x_{oe} - x_{ob}} = \dfrac{y_{ie} - y_{ib}}{x_{ie} - x_{oib}}$$

Both of these segments are parallel to the line $y=mx$.

Assuming that $m$ is a negative number, we want to rotate the points through the angle $\theta$ where

$$\cos \theta = \dfrac{1}{\sqrt{1 + m^2}} \qquad \sin \theta = \dfrac{-m}{\sqrt{1 + m^2}}$$

So the rotations can be represented by

$\begin{pmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} = \dfrac{1}{\sqrt{1+m^2}} \begin{pmatrix} 1 & m \\ -m & 1 \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} $

So

$B_o \mapsto \dfrac{1}{\sqrt{1+m^2}} \begin{pmatrix} 1 & m \\ -m & 1 \end{pmatrix} \begin{pmatrix} x_{ob} \\ y_{ob} \end{pmatrix}$

$B_i \mapsto \dfrac{1}{\sqrt{1+m^2}} \begin{pmatrix} 1 & m \\ -m & 1 \end{pmatrix} \begin{pmatrix} x_{ib} \\ y_{ib} \end{pmatrix}$

$E_o \mapsto \dfrac{1}{\sqrt{1+m^2}} \begin{pmatrix} 1 & m \\ -m & 1 \end{pmatrix} \begin{pmatrix} x_{oe} \\ y_{oe} \end{pmatrix}$

$E_i \mapsto \dfrac{1}{\sqrt{1+m^2}} \begin{pmatrix} 1 & m \\ -m & 1 \end{pmatrix} \begin{pmatrix} x_{ie} \\ y_{ie} \end{pmatrix}$

In the image shown below, $m = -\dfrac 34$ The rotation matrix turned out to be $\begin{pmatrix} 0.8 & -0.6 \\ 0.6 & 0.8 \end{pmatrix}$

enter image description here

0
On

Assume that the reference line isn’t parallel to any edge (in which case the bounding rectangle is the rectangle). Consider the case $x_b<x_e$, $y_b<y_e$. Let $\Delta x=x_e-x_b$ and $\Delta y=y_e-y_b$. Then, from inspection of the corresponding picture, the four corners of the rectangle can be seen to be $$\begin{align} ib &= (x_{max}-\Delta x, y_{min}) \\ ie &= (x_{max},y_{min}+\Delta y) \\ ob &= (x_{min},y_{max}-\Delta y) \\ oe &= (x_{min}+\Delta x,y_{max}). \end{align}$$ (Note that these formulas also work for $\Delta x>0$, $\Delta y=0$.)

The other three cases can either be handled separately in a similar manner, or by permuting the given values (i.e., by rotating through a suitable multiple of $\frac\pi2$), applying the above formula, and finally reversing the permutation.