calda/CGPathIntersection
:curly_loop: A library that identifies points where two CGPaths intersect
Installation
Swift Package Manager
Add the following dependency to your package definition:
.package(
name: "CGPathIntersection",
url: "https://github.com/calda/CGPathIntersection.git",
from: "4.0.0")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(path2) // returns a boolean
path1.intersectionPoints(with: path2) // returns an array of pointsIf 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 { pathImage.intersects($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.
<p align="center"> <img src="images/streets.gif" width=250px> <img src="images/streets 2.gif" width=250px> </p>
Streets also has some support for more complex paths, like roundabouts:
<p align="center"> <img src="images/roundabout.jpg" width=250px> </p>
Package Metadata
Repository: calda/CGPathIntersection
Stars: 67
Forks: 5
Open issues: 0
Default branch: master
Primary language: swift
License: MIT
Topics: core-graphics, intersection, ios, swift
README: README.md