CGPathIntersection
:curly_loop: A library that identifies points where two CGPaths intersect
Install / Use
/learn @calda/CGPathIntersectionREADME
CGPathIntersection
CGPathIntersection is a library for iOS, macOS, and tvOS 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
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 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 { 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>Related Skills
node-connect
345.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
106.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
345.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
345.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
