Skip to content

Commit

Permalink
Merge pull request #13 from sobri909/master
Browse files Browse the repository at this point in the history
safety checks, to avoid crashes
  • Loading branch information
malcommac authored Jun 15, 2019
2 parents 5fdfa3e e0e212c commit d30f23d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Pod/Classes/SwiftSimplify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 53,8 @@ open class SwiftSimplify {
- returns: Returns an array of simplified points
*/
open class func simplify<T>(_ points: [T], tolerance: Float?, highQuality: Bool = false) -> [T] {
if points.count == 2 {
return points
}
guard points.count > 1 else { return points }

// both algorithms combined for awesome performance
let sqTolerance = (tolerance != nil ? tolerance! * tolerance! : 1.0)
var result: [T] = (highQuality == true ? points : simplifyRadialDistance(points, tolerance: sqTolerance))
Expand Down Expand Up @@ -85,15 84,20 @@ open class SwiftSimplify {
}

fileprivate class func simplifyDouglasPeucker<T>(_ points: [T], tolerance: Float!) -> [T] {
guard points.count > 1 else { return [] }
guard let first = points.first else { return [] }

// simplification using Ramer-Douglas-Peucker algorithm
let last: Int = points.count - 1
var simplified: [T] = [points.first!]
var simplified: [T] = [first]
simplifyDPStep(points, first: 0, last: last, tolerance: tolerance, simplified: &simplified)
simplified.append(points[last])
return simplified
}

fileprivate class func simplifyDPStep<T>(_ points: [T], first: Int, last: Int, tolerance: Float, simplified: inout [T]) {
guard last > first else { return }

var maxSqDistance = tolerance
var index = 0

Expand Down

0 comments on commit d30f23d

Please sign in to comment.