Thursday, August 25, 2011

iPhone Find CGRect Coordinates.

If you want to find out the top left, top right, bottom left and bottom right coordinates of your CGRect. Following is how you do it.




















     
    CGRect b=CGRectMake(4050240150);
    //top left
    float topLX=CGRectGetMinX(b);
    float topLY=CGRectGetMinY(b);
    
    NSLog(@"(%f,%f)",topLX,topLY);
  
    //top right
    float topRX=CGRectGetMaxX(b);
    float topRY=CGRectGetMinY(b);
    
    NSLog(@"(%f,%f)",topRX,topRY);
    
    //bottom left
    float bottomLX=CGRectGetMinX(b);
    float bottomLY=CGRectGetMaxY(b);
    
    NSLog(@"(%f,%f)",bottomLX,bottomLY);
  
    //bottom right
    float bottomRX=CGRectGetMaxX(b);
    float bottomRY=CGRectGetMaxY(b);
    
    NSLog(@"(%f,%f)",bottomRX,bottomRY);


It will print following results


top left (40.000000,50.000000)
top right (280.000000,50.000000)
bottom left (40.000000,200.000000)
bottom right (280.000000,200.000000)




If this is not what you were looking for CGRect, try this comprehensive LIST.

4 comments:

  1. well Explained.great work.but small amendment on above code. Replace 'b.frame' to 'b' ,because b is already a rect(i guess).

    ReplyDelete
  2. Oops. Thanks Chandra for pointing it out.

    ReplyDelete
  3. Nice post! Can’t wait for the next one. Keep stuff like this coming.
    Asus Laptops

    ReplyDelete