# 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.

## 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?

Thanks,

GaMEFRo

##### 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

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632910
• Total Posts
3009174
• ### Who's Online (See full list)

There are no registered users currently online

×