Jump to content
  • Advertisement
Sign in to follow this  

Rotating and Scaling a Rectangle around the a point

This topic is 2033 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 all,


I'm writing an iPhone application where I'd like the user to be able to push a button and be able to rotate / stretch a rectangle with regards to the red point in the picture below:




As you can see I'd like to be able to rotate around the red dot (or half way down the y axis) and would also be able to stretch the rectangle with it still being anchored to the red point (doesn't move).


I should mention that in iPhone, that anchor point is initially set to the center of the object.


I struggle badly with transformations, but was able to figure out the rotation:




    self.rotateView.transform = CGAffineTransformTranslate(self.rotateView.transform,   self.rotateView.bounds.size.width / 2, 0);
    self.rotateView.transform = CGAffineTransformRotate(self.rotateView.transform, M_PI / 180);
    self.rotateView.transform = CGAffineTransformTranslate(self.rotateView.transform, self.rotateView.bounds.size.width  / 2, 0);


How to scale something is obvious, what I'm having trouble with is correctly translating the object before and after I scale it, and then doing all of this within the context of the rotation so that they both can work out when applied together. 


Anyone might be able to point me in the right direction on how to approach this?






Share this post

Link to post
Share on other sites

The solution is to translate the rotation pivot to the origin, rotate and then translate back (in short, you're missing a sign in your code).


For instance assume you have a unit square (width and height = 1) centered on the origin (eg its far corners are [-0.5, -0.5] and [+0.5, +0.5]) and you want to rotate it around its upper left corner ([-0.5, 0.5]): you would first translate the cube's origin to this pivot, eg subtract [-0.5, +0.5] from all vertex coordinates, then rotate them and finally undo the translation by adding [-0.5, +0.5] to all vertices.


As for scaling - consider the following order: scale, translate, rotate, translate back. Store your rectangle in a "rest position" and calculate its transformed vertices from scratch every time it's scaled or rotated.

Edited by irreversible

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!