Code Monkey home page Code Monkey logo

swiftclipper's Introduction

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.

import SwiftClipper

let path = [CGPoint(x: -10, y: 10), CGPoint(x: 20, y: 10), CGPoint(x: 10, y: 0), CGPoint(x: 25, y: -8)]
let path2 = [CGPoint(x: -5, y: 5), CGPoint(x: 20, y: 5), CGPoint(x: 20, y: -15), CGPoint(x: -5, y: -15)]

let intersections  = path.intersection(path2) 
//[[(15.0, 5.0), (10.0, 0.0), (0.0, 5.0)]]

let unions = path.union(path2)
//[[(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.

import SwiftClipper

let path1 = [CGPoint(x: -10, y: 10), CGPoint(x: 20, y: 10), CGPoint(x: 10, y: 0), CGPoint(x: 25, y: -8)]
let path2 = [CGPoint(x: -5, y: 5), CGPoint(x: 20, y: 5), CGPoint(x: 20, y: -15), CGPoint(x: -5, y: -15)]

var paths = Paths()
let c = Clipper(options: .strictlySimple)
c.addPath(path1, .subject, isClosed)
c.addPath(path2, .clip, isClosed)
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.

swiftclipper's People

Contributors

lhuanyu avatar literalpie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.