4
$\begingroup$

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
    Are positionX and positionY the coordinates of the center?2012-07-14
  • 0
    No, that is the position of the upper left corner of the rectangle when no rotation is applied.2012-07-14
  • 1
    About which point are you rotating? The origin? The centre of the rectangle?2012-07-14
  • 2
    How is the rectangle being rotated? About the upper left corner, or about the center?2012-07-14
  • 0
    the rectangle is being rotated at positionX and positionY, so the upper left corner when no rotation is applied2012-07-14
  • 0
    So are positionX and positionY just the co-ordinates of the upper-left corner?2012-07-14
  • 0
    Generally, positionX and Y are provided for lower-left corner instead of upper-left.2012-07-14

2 Answers 2

3

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
    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?2012-07-14
  • 0
    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).2012-07-14
  • 0
    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.2012-07-14
  • 0
    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.2012-07-14
  • 0
    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.2012-07-14
  • 0
    Thanks, seems i solved it after changing your formulaes: `x+w*cos(0) - h*sin(0),y+w*sin(0) + h*cos(0)` and `x-h∗sin(θ),y+h∗cos(θ)`2012-07-14
1

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