iOverlay

main

Boolean Operations for 2D Polygons: Supports intersection, union, difference, xor, and self-intersections for all polygon varieties.
iShape-Swift/iOverlay

iOverlay

The iOverlay is a fast poly-bool library supporting main operations like union, intersection, difference, and xor, governed by either the even-odd or non-zero rule. This algorithm is based on Vatti clipping ideas but is an original implementation.

Try out iOverlay with an interactive demo. The demo covers operations like union, intersection, difference and exclusion

Features

  • Operations: union, intersection, difference, and exclusion.
  • Polygons: with holes, self-intersections, and multiple paths.
  • Simplification: removes degenerate vertices and merges collinear edges.
  • Fill Rules: even-odd and non-zero.

Working Range and Precision

The iOverlay library operates within the following ranges and precision levels:

Extended Range: From -1,000,000 to 1,000,000 with a precision of 0.001. Recommended Range: From -100,000 to 100,000 with a precision of 0.01 for more accurate results. Utilizing the library within the recommended range ensures optimal accuracy in computations and is advised for most use cases.

Installation

Installing iOverlay is simple and easy using Swift Package Manager. Just follow these steps:

  • Open your Xcode project.
  • Select your project and open tab Package Dependencies.
  • Click on the "+" button.
  • In search bar enter https://github.com/iShape-Swift/iOverlay
  • Click the "Add" button.
  • Wait for the package to be imported.
  • In your Swift code, add the following using statement to access the library:
import iOverlay

Usage

Here's an example of how you can create a square with a hole and union / differnce / intersect / xor with other polygon:

var overlay = CGOverlay()

// add shape
overlay.add(path: [
    CGPoint(x:-20, y:-16),
    CGPoint(x:-20, y: 16),
    CGPoint(x: 20, y: 16),
    CGPoint(x: 20, y:-16)
], type: ShapeType.subject)

// add hole
overlay.add(path: [
    CGPoint(x:-12, y:-8),
    CGPoint(x:-12, y: 8),
    CGPoint(x: 12, y: 8),
    CGPoint(x: 12, y:-8)
], type: ShapeType.subject)

// add clip
overlay.add(path: [
    CGPoint(x:-4, y:-24),
    CGPoint(x:-4, y: 24),
    CGPoint(x: 4, y: 24),
    CGPoint(x: 4, y:-24)
], type: ShapeType.clip)

// make overlay graph
let graph = overlay.buildGraph()

// get union shapes
let union = graph.extractShapes(overlayRule: OverlayRule.union)

// get difference shapes
let difference = graph.extractShapes(overlayRule: OverlayRule.difference)

// get intersect shapes
let intersect = graph.extractShapes(overlayRule: OverlayRule.intersect)

// get exclusion shapes
let xor = graph.extractShapes(overlayRule: OverlayRule.xor)

// get clean shapes from subject, self intersections will be removed
let subject = graph.extractShapes(overlayRule: OverlayRule.subject)

Union

Difference

Intersection

Exclusion (xor)

Self-intersection

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

Last updated: Thu Nov 28 2024 01:11:48 GMT-1000 (Hawaii-Aleutian Standard Time)