Sign in to follow this  
GaMEFRo

Rotating and Scaling a Rectangle around the a point

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:

 

graphicquestion.png

 

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?

 

Thanks,

 

GaMEFRo

 

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this