Created
March 22, 2020 15:03
-
-
Save fewlinesofcode/283a0c046d14340be8c49cd2c3169ae4 to your computer and use it in GitHub Desktop.
Implements Chaikin corners rounding algorithm on ordered set of points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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