Latest 2.3
Homepage https://github.com/calda/CGPathIntersection
License MIT
Platforms ios 10.0, requires ARC
Authors

CGPathIntersection is a CoreGraphics library that identifies points where two CGPaths intersect.

Surprisingly, this is not provided out-of-the-box by CoreGraphics. Intersections can be calculated analytically for simple geometric shapes (especially straight lines), but that method becomes rather challenging when considering a CGPath can be arbitrarily complex. CGPathIntersection solves this problem by rendering each path into an image and then finding the exact pixels where they intersect.

Installation

Carthage

Add github "calda/CGPathIntersection" to your Cartfile

CocoaPods

Add pod 'CGPathIntersection' to your Podfile

Usage

import CGPathIntersection

let path1 = CGPath(...)
let path2 = CGPath(...)

path1.intersects(path: path2) //returns a boolean
path1.intersectionPoints(with: path2) //returns an array of points

If performing many calculations, you can increase performance by creating a CGPathImage. Any calculations performed on a pre-existing CGPathImage will run faster than the same calculation performed on a raw CGPath.

import CGPathIntersection

let pathImage = CGPathImage(from: CGPath(...))
let otherPathImages: [CGPathImage] = [...]

let intersectingPaths = otherPathImages.filter{ path.intersects(path: $0) }

Example

CGPathIntersection was created as a component of Streets, a prototype SpriteKit game that simulates managing a network of streets. Streets uses CGPathIntersection to connect individual roads together with physical intersections. When a car reaches an intersection, it makes a random turn onto one of the other connected roads.

Streets also has some support for more complex paths, like roundabouts:

Latest podspec

{
    "name": "CGPathIntersection",
    "platforms": {
        "ios": "10.0"
    },
    "version": "2.3",
    "summary": "A CoreGraphics library that identifies points where two CGPaths intersect",
    "homepage": "https://github.com/calda/CGPathIntersection",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Cal Stephens": "[email protected]"
    },
    "source": {
        "git": "https://github.com/calda/CGPathIntersection.git",
        "tag": "2.3"
    },
    "source_files": "CGPathIntersection/*.swift",
    "exclude_files": [
        "README.md",
        "LICENSE.md",
        "CGPathIntersection.podspec",
        "images/*"
    ],
    "requires_arc": true
}

Pin It on Pinterest

Share This