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 CGContext 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.

Symbols

Creating a Dictionary Representation from a Geometric Primitive

var dictionaryRepresentation: CFDictionary

Returns a dictionary representation of the specified point.

var dictionaryRepresentation: CFDictionary

Returns a dictionary representation of the specified size.

var dictionaryRepresentation: CFDictionary

Returns a dictionary representation of the provided rectangle.

Modifying Rectangles

func insetBy(dx: CGFloat, dy: CGFloat)

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

var integral: CGRect

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

func intersection(CGRect)

Returns the intersection of two rectangles.

func offsetBy(dx: CGFloat, dy: CGFloat)

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

var standardized: CGRect

Returns a rectangle with a positive width and height.

func union(CGRect)

Returns the smallest rectangle that contains the two source rectangles.

Comparing Values

func equalTo(CGRect)

Returns whether two rectangles are equal in size and position.

func intersects(CGRect)

Returns whether two rectangles intersect.

Checking for Membership

func contains(CGPoint)

Returns whether a rectangle contains a specified point.

func contains(CGRect)

Returns whether the first rectangle contains the second rectangle.

Getting Min, Mid, and Max Values

var minX: CGFloat

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

var minY: CGFloat

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

var midX: CGFloat

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

var midY: CGFloat

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

var maxX: CGFloat

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

var maxY: CGFloat

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

Getting Height and Width

var height: CGFloat

Returns the height of a rectangle.

var width: CGFloat

Returns the width of a rectangle.

Checking Rectangle Characteristics

var isEmpty: Bool

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

var isNull: Bool

Returns whether the rectangle is equal to the null rectangle.

var isInfinite: Bool

Returns whether a rectangle is infinite.

Data Types

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.