Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/calda/cgpathintersection
:curly_loop: A library that identifies points where two CGPaths intersect
https://github.com/calda/cgpathintersection
core-graphics intersection ios swift
Last synced: 3 months ago
JSON representation
:curly_loop: A library that identifies points where two CGPaths intersect
- Host: GitHub
- URL: https://github.com/calda/cgpathintersection
- Owner: calda
- License: mit
- Created: 2016-11-13T18:52:51.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-02-05T23:54:57.000Z (about 3 years ago)
- Last Synced: 2024-10-14T03:42:35.350Z (4 months ago)
- Topics: core-graphics, intersection, ios, swift
- Language: Swift
- Homepage:
- Size: 6.2 MB
- Stars: 69
- Watchers: 5
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CGPathIntersection
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fcalda%2FCGPathIntersection%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/calda/CGPathIntersection) [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fcalda%2FCGPathIntersection%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/calda/CGPathIntersection)
**CGPathIntersection** is a library for iOS, macOS, and tvOS that identifies points where two `CGPath`s 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](https://www.swift.org/package-manager/)
Add the following dependency to your package definition:```swift
.package(
name: "CGPathIntersection",
url: "https://github.com/calda/CGPathIntersection.git",
from: "4.0.0")
```#### [Carthage](https://github.com/Carthage/Carthage)
Add `github "calda/CGPathIntersection"` to your Cartfile#### [CocoaPods](https://github.com/cocoapods/cocoapods)
Add `pod 'CGPathIntersection'` to your Podfile## Usage
```swift
import CGPathIntersectionlet 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`.
```swift
import CGPathIntersectionlet pathImage = CGPathImage(from: CGPath(...))
let otherPathImages: [CGPathImage] = [...]let intersectingPaths = otherPathImages.filter { pathImage.intersects($0) }
```## Example
CGPathIntersection was created as a component of **[Streets](http://github.com/calda/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: