CGGeometry

Various structures and associated functions for 2D geometric primitives.

Overview

The data structure CGPoint represents a point in a two-dimensional coordinate system. The data structure CGRect represents the location and dimensions of a rectangle. The data structure CGSize represents the dimensions of width and height.

A CGPoint, CGRect, or CGSize structure does not explicitly define the unit of measure for its member quantities. A point’s x- and y-coordinates or a size’s width and height are unitless quantities—whether such measurements are treated as pixels, scale-factor-independent points, texture elements (texels), or some other unit depends on the API using the measurement, and on the context in which that API is used. For example, a CGRect structure specifying the frame of an NSView or UIView object defines the view’s dimensions in points, not pixels. However, the effects of using a CGPoint structure in a CGContextRef drawing operation depend on the scale factor associated with that context. Where not otherwise specified, you can assume that a CGPoint, CGRect, or CGSize structure is defined in points, not pixels. (For details, see Drawing and Printing Guide for iOS or High Resolution Guidelines for OS X.)

The height and width stored in a CGRect data structure can be negative. For example, a rectangle with an origin of [0.0, 0.0] and a size of [10.0,10.0] is exactly equivalent to a rectangle with an origin of [10.0, 10.0] and a size of [-10.0,-10.0]. Your application can standardize a rectangle—that is, ensure that the height and width are stored as positive values—by calling the CGRectStandardize function. All functions described in this reference that take CGRect data structures as inputs implicitly standardize those rectangles before calculating their results. For this reason, your applications should avoid directly reading and writing the data stored in the CGRect data structure. Instead, use the functions described here to manipulate rectangles and to retrieve their characteristics.

Topics

Creating a Dictionary Representation from a Geometric Primitive

CGPointCreateDictionaryRepresentation

Returns a dictionary representation of the specified point.

CGSizeCreateDictionaryRepresentation

Returns a dictionary representation of the specified size.

CGRectCreateDictionaryRepresentation

Returns a dictionary representation of the provided rectangle.

Creating a Geometric Primitive from a Dictionary Representation

CGPointMakeWithDictionaryRepresentation

Fills in a point using the contents of the specified dictionary.

CGSizeMakeWithDictionaryRepresentation

Fills in a size using the contents of the specified dictionary.

CGRectMakeWithDictionaryRepresentation

Fills in a rectangle using the contents of the specified dictionary.

Creating a Geometric Primitive from Values

CGPointMake

Returns a point with the specified coordinates.

CGRectMake

Returns a rectangle with the specified coordinate and size values.

CGSizeMake

Returns a size with the specified dimension values.

CGVectorMake

Returns a vector with the specified dimension values.

Modifying Rectangles

CGRectDivide

Divides a source rectangle into two component rectangles.

CGRectInset

Returns a rectangle that is smaller or larger than the source rectangle, with the same center point.

CGRectIntegral

Returns the smallest rectangle that results from converting the source rectangle values to integers.

CGRectIntersection

Returns the intersection of two rectangles.

CGRectOffset

Returns a rectangle with an origin that is offset from that of the source rectangle.

CGRectStandardize

Returns a rectangle with a positive width and height.

CGRectUnion

Returns the smallest rectangle that contains the two source rectangles.

Comparing Values

CGPointEqualToPoint

Returns whether two points are equal.

CGSizeEqualToSize

Returns whether two sizes are equal.

CGRectEqualToRect

Returns whether two rectangles are equal in size and position.

CGRectIntersectsRect

Returns whether two rectangles intersect.

Checking for Membership

CGRectContainsPoint

Returns whether a rectangle contains a specified point.

CGRectContainsRect

Returns whether the first rectangle contains the second rectangle.

Getting Min, Mid, and Max Values

CGRectGetMinX

Returns the smallest value for the x-coordinate of the rectangle.

CGRectGetMinY

Returns the smallest value for the y-coordinate of the rectangle.

CGRectGetMidX

Returns the x- coordinate that establishes the center of a rectangle.

CGRectGetMidY

Returns the y-coordinate that establishes the center of the rectangle.

CGRectGetMaxX

Returns the largest value of the x-coordinate for the rectangle.

CGRectGetMaxY

Returns the largest value for the y-coordinate of the rectangle.

Getting Height and Width

CGRectGetHeight

Returns the height of a rectangle.

CGRectGetWidth

Returns the width of a rectangle.

Checking Rectangle Characteristics

CGRectIsEmpty

Returns whether a rectangle has zero width or height, or is a null rectangle.

CGRectIsNull

Returns whether the rectangle is equal to the null rectangle.

CGRectIsInfinite

Returns whether a rectangle is infinite.

Data Types

CGFloat

The basic type for all floating-point values.

CGPoint

A structure that contains a point in a two-dimensional coordinate system.

CGRect

A structure that contains the location and dimensions of a rectangle.

CGSize

A structure that contains width and height values.

CGVector

A structure that contains a two-dimensional vector.

Constants

CGRectInfinite

A rectangle that has infinite extent.

Geometric Zeros

A zero point, zero rectangle, or zero size.

Geometrical Null

The null or empty rectangle.

CGRectEdge

Coordinates that establish the edges of a rectangle.

CGFloat Informational Macros

Informational macros for the CGFloat type.

See Also