CoreGraphics Geometry Primitives
CGVector isn’t widely used in view programming; instead, CGSize values are typically used to express positional vectors. Unfortunately, this can result in awkward semantics, because sizes may have negative width and / or height components (in which case a rectangle is extended in the opposite direction along that dimension).
The article provides a concise guide to Core Graphics geometry types in Swift, essential for Quartz 2D drawing on Apple platforms, covering CGFloat, CGPoint, CGVector, CGSize, and CGRect, with iOS using a top-left origin and macOS a bottom-left origin by default.
Developers can access and mutate properties like point.x or rectangle.width, but computed properties like width are read-only, while minX, midX, maxX, minY, midY, and maxY offer positional insights, and a custom center extension can be added.
Normalisation methods like standardized ensure non-negative rectangle dimensions, and integral aligns to pixel boundaries to avoid blurry rendering. Transformations include offsetBy(dx:dy:) for translation and insetBy(dx:dy:) for contracting or expanding rectangles, with CGAffineTransform supporting complex operations. Special values like CGPoint.zero, CGRect.infinite, and CGRect.null handle edge cases, while intersects(_:) and union(_:) manage rectangle relationships, enabling precise UI design with Swift’s clear syntax and safety features.
Enjoyed this post?
Well, you could share the post with others, follow me with RSS Feeds, send me a comment via email, and/or leave a donation in the Tip Jar.
Tags
Category:
Tags:
Year: