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

How can i get the get upper-left, upper-right, lower-left and lower-right corners 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).

-
Are positionX and positionY the coordinates of the center? –  Alex Becker Jul 14 '12 at 8:01
No, that is the position of the upper left corner of the rectangle when no rotation is applied. –  netbrain Jul 14 '12 at 8:02
About which point are you rotating? The origin? The centre of the rectangle? –  Host-website-on-iPage Jul 14 '12 at 8:08
How is the rectangle being rotated? About the upper left corner, or about the center? –  Zev Chonoles Jul 14 '12 at 8:08
the rectangle is being rotated at positionX and positionY, so the upper left corner when no rotation is applied –  netbrain Jul 14 '12 at 8:12

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.

-
You state that (x,y) will always be the upper left corner. This is not true. if you rotate the rectangle a full 180 degrees, then in fact the (x,y) would be the bottom right corner? –  netbrain Jul 14 '12 at 11:02
Yes, it will be so, but in that case, you'll have to rotate your view too(so that +ve Y-Axis goes back to top). –  hjpotter92 Jul 14 '12 at 11:04
Im not sure i follow. my requirement for this is to be able to identify the upper-right, upper-left, bottom-right and bottom-left corner regardless of the rectangles rotation. –  netbrain Jul 14 '12 at 11:11
With the logic you've given; you can't say which corner goes to which position unless you rotate your view also such that the Y-Axis goes down-to-up(from negative to positive). Otherwise, in my solution, the original top-left will remain at the same co-ordinate value and others will change. Position can't be determined. –  hjpotter92 Jul 14 '12 at 11:13
The bottom-right corner doesn`t seem to be correct. I dont't have the time right now to investigate, but ill look at it later. –  netbrain Jul 14 '12 at 12:59

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 ${} {} {}$

-