Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
typealias ValueBuilderType = (CGFloat) -> CGFloat
func BuildPath(f : ValueBuilderType)(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 = CGPointMake(x * magnitude, f(x) * magnitude)
outPoint = CGPointApplyAffineTransform(outPoint, CGAffineTransformMakeRotation(theta))
outPoint = CGPointApplyAffineTransform(outPoint, CGAffineTransformMakeTranslation(p0.x, p0.y))
return CGPointMake(outPoint.x, outPoint.y)
}
// A few functions to kick around
let fnsin : ValueBuilderType = {CGFloat(0.5 * sin($0 * CGFloat(M_PI)))}
let fnsoftsin : ValueBuilderType = {CGFloat(0.5 * sin($0 * CGFloat(2.0 * M_PI)))}
let fnsawtooth : ValueBuilderType = {($0 * 5) % 1}
let randomPosition = {CGFloat(arc4random_uniform(200))}
var p0 = CGPointMake(randomPosition(), randomPosition())
var p1 = CGPointMake(randomPosition(), randomPosition())
let tuple = (p0: p0, p1: p1)
let base = BuildPath([fnsin, fnsoftsin, fnsawtooth][Int(arc4random_uniform(3))])
let f = base(tuple)
let path = UIBezierPath(); path.moveToPoint(p1); path.addLineToPoint(p0)
for value in stride(from: 0.0, to: 1.01, by: 0.01) {
path.addLineToPoint(f(x: CGFloat(value)))
}
path.closePath()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.