Skip to content

Instantly share code, notes, and snippets.

@erica
Created December 18, 2015 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/d6e9e284d4a71cf68214 to your computer and use it in GitHub Desktop.
Save erica/d6e9e284d4a71cf68214 to your computer and use it in GitHub Desktop.
public typealias FunctionType = (CGFloat) -> CGFloat
//: Use currying to partially apply function to the
//: coordinate system established between two points
//: Recipe 4-7
public func projectFunctionToCoordinateSystem(function f: FunctionType)(p0: CGPoint, p1: CGPoint)(x: CGFloat) -> CGPoint {
let dx = p1.x - p0.x
let dy = p1.y - p0.y
let magnitude = sqrt(dy * dy + dx * dx)
let theta = atan2(dy, dx)
var outPoint = CGPoint(x: x * magnitude, y: f(x) * magnitude)
outPoint = CGPointApplyAffineTransform(outPoint, CGAffineTransformMakeRotation(theta))
outPoint = CGPointApplyAffineTransform(outPoint, CGAffineTransformMakeTranslation(p0.x, p0.y))
return CGPoint(x: outPoint.x, y: outPoint.y)
}
//: Build path using f(x)
public func buildPath(f: (CGFloat) -> CGPoint) -> UIBezierPath {
let path = UIBezierPath(); path.moveToPoint(f(0.0))
let dx = 0.01
for value in 0.0.stride(to: 1.01, by: dx) {
path.addQuadCurveToPoint(f(CGFloat(value)), controlPoint: f(CGFloat(value - dx / 2.0)))
}
return path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment