Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 4. Event Handling and Graphics S... > Introduction to Geometric Structures

4.1. Introduction to Geometric Structures

Before diving into events management, you'll need a basic understanding of some geometric structures commonly used on the iPhone. You've already seen some of these in Chapter 3. The Core Graphics framework provides many general structures to handle graphics-related functions. Among these structures are points, window sizes, and window regions. Core Graphics also provides many C-based functions for creating and comparing these structures.

4.1.1. CGPoint

A CGPoint is the simplest Core Graphics structure, and contains two floating-point values corresponding to horizontal (X) and vertical (Y) coordinates on a display. To create a CGPoint, use the CGPointMake method:

CGPoint point = CGPointMake (320.0, 480.0);

The first value represents X, the horizontal pixel value, and the second Y, the vertical pixel value. These values can also be accessed directly:

float x = point.x;
float y = point.y;

The iPhone's display resolution is 320×480 pixels. The upper-left corner of the screen is referenced at 0×0, and the lower right at 319×479 (pixel values are zero-indexed).

Being a general-purpose structure, a CGPoint can refer equally well to a coordinate on the screen or within a window. For example, if a window is drawn at 0×240 (halfway down the screen), a CGPoint with values (0, 0) could address either the upper-left corner of the screen or the upper-left corner of the window (0×240). Which one it means is determined by the context where the structure is being used in the program.

Two CGPoint structures can be compared using the CGPointEqualToPoint function:

BOOL isEqual = CGPointEqualToPoint(point1, point2);

4.1.2. CGSize

A CGSize structure represents the size of a rectangle. It encapsulates the width and height of an object, and is primarily found in the iPhone APIs to dictate the size of screen objects—namely windows. To create a CGSize object, use CGSizeMake:

CGSize size = CGSizeMake(320.0, 480.0);

The values provided to CGSizeMake indicate the width and height of the element being described. Values can be directly accessed using the structure's width and height variable names:

float width = size.width;
float height = size.height;

Two CGSize structures can be compared using the CGSizeEqualToSize function:

BOOL isEqual = CGSizeEqualToSize(size1, size2);

4.1.3. CGRect

The CGRect structure combines a CGPoint and CGSize structure to describe the frame of a window on the screen. The frame includes an origin, which represents the location of the upper-left corner of the window, and the size of the window. To create a CGRect, use the CGRectMake function:

CGRect rect = CGRectMake(0, 200, 320, 240);

This example describes a 320×240 window whose upper-left corner is located at coordinates 0×200. As with the CGPoint structure, these coordinates could reference a point on the screen itself or offsets within an existing window; it depends on where and how the CGRect structure is used.

The components of the CGRect structure can also be accessed directly:

CGPoint windowOrigin = rect.origin;
float x = rect.origin.x;
float y = rect.origin.y;

CGSize windowSize = rect.size;
float width = rect.size.width;
float height = rect.size.height;

4.1.3.1. Containment and intersection

Two CGRect structures can be compared using the CGRectEqualToRect function:

BOOL isEqual = CGRectEqualToRect(rect1, rect2);

To determine whether a given point is contained inside a CGRect, use the CGRectContainsPoint method. This is particularly useful when determining whether a user has tapped inside a particular region. The point is represented as a CGPoint structure:

BOOL containsPoint = CGRectContainsPoint(rect, point);

A similar function can be used to determine whether one CGRect structure contains another CGRect structure. This is useful when testing whether certain objects overlap:

BOOL containsRect = CGRectContainsRect(rect1, rect2);

To determine whether two CGRect structures intersect, use the CGRectIntersectsRect function:

BOOL doesIntersect = CGRectIntersectsRect(rect1, rect2);

4.1.3.2. Edge and center detection

The following functions can be used to determine the various edges of a rectangle and calculate the coordinates of the rectangle's center. All of these functions accept a CGRect structure as their only argument and return a float value:


CGRectGetMinX

Returns the coordinate of the left edge of the rectangle.


CGRectGetMinY

Returns the coordinate of the bottom edge of the rectangle.


CGRectGetMidX

Returns the center X coordinate of the rectangle.


CGRectGetMidY

Returns the center Y coordinate of the rectangle.


CGRectGetMaxX

Returns the coordinate of the right edge of the rectangle.


CGRectGetMaxY

Returns the coordinate of the upper edge of the rectangle.