Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports mandatory points to be #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/SwiftSimplify/Point2DRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import CoreLocation
public protocol Point2DRepresentable {
var xValue: Float { get }
var yValue: Float { get }
var isMandatory: Bool { get }

var cgPoint: CGPoint { get }

Expand All @@ -47,6 +48,10 @@ public protocol Point2DRepresentable {

public extension Point2DRepresentable {

var isMandatory: Bool {
return false
}

func equalsTo(_ compare: Self) -> Bool {
xValue == compare.xValue && yValue == compare.yValue
}
Expand Down
28 changes: 21 additions & 7 deletions Sources/SwiftSimplify/SwiftSimplify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,29 @@ import Foundation
public enum SwiftSimplify {

public static func simplify<P: Point2DRepresentable>(_ points: [P], tolerance: Float?, highestQuality: Bool = false) -> [P] {
guard points.count > 1 else {
return points
}

let sqTolerance = tolerance != nil ? (tolerance! * tolerance!) : 1.0
var result = highestQuality ? points : simplifyRadialDistance(points, tolerance: sqTolerance)
result = simplifyDouglasPeucker(result, sqTolerance: sqTolerance)

// Split the given points by their mandatory members.
// Since the first (and last) point of each simplification loop will be
// added to the final result, all mandatory points will be included.
let splitPoints = points
.split(whereSeparator: { $0.isMandatory })
.map({ Array($0) })

return result
var results: [[P]] = []

for somePoints in splitPoints {
guard somePoints.count > 1 else {
results.append(somePoints)
continue
}

var result = highestQuality ? somePoints : simplifyRadialDistance(somePoints, tolerance: sqTolerance)
result = simplifyDouglasPeucker(result, sqTolerance: sqTolerance)
results.append(result)
}

return results.flatMap({ return $0 })
}

private static func simplifyRadialDistance<P: Point2DRepresentable>(_ points: [P], tolerance: Float) -> [P] {
Expand Down