Skip to content

Instantly share code, notes, and snippets.

@malcommac
Created October 15, 2015 10:07
Show Gist options
  • Save malcommac/f03b560ed5d097dbf9c3 to your computer and use it in GitHub Desktop.
Save malcommac/f03b560ed5d097dbf9c3 to your computer and use it in GitHub Desktop.
Resample Points
private static func resample(points: [StrokePoint], totalPoints: Int) -> [StrokePoint] {
var initialPoints = points
let interval = StrokePoint.pathLength(initialPoints) / Double(totalPoints - 1)
var totalLength: Double = 0.0
var newPoints: [StrokePoint] = [points.first!]
for var i = 1; i < initialPoints.count; ++i { let currentLength = initialPoints[i-1].distanceTo(initialPoints[i]) if ( (totalLength+currentLength) >= interval) {
let qx = initialPoints[i-1].x + ((interval - totalLength) / currentLength) * (initialPoints[i].x - initialPoints[i-1].x)
let qy = initialPoints[i-1].y + ((interval - totalLength) / currentLength) * (initialPoints[i].y - initialPoints[i-1].y)
let q = StrokePoint(x: qx, y: qy)
newPoints.append(q)
initialPoints.insert(q, atIndex: i)
totalLength = 0.0
} else {
totalLength += currentLength
}
}
if newPoints.count == totalPoints-1 {
newPoints.append(points.last!)
}
return newPoints
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment