项目作者: lhuanyu

项目描述 :
A Swift version of polygon clipper.
高级语言: Swift
项目地址: git://github.com/lhuanyu/SwiftClipper.git
创建时间: 2019-10-13T07:22:46Z
项目社区:https://github.com/lhuanyu/SwiftClipper

开源协议:

下载


SwiftClipper

This package is basically a Swift translation of the Clipper Lib authored by Angusj.
For the consideration of complexity and performance, some features(like big integer support, line clipping) have been temporarily removed. Most codes are reorganized or rewritten in a Swift convention.

Usage

Basic functions are written as extensions of CGPoint Array.

  1. import SwiftClipper
  2. let path = [CGPoint(x: -10, y: 10), CGPoint(x: 20, y: 10), CGPoint(x: 10, y: 0), CGPoint(x: 25, y: -8)]
  3. let path2 = [CGPoint(x: -5, y: 5), CGPoint(x: 20, y: 5), CGPoint(x: 20, y: -15), CGPoint(x: -5, y: -15)]
  4. let intersections = path.intersection(path2)
  5. //[[(15.0, 5.0), (10.0, 0.0), (0.0, 5.0)]]
  6. let unions = path.union(path2)
  7. //[[(15.0, 5.0), (20.0, 10.0), (-10.0, 10.0), (0.0, 5.0), (-5.0, 5.0), (-5.0, -15.0), (20.0, -15.0), (20.0, 5.0)]]

You can also use the Clipper class for customized operations.

  1. import SwiftClipper
  2. let path1 = [CGPoint(x: -10, y: 10), CGPoint(x: 20, y: 10), CGPoint(x: 10, y: 0), CGPoint(x: 25, y: -8)]
  3. let path2 = [CGPoint(x: -5, y: 5), CGPoint(x: 20, y: 5), CGPoint(x: 20, y: -15), CGPoint(x: -5, y: -15)]
  4. var paths = Paths()
  5. let c = Clipper(options: .strictlySimple)
  6. c.addPath(path1, .subject, isClosed)
  7. c.addPath(path2, .clip, isClosed)
  8. try c.execute(clipType: .difference, solution: &paths)

Additonal userful functions for geometry caculations are also included in these extensions. For details, check Path+Geometry.swift and Point.swift.

Note

The original lib uses integer numeric for caculations due to the presicion issue. For some cases, using a minor floating number as coordinates can reach a unpredicable or unprecise result. If you want to a more precise result, try to use a magnification scale for your numbers.