Getting all 4 coordinates of a rotated UIImageView

How to get 4 coordinates for UIImageView?

I know that CGRect can be obtained from both origin.x and origin.y, but how can I find all 4 corners?

EDIT: I rotate UIImageViews, which is why I asked: P

+4
source share
6 answers

You can add the width and height of the rectangle to get the coordinates of the other three points.

CGRect rect = view.bounds; CGPoint topLeft = rect.origin; CGPoint topRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y); CGPoint bottomLeft =CGPointMake(rect.origin.x, rect.origin.y + rect.size.height); CGPoint bottomRight = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); 

Then you can use CGPointApplyAffineTransform to get the transformed coordinates from them according to the specified transformation.

 CGPoint center = view.center; CGAffineTransform transf = CGAffineTransformMakeTranslation(-rect.size.width/2, -rect.size.height/2); transf = CGAffineTransformConcat(transf, view.transform); transf = CGAffineTransformTranslate(transf, center.x, center.y); topLeft = CGPointApplyAffineTransform(topLeft, transf); //... 

(note: not verified.)

+5
source

This is my decision:

  • [self] is a subclass of UIImageView
  • [self.transform] is the transformation I do on [self] :

     CGAffineTransform transform = CGAffineTransformMakeTranslation(-center.x, -center.y); transform = CGAffineTransformConcat(transform, self.transform); CGAffineTransform transform1 = CGAffineTransformMakeTranslation(center.x, center.y); transform = CGAffineTransformConcat(transform, transform1); CGPoint leftTopPoint = CGPointApplyAffineTransform(leftTopPoint, transform); CGPoint rightTopPoint = CGPointApplyAffineTransform(rightTopPoint, transform); CGPoint rightBottomPoint = CGPointApplyAffineTransform(rightBottomPoint, transform); CGPoint leftBottomPoint = CGPointApplyAffineTransform(leftBottomPoint, transform); 
+2
source

You can get size.width and size.height. Adding these objects to x and y will give you other coordinates.

+1
source

Although this (of course) is relative to the supervisor, you can use the frame property to get a CGRect containing the beginning and size of the UIImageView. Then you can simply add the appropriate size to the corresponding starting point to get a complete set of coordinates.

See the frame section in the UIView for more information.

0
source

Build the rotation matrix http://en.wikipedia.org/wiki/Rotation_matrix . You must calculate the initial positions of the angles with respect to the point that is the center of rotation. Store these positions in an array and keep them all the time. You calculate new positions by passing the angle in the 2x2 rotation matrix and multiplying them by the starting positions.

0
source

Well, if you know the angle of rotation, this is the math to get the y coordinate of the upper right corner:

 Sin (angle of rotation) = height difference y / width 

Therefore, if you rotate the rectangle 10 degrees and it has a width of 20pt:

 Sin 10 = yDiff / 20 

This means you can do this:

 yDiff = Sin 10 * 20 

This gives you the difference in y from the y coordinate of the origin to the y coordinate in the upper right corner. Add this value to the current y of the beginning of your rectangle to get the actual y coordinate of your upper right corner. The next step is to use pythagoras on your width and yDiff to get xDiff and do the same (add it to the x coordinate) to get the x coordinate of your right corner. Hope this makes sense.

Now you just need to do it again for every other corner - imagine if you want the rectangle to rotate 90 degrees, you can just repeat the logic, however x is y and vice versa. :) etc.

0
source

Source: https://habr.com/ru/post/1346449/


All Articles