Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. typealias ValueBuilderType = (CGFloat) -> CGFloat
  2.  
  3. func BuildPath(f : ValueBuilderType)(p0: CGPoint, p1: CGPoint)(x: CGFloat) -> CGPoint {
  4. let dx = p1.x - p0.x
  5. let dy = p1.y - p0.y
  6. let magnitude = sqrt(dy * dy + dx * dx)
  7. let theta = atan2(dy, dx)
  8.  
  9. var outPoint = CGPointMake(x * magnitude, f(x) * magnitude)
  10. outPoint = CGPointApplyAffineTransform(outPoint, CGAffineTransformMakeRotation(theta))
  11. outPoint = CGPointApplyAffineTransform(outPoint, CGAffineTransformMakeTranslation(p0.x, p0.y))
  12. return CGPointMake(outPoint.x, outPoint.y)
  13. }
  14.  
  15. // A few functions to kick around
  16. let fnsin : ValueBuilderType = {CGFloat(0.5 * sin($0 * CGFloat(M_PI)))}
  17. let fnsoftsin : ValueBuilderType = {CGFloat(0.5 * sin($0 * CGFloat(2.0 * M_PI)))}
  18. let fnsawtooth : ValueBuilderType = {($0 * 5) % 1}
  19.  
  20. let randomPosition = {CGFloat(arc4random_uniform(200))}
  21. var p0 = CGPointMake(randomPosition(), randomPosition())
  22. var p1 = CGPointMake(randomPosition(), randomPosition())
  23. let tuple = (p0: p0, p1: p1)
  24. let base = BuildPath([fnsin, fnsoftsin, fnsawtooth][Int(arc4random_uniform(3))])
  25. let f = base(tuple)
  26.  
  27. let path = UIBezierPath(); path.moveToPoint(p1); path.addLineToPoint(p0)
  28. for value in stride(from: 0.0, to: 1.01, by: 0.01) {
  29. path.addLineToPoint(f(x: CGFloat(value)))
  30. }
  31. path.closePath()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement