Skip to content

Instantly share code, notes, and snippets.

@malcommac
Created October 15, 2015 10:14
Show Gist options
  • Save malcommac/523a1bd1b0bd470a01cb to your computer and use it in GitHub Desktop.
Save malcommac/523a1bd1b0bd470a01cb to your computer and use it in GitHub Desktop.
Distance At Best Angle
public static func distanceAtBestAngle(points: [StrokePoint], strokeTemplate: [StrokePoint], var fromAngle: Double, var toAngle: Double, threshold: Double) -> Double {
var x1 = StrokeConsts.Phi * fromAngle + (1.0 - StrokeConsts.Phi) * toAngle
var f1 = StrokePoint.distanceAtAngle(points, strokeTemplate: strokeTemplate, radians: x1)
var x2 = (1.0 - StrokeConsts.Phi) * fromAngle + StrokeConsts.Phi * toAngle
var f2 = StrokePoint.distanceAtAngle(points, strokeTemplate: strokeTemplate, radians: x2)
while ( abs(toAngle-fromAngle) > threshold ) {
if f1 < f2 {
toAngle = x2
x2 = x1
f2 = f1
x1 = StrokeConsts.Phi * fromAngle + (1.0 - StrokeConsts.Phi) * toAngle
f1 = StrokePoint.distanceAtAngle(points, strokeTemplate: strokeTemplate, radians: x1)
} else {
fromAngle = x1
x1 = x2
f1 = f2
x2 = (1.0 - StrokeConsts.Phi) * fromAngle + StrokeConsts.Phi * toAngle
f2 = StrokePoint.distanceAtAngle(points, strokeTemplate: strokeTemplate, radians: x2)
}
}
return min(f1,f2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment