# CGRect, CGSize and CGPoint Functions

With an understanding of C structures, and the definitions of CGRect, CGSize and CGPoint behind us, let’s look at a handful of functions for working with these structures.

CGRectMake and NSStringFromCGRect

With CGRectMake we can create a new CGRect structure. The rectangles below have differing starting points, however, each have a width and height of 100. The function NSStringFromCGRect returns a string object that defines the rectangle passed in as a parameter:

```1 2 3 4 5 CGRect rect1 = CGRectMake(100, 100, 100, 100); CGRect rect2 = CGRectMake(190, 190, 100, 100);   NSLog(@"rect1: %@", NSStringFromCGRect(rect1)); NSLog(@"rect2: %@", NSStringFromCGRect(rect2));```

Note : The output of all examples are shown in the image at the bottom of this post.

CGRectIntersect

To determine if two rectangles intersect, you can write code as follows:

```1 2 3 4 5 6 7 8 9 10 CGRect rect3 = CGRectMake(100, 100, 100, 100); CGRect rect4 = CGRectMake(190, 190, 100, 100);   if (CGRectIntersectsRect(rect3, rect4) == 1) NSLog(@"The rectangles intersect"); else NSLog(@"The rectangles do not intersect");   NSLog(@"rect3: %@", NSStringFromCGRect(rect3)); NSLog(@"rect4: %@", NSStringFromCGRect(rect4));```

CGRectInset

If you need to create a rectangle that is either larger or smaller than an existing rectangle, centered on the same point, try CGRectInset:

```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 CGRect rect5 = CGRectMake(100, 100, 100, 100); // Use positive values for a smaller rectangle CGRect rect6 = CGRectInset(rect, 25, 25);   NSLog(@"%rect5: %@", NSStringFromCGRect(rect5)); NSLog(@"%rect6: %@", NSStringFromCGRect(rect6));   ...   CGRect rect7 = CGRectMake(100, 100, 100, 100); // Use negative values for a larger rectangle CGRect rect8 = CGRectInset(rect, -25, -25);   NSLog(@"rect7: %@", NSStringFromCGRect(rect7)); NSLog(@"rect8: %@", NSStringFromCGRect(rect8));```

CGRectFromString

A little more uncommon, is creating rectangles from a string, however, if the need arises:

```1 2 3 NSString *str = @"{{0,0},{50,50}}"; CGRect rect9 = CGRectFromString(str); NSLog(@"rect9: %@", NSStringFromCGRect(rect9));```

Functions for CGSize and CGPoint

As you would expect, there are similar functions to those above for working with CGSize and CGPoint:

• CGSizeMake
• CGSizeEqualToSize
• CGPointMake
• CGPointEqualToPoint

The output for the above code examples follows: Thanks for the post… You do a great job covering stuff of interest to new iPhone developers… and I often find your blog postings in the results of my google searches, so kudos to you!

2. HI John,

Thanks for the post. It seems that the quotes in the NSLog(@””) are replaced by the NSLog(@""); in your post.

• Thanks krishnan, I’ve updated the post.

3. Welcome John.

4. Vijay Akkineni

Thank You

5. Vinay

Nice post

6. ahamdbilal

7. melanie

What if the values I get are the following:
2011-11-09 14:12:50.176 Robofair2011[4977:207] handleDoubleTap
2011-11-09 14:12:50.177 Robofair2011[4977:207] zoomRectForScale
2011-11-09 14:12:50.177 Robofair2011[4977:207] handleDoubleTap zoomRect: {{-inf, -inf}, {inf, inf}}

8. Arpit Parekh

Your code and blog is really very helpful, Thank you very much.

9. pons

nice post good for a beginer tip