Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rotated rectangle UIView corners' coordinates iOS

I'm trying to find a rotated rectangle UIView's four corners' coordinates.

I think one way I can do is to use recognizer.rotation, find the rotated angle then calculate the origins. But that requires some geometry calculation.

- (IBAction)handlePan:(UIRotationGestureRecognizer*)recognizer {
    NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    NSLog(@"%@",recognizer);

    recognizer.rotation = 0;
    NSLog(@"bound is %f and %f, frame is %f and %f, %f and %f.",recognizer.view.bounds.size.width,recognizer.view.bounds.size.height, recognizer.view.frame.size.width,recognizer.view.frame.size.height, recognizer.view.frame.origin.x, recognizer.view.frame.origin.y);
}

I'm just wondering if there are any other easier ways to get the coordinates? Thanks!

EDIT:

Looks like we have a great answer here(see answer below). I have managed to calculate the corners through a stupid way -- using rotation angle and geometry. It works but not easy and light. I'm sharing my code here just in case some one may want to use it(Even though I doubt it.)

    float r = 100;
    NSLog(@"radius is %f.",r);
    float AAngle = M_PI/3+self.rotatedAngle;
    float AY = recognizer.view.center.y - sin(AAngle)*r;
    float AX = recognizer.view.center.x - cos(AAngle)*r;
    self.pointPADA = CGPointMake(AX, AY);
    NSLog(@"View Center is (%f,%f)",recognizer.view.center.x,recognizer.view.center.y);
    NSLog(@"Point A has coordinate (%f,%f)",self.pointPADA.x,self.pointPADA.y);

    float BAngle = M_PI/3-self.rotatedAngle;
    float BY = recognizer.view.center.y - sin(BAngle)*r;
    float BX = recognizer.view.center.x + cos(BAngle)*r;
    self.pointPADB = CGPointMake(BX, BY);
    NSLog(@"Point B has coordinate (%f,%f)",BX,BY);

    float CY = recognizer.view.center.y + sin(AAngle)*r;
    float CX = recognizer.view.center.x + cos(AAngle)*r;
    self.pointPADC = CGPointMake(CX, CY);
    NSLog(@"Point C has coordinate (%f,%f)",CX,CY);

    float DY = recognizer.view.center.y + sin(BAngle)*r;
    float DX = recognizer.view.center.x - cos(BAngle)*r;
    self.pointPADD = CGPointMake(DX, DY);
    NSLog(@"Point D has coordinate (%f,%f)",DX,DY);
like image 487
user1491987 Avatar asked Sep 30 '13 15:09

user1491987


People also ask

How to programmatically add a corner radius to a UIView?

We can also achieve the same result programmatically instead of adding attributes in XCode. First connect the UIView to an ‘IBOutlet’ in the Connected ViewController and then add the ‘cornerRadius’ property to that UIView as below :

How do I get the radius of a corner in Revit?

Now inside ‘User Defined Runtime Attributes’ tab, click on the ’+’ button and add one new row with ‘Key Path’ as ‘layer.cornerRadius’ , ‘Type’ as ‘Number’ and ‘Value’ as 10 . Value can be any number you want. It is the radius of the corner for each corner of the view. That’s it.

How to align a UIView to the center of a viewcontroller?

This code is compatible with both Swift-3 and Swift-4 : In this example, I am adding one simple UIView to a ViewController. Add any height/width to the view and align it to the center of the ViewController as shown below : Click on the View and click on the third button of the ‘Utilities’ panel .

Are my rectangle corners stored in world coordinates?

However you are not storing your rectangle corners in world coordinates so we can tailor an approach to suit the data you have available.


1 Answers

Here's my solution though I wonder if there's a more succinct way:

CGPoint originalCenter = CGPointApplyAffineTransform(theView.center,
    CGAffineTransformInvert(theView.transform));

CGPoint topLeft = originalCenter;
topLeft.x -= theView.bounds.size.width / 2;
topLeft.y -= theView.bounds.size.height / 2;
topLeft = CGPointApplyAffineTransform(topLeft, theView.transform);

CGPoint topRight = originalCenter;
topRight.x += theView.bounds.size.width / 2;
topRight.y -= theView.bounds.size.height / 2;
topRight = CGPointApplyAffineTransform(topRight, theView.transform);

CGPoint bottomLeft = originalCenter;
bottomLeft.x -= theView.bounds.size.width / 2;
bottomLeft.y += theView.bounds.size.height / 2;
bottomLeft = CGPointApplyAffineTransform(bottomLeft, theView.transform);

CGPoint bottomRight = originalCenter;
bottomRight.x += theView.bounds.size.width / 2;
bottomRight.y += theView.bounds.size.height / 2;
bottomRight = CGPointApplyAffineTransform(bottomRight, theView.transform);
like image 159
Benjamin Cheah Avatar answered Sep 30 '22 21:09

Benjamin Cheah