How to get upper-left, upper-right, lower-left and lower-right corners XY coordinates from a rectangle shape.

15.6k Views Asked by At

How can I get the get upper-left, upper-right, lower-left and lower-right corners given XY coordinates from a rectangle shape when I have the following data available to me?

positionX 
positionY
width
height
rotation

Is there an easy way of doing this?

Clarification:

The rectangle is being rotated at positionX and positionY, the upper-left corner when no rotation is applied (rotation=0).

2

There are 2 best solutions below

6
On BEST ANSWER

First, let me take these smaller notifications:

  • positionX = $x$
  • positionY = $y$
  • width = $w$
  • height = $h$
  • rotation = $\theta$ Thus, our top-left point is $(x,y)$. The other 3 points will be(without rotation): $(x+w, y)$, $(x+w, y-h)$ and $(x, y-h)$.

Since we are rotating the complete geometry about point $(x,y)$ by an angle of $\theta$, we'll have new points given as:

  1. $(x, y)$
  2. $(x + w*\cos(\theta), y + w*\sin(\theta))$
  3. $(x + w*\cos(\theta) + h*\cos(\frac{3\pi}{2}-\theta), y + w*\sin(\theta) + h*\sin(\frac{3\pi}{2}-\theta))$
  4. $(x + h*\cos(\frac{3\pi}{2}+\theta), y + h*\sin(\frac{3\pi}{2}+\theta))$

which, on simplification give us the co-ordinates

  1. $(x, y)$
  2. $(x + w*\cos(\theta), y + w*\sin(\theta))$
  3. $(x + w*\cos(\theta) - h*\sin(\theta), y + w*\sin(\theta) - h*\cos(\theta))$
  4. $(x + h*\sin(\theta), y - h*\cos\theta))$

I am not entirely sure of the conversion I did for $\sin(\frac{3\pi}{2}±\theta)$ or $\cos(\frac{3\pi}{2}±\theta)$

KEY: 1. -> Top-Left corner, 2. -> Top-Right Corner, 3. -> Bottom-Right Corner and 4. -> Bottom-Left Corner.

0
On

Let the coordinate of upper left corner be $(a,b)$ and width and hight are $w$ and $h$ respectively. Now if the rotation angle is $0$ then upper right corner is $(a+w,b)$, lower left corner is $(a,b-h)$, lower right corner is $(a+w,b-h)$. Now translate your origin to $(a,b)$ and rotate the coordinate axes by $\theta$ say, then you can compute the transformed coordinates using the formulas given here and here ${} {} {}$