• Advertisement
Sign in to follow this  

Rotation questions (beginner)

This topic is 4143 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I need some help implementing an object rotation feature in my program. I have two questions: 1) Have a look at this illustration: http://www.softwarefailure.de/rot.jpg You'll see a blue rotated rectangle defined through the coordinates x1,y1,x2,y2 and the rotation angle a. Then there is the selection rectangle (only visible through the 8 small black boxes which lie on the border of the selection rectangle). The selection rectangle fully comprises the blue rectangle and it is defined through the coordinates sx1,sy1,sx2,sy2. Now the user shall be able to change the size of the rotated blue rectangle by clicking and dragging one of the 8 boxes on the border of the selection rectangle. When he does so, the size of the selection rectangle will change. My question is now: How do I compute the new size for the rotated blue rectangle from the new size of the selection rectangle? The rotation angle of the blue rectangle stays the same of course, but the size must change. Thus, I somehow need to compute new x1,y1,x2,y2. For instance, the old size of the selection rectangle is 200x150 pixels. Now the user scales the selection rectangle to 300x200 pixels. The blue rectangle needs to be scaled so that it fits exactly into the new selection rectangle. How to find out the new dimensions of the blue rectangle? 2) My second question is also related to object rotation. Imagine that there is a point x1,y1 somewhere inside the unrotated blue rectangle from above. Now the user rotates the blue rectangle, thus, the coordinates of the point x1,y1 must change too. My question is now: How do I compute the rotated coordinates of the point? For instance, the point is at 50:50 inside my 200x150 blue rectangle if there is no rotation. Now the 200x150 rectangle is rotated by 45 degrees. Where is the point now? Any help is greatly appreciated! Andy

Share this post


Link to post
Share on other sites
Advertisement
1) You don't have to care about the rotation as long as you have the position of each point. All you have to do is calculate the difference from the original blue rect and the changed rect (while dragging). Say the blue rect was 200 pixel width and the user dragged it to 250 - the difference would be 250/200 makes a scale of 1.25, now just scale your points by this value (move to origin, apply scaling, move back)

2) Is your problem the rotation (look for 2d rotation formula, it's 2 lines and quite simple) or keeping the correct origin for that point?

Yhoko

Share this post


Link to post
Share on other sites
Wanted to say that for the case of the first question (scaling a rotated rectangle along the global coordinate axes), the resulting shape no longer has to be a rectangle. Look at that sample picture you provided and scale it along the y-axis only. The result will be a parallelogram. So I guess there is no way to just calculate new x1,y1,x2 and y2 for a rectangle that fits the bounding box that behaves nicely.
However, if you restrict the operation to uniform scale (scaling along x- and y-axis by the same amount) the resulting shape should be a rectangle again and you should just have to scale the original rectangle by the same amount before rotating it.

2-dimensional rotation:
x1' = x1 * cos(theta) - y1 * sin(theta)
y1' = x1 * sin(theta) + y1 * cos(theta),
where theta is the rotation angle and x1' and y1' are the new coordinates or x1 and y1.

Good luck!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement