Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bézier Curves

Bézier Curves

Presented at try! Swift Tokyo 2018

Ben Scheirman

March 02, 2018
Tweet

More Decks by Ben Scheirman

Other Decks in Programming

Transcript

  1. let path = UIBezierPath() path.move(to: startPoint) path.addCurve(to: endPoint, controlPoint1: c1,

    controlPoint2: c2) context.addPath(path.cgPath) context.strokePath()
  2. class BezierView : UIView { var startPoint: CGPoint? var endPoint:

    CGPoint? var control1: CGPoint? var control2: CGPoint? ...
  3. func drawPoint(_ context: CGContext, p: CGPoint, color: UIColor, radius: CGFloat)

    { color.setFill() context.addArc(center: p, radius: radius, startAngle: 0, endAngle: .pi * 2, clockwise: true) context.fillPath() }
  4. class BezierView : UIView { var startPoint: CGPoint? var endPoint:

    CGPoint? var control1: CGPoint? var control2: CGPoint? ...
  5. func drawLinesBetween(_ context: CGContext, _ points: [CGPoint]) { points.first.flatMap {

    context.move(to: $0) } points.dropFirst().forEach { context.addLine(to: $0 ) } context.setLineWidth(3) context.setLineDash(phase: 0, lengths: [2]) UIColor.lightGray.setStroke() context.strokePath() }
  6. func drawLinesBetween(_ context: CGContext, _ points: [CGPoint]) { points.first.flatMap {

    context.move(to: $0) } points.dropFirst().forEach { context.addLine(to: $0 ) } context.setLineWidth(3) context.setLineDash(phase: 0, lengths: [2]) UIColor.lightGray.setStroke() context.strokePath() }
  7. func drawLinesBetween(_ context: CGContext, _ points: [CGPoint]) { points.first.flatMap {

    context.move(to: $0) } points.dropFirst().forEach { context.addLine(to: $0 ) } context.setLineWidth(3) context.setLineDash(phase: 0, lengths: [2]) UIColor.lightGray.setStroke() context.strokePath() }
  8. func drawLinesBetween(_ context: CGContext, _ points: [CGPoint]) { points.first.flatMap {

    context.move(to: $0) } points.dropFirst().forEach { context.addLine(to: $0 ) } context.setLineWidth(3) context.setLineDash(phase: 0, lengths: [2]) UIColor.lightGray.setStroke() context.strokePath() if timeValue > 0 { interpolateBetween(context, points) } }
  9. func interpolateBetween(_ context: CGContext, _ points: [CGPoint]) { guard points.count

    >= 2 else { return } var interpolated: [CGPoint] = [] for i in 0 ..< points.count-1 { let p1 = points[i] let p2 = points[i+1] let t = p1.lerp(p2, t: timeValue) interpolated.append(t) drawPoint(context, p: t, color: .lightGray, radius: 6) } }
  10. func interpolateBetween(_ context: CGContext, _ points: [CGPoint]) { guard points.count

    >= 2 else { return } var interpolated: [CGPoint] = [] for i in 0 ..< points.count-1 { let p1 = points[i] let p2 = points[i+1] let t = p1.lerp(p2, t: timeValue) interpolated.append(t) drawPoint(context, p: t, color: .lightGray, radius: 6) } }
  11. func interpolateBetween(_ context: CGContext, _ points: [CGPoint]) { guard points.count

    >= 2 else { return } var interpolated: [CGPoint] = [] for i in 0 ..< points.count-1 { let p1 = points[i] let p2 = points[i+1] let t = p1.lerp(p2, t: timeValue) interpolated.append(t) drawPoint(context, p: t, color: .lightGray, radius: 6) } }
  12. func interpolateBetween(_ context: CGContext, _ points: [CGPoint]) { guard points.count

    >= 2 else { return } var interpolated: [CGPoint] = [] for i in 0 ..< points.count-1 { let p1 = points[i] let p2 = points[i+1] let t = p1.lerp(p2, t: timeValue) interpolated.append(t) drawPoint(context, p: t, color: .lightGray, radius: 6) } }
  13. func interpolateBetween(_ context: CGContext, _ points: [CGPoint]) { guard points.count

    >= 2 else { return } var interpolated: [CGPoint] = [] for i in 0 ..< points.count-1 { let p1 = points[i] let p2 = points[i+1] let t = p1.lerp(p2, t: timeValue) interpolated.append(t) drawPoint(context, p: t, color: .lightGray, radius: 6) } }
  14. func interpolateBetween(_ context: CGContext, _ points: [CGPoint]) { guard points.count

    >= 2 else { return } var interpolated: [CGPoint] = [] for i in 0 ..< points.count-1 { let p1 = points[i] let p2 = points[i+1] let t = p1.lerp(p2, t: timeValue) interpolated.append(t) drawPoint(context, p: t, color: .lightGray, radius: 6) } }
  15. func interpolateBetween(_ context: CGContext, _ points: [CGPoint]) { guard points.count

    >= 2 else { return } var interpolated: [CGPoint] = [] for i in 0 ..< points.count-1 { let p1 = points[i] let p2 = points[i+1] let t = p1.lerp(p2, t: timeValue) interpolated.append(t) drawPoint(context, p: t, color: .lightGray, radius: 6) } if interpolated.count >= 2 { drawLinesBetween(context, interpolated) } }
  16. func interpZip(points: [CGPoint]) -> (CGFloat) -> [CGPoint] { return {

    t in let pairs = zip(points, points.dropFirst()) return pairs.map { pair in return lerp(pair.0, pair.1, t) } } }
  17. func interpZip(points: [CGPoint]) -> (CGFloat) -> [CGPoint] { return {

    t in let pairs = zip(points, points.dropFirst()) return pairs.map { pair in return lerp(pair.0, pair.1, t) } } }
  18. func interpZip(points: [CGPoint]) -> (CGFloat) -> [CGPoint] { return {

    t in let pairs = zip(points, points.dropFirst()) return pairs.map { pair in return lerp(pair.0, pair.1, t) } } }
  19. func interpZip(points: [CGPoint]) -> (CGFloat) -> [CGPoint] { return {

    t in let pairs = zip(points, points.dropFirst()) return pairs.map { pair in return pair.0.lerp(pair.1, t: timeValue) } } }
  20. Thank you ! Ben Scheirman nsscreencast.com @subdigital Ask me for

    a sticker! ステッカーが欲しかったら話 しかけてください!
  21. Thank you ! Ben Scheirman nsscreencast.com @subdigital Ask me for

    a sticker! ステッカーが欲しかったら話 しかけてください!