Skip to content

Instantly share code, notes, and snippets.

@erica
Last active November 15, 2016 09:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erica/e602e30c69cf691acc16 to your computer and use it in GitHub Desktop.
Save erica/e602e30c69cf691acc16 to your computer and use it in GitHub Desktop.
public func projectFunctionToCoordinateSystem(function f: FunctionType) -> (p0: CGPoint, p1: CGPoint) -> (x: CGFloat) -> CGPoint {
return { p0, p1 in
return { x in
let (dx, dy) = (p1.x - p0.x, p1.y - p0.y)
let (magnitude, theta) = (hypot(dy, dx), atan2(dy, dx)) // Thanks loooop
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)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment