Jump to content

  • Log In with Google      Sign In   
  • Create Account


Rotating and Scaling a Rectangle around the a point


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 GaMEFRo   Members   -  Reputation: 131

Like
0Likes
Like

Posted 21 March 2013 - 06:42 PM

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

 



Sponsor:

#2 irreversible   Crossbones+   -  Reputation: 1317

Like
0Likes
Like

Posted 21 March 2013 - 11:23 PM

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, 21 March 2013 - 11:24 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS