Skip to content

Instantly share code, notes, and snippets.

@andrewsardone
Created September 17, 2012 23:58
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewsardone/3740504 to your computer and use it in GitHub Desktop.
Save andrewsardone/3740504 to your computer and use it in GitHub Desktop.
CGRectDivide

CGRectDivide is handy for slicing up a rectangle.

Example of getting a slice and the remaining area of a rectange.

CGRect rect = CGRectMake(0, 0, 240, 150);
  
CGRect remainder, slice;
    
CGRectDivide(rect, &slice, &remainder, 120, CGRectMinYEdge);

Here's a visual reference:

(0,0)                                 (240,0)
  *---------------------------------------*
  |                                       |
  |                                       |
  |           Slice  Rectangle            |
  |                                       |
  |                                       |
  |                                       |
 -------------------------------------------
  |                                       |
  |         Remainder Rectangle           |
  |                                       |
  *---------------------------------------*
(0,150)                               (240,150)

(diagram via http://soulwithmobiletechnology.blogspot.com/2011/08/how-to-divide-cgrect-using-cgrectdivide.html)

Apple Documentation

CGRectDivide

Divides a source rectangle into two component rectangles.

void CGRectDivide (
   CGRect rect,
   CGRect *slice,
   CGRect *remainder,
   CGFloat amount,
   CGRectEdge edge
);

Parameters

rect

The source rectangle.

slice

On input, a pointer to an uninitialized rectangle. On return, the rectangle is filled in with the specified edge and values that extends the distance beyond the edge specified by the amount parameter.

remainder

On input, a pointer to an uninitialized rectangle. On return, the rectangle contains the portion of the source rectangle that remains after CGRectEdge produces the “slice” rectangle.

amount

A distance from the rectangle side that is specified in the edge parameter. This distance defines the line, parallel to the specified side, that Quartz uses to divide the source rectangle.

edge

An edge value that specifies the side of the rectangle from which the distance passed in the amount parameter is measured. CGRectDivide produces a “slice” rectangle that contains the specified edge and extends amount distance beyond it.

Availability

Available in iOS 2.0 and later.

Declared In

CGGeometry.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment