Skip to content

Instantly share code, notes, and snippets.

@fewlinesofcode
Created March 22, 2020 15:03
Show Gist options
  • Save fewlinesofcode/283a0c046d14340be8c49cd2c3169ae4 to your computer and use it in GitHub Desktop.
Save fewlinesofcode/283a0c046d14340be8c49cd2c3169ae4 to your computer and use it in GitHub Desktop.
Implements Chaikin corners rounding algorithm on ordered set of points
import UIKit
extension UIBezierPath {
private static func midpoint(_ a: CGPoint, b: CGPoint) -> CGPoint {
CGPoint(
x: (b.x + a.x) / 2,
y: (b.y + a.y) / 2
)
}
static func chaikinPath(_ pts: [CGPoint]) -> UIBezierPath? {
guard pts.count > 2 else {
return nil
}
let path = UIBezierPath()
for i in 1...pts.count {
let prev = pts[i-1]
let cp = pts[i % pts.count]
let next = pts[(i + 1) % pts.count]
path.move(
to: midpoint(prev, b: cp)
)
path.addQuadCurve(
to: midpoint(cp, b: next),
controlPoint: cp
)
}
path.close()
return path
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment