Skip to content

Instantly share code, notes, and snippets.

@FlexMonkey
Created January 13, 2016 13:51
Show Gist options
  • Save FlexMonkey/1cb50efd7427c37298af to your computer and use it in GitHub Desktop.
Save FlexMonkey/1cb50efd7427c37298af to your computer and use it in GitHub Desktop.
interpolatePointsWithHermite() - adds a Hermite interpolated curve to a UIBezierPath()
extension UIBezierPath
{
func interpolatePointsWithHermite(interpolationPoints : [CGPoint])
{
let n = interpolationPoints.count - 1
for var ii = 0; ii < n; ++ii
{
var currentPoint = interpolationPoints[ii]
if ii == 0
{
self.moveToPoint(interpolationPoints[0])
}
var nextii = (ii + 1) % interpolationPoints.count
var previi = (ii - 1 < 0 ? interpolationPoints.count - 1 : ii-1);
var previousPoint = interpolationPoints[previi]
var nextPoint = interpolationPoints[nextii]
let endPoint = nextPoint;
var mx : CGFloat = 0.0
var my : CGFloat = 0.0
if ii > 0
{
mx = (nextPoint.x - currentPoint.x) * 0.5 + (currentPoint.x - previousPoint.x) * 0.5;
my = (nextPoint.y - currentPoint.y) * 0.5 + (currentPoint.y - previousPoint.y) * 0.5;
}
else
{
mx = (nextPoint.x - currentPoint.x) * 0.5;
my = (nextPoint.y - currentPoint.y) * 0.5;
}
let controlPoint1 = CGPoint(x: currentPoint.x + mx / 3.0, y: currentPoint.y + my / 3.0)
currentPoint = interpolationPoints[nextii]
nextii = (nextii + 1) % interpolationPoints.count
previi = ii;
previousPoint = interpolationPoints[previi]
nextPoint = interpolationPoints[nextii]
if ii < n - 1
{
mx = (nextPoint.x - currentPoint.x) * 0.5 + (currentPoint.x - previousPoint.x) * 0.5;
my = (nextPoint.y - currentPoint.y) * 0.5 + (currentPoint.y - previousPoint.y) * 0.5;
}
else
{
mx = (currentPoint.x - previousPoint.x) * 0.5;
my = (currentPoint.y - previousPoint.y) * 0.5;
}
let controlPoint2 = CGPoint(x: currentPoint.x - mx / 3.0, y: currentPoint.y - my / 3.0)
self.addCurveToPoint(endPoint, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment