title: Draw Bezier Curve using DeCasteljau Algorithm using Swift
body: <p>I would like to know how to draw Bezier Curve using deCasteljau algorithm where the user input N custom point. For example when the user touch on the screen, it create a point and when the next point is created, a line is drawn in between. The problem I face is I only manage to draw the Bezier Curve with four N point present. How to continue the Bezier Curve depends on the number of N points using the algorithm. I am still new to Swift so any help is welcome.</p>
  
  <p>For now I set the control point to be fixed position. </p>
  
  <p><a href="https://i.stack.imgur.com/2rwop.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2rwop.png" alt="enter image description here"></a></p>
  
  <pre><code>import UIKit
  import GLKit
  
   //Class to store the x and y coordinates
   class point_arr{   
      var x:CGFloat=0 
      var y:CGFloat=0  
   }
  
   public var screenWidth: CGFloat {
       return UIScreen.main.bounds.width
  
   }
  
    public var screenHeight: CGFloat {
       return UIScreen.main.bounds.height
  
   }
  
   class ViewController: GLKViewController {
  
  private var context: EAGLContext?
  var lastPosition: CGPoint?
  var point_count :Int = 0
  //var points = Array&lt;Array&lt;CGPoint&gt;&gt;() // Store array of CGPoints
  
  //To store the position of the coordinates
  var points:[point_arr] = []
  var position_x: CGFloat = 0
  var position_y: CGFloat = 0
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view.
      EAGLcontext()
      // Gesture Code
   }
  
  // Touch Function
  override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      if let touch = touches.first{
  
          let position = touch.location(in: view)
          if let lastPosition = self.lastPosition {
              self.drawLineFromPoint(start: lastPosition, toPoint: position, ofColor: UIColor.red, inView: self.view)
          }
          self.lastPosition = position
          // View the x and y coordinates
          let dot = UIView(frame: CGRect(x: position.x, y: position.y, width: 10, height: 10))
          dot.backgroundColor = .red
          view.addSubview(dot)
  
          position_x=position.x
          position_y=position.y
  
          //Assign the point_array
          let dots = point_arr()
          dots.x = position_x
          dots.y = position_y
          points.append(dots)
  
          print("Position :",points[point_count].x,",",points[point_count].y)
          point_count+=1
          print("Number of points : ", point_count)
          print(points)
  
      }
  }
  
  override func touchesEnded(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      if let touch = touches.first, let lastPosition = self.lastPosition{
  
          let position = touch.location(in: view)
          self.drawLineFromPoint(start: lastPosition, toPoint: position, ofColor: UIColor.red, inView: self.view)
          self.lastPosition = position
          let dot = UIView(frame: CGRect(x: position.x, y: position.y, width: 10, height: 10))
          dot.backgroundColor = .red
          view.addSubview(dot)
  
      }
  }
  
  //calculation for Bezier curve
  func calculateBezier(point p1:CGPoint, andPoint p2:CGPoint, point_count:Int) {
  
      //Testing Purposes
      //for t in stride(from: 0, to: 1, by: 0.125){}
      //print(p1.x,p1.y)
      //print(p2.x,p2.y)
      //print("Number of points: ",point_count)
      //print(points)
      //print("Factorial of N:",factorial(n: point_count))
  
      //Do the calculation here
  
  
  }
  
  
  //factorial recursive function
  func factorial ( n:Int)-&gt;Int   {
  
      var ans = n
      if (ans&lt;=1) {
          return (1)
      }
      else {
          ans = ans * (factorial(n:n - 1))
      }
      return ans
  }
  
  func binomial_coff(n:Int , i:Int)-&gt;Int{
  
      var ans:Int
      ans = factorial(n: n) / (factorial(n: n - i) * factorial(n: i))
      return ans
  
  }
  
  
  //Draw line from point to point
  func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {
  
      let path = UIBezierPath()
      path.move(to: start)
      path.addLine(to: end)
  
      //Draw the bezier curve taking two control points.
      if(points.count&gt;3){
          path.move(to: CGPoint(x: points[0].x, y: points[0].y ))
          path.addCurve(to: CGPoint(x: position_x , y: position_y),
                        controlPoint1: CGPoint(x: points[1].x , y: points[1].y), // This CP is fixed so need to change
                        controlPoint2: CGPoint(x: points[2].x , y: points[2].y)
          )
  
      }
  
      let shapeLayer = CAShapeLayer()
      shapeLayer.path = path.cgPath
      shapeLayer.strokeColor = UIColor.black.cgColor
      shapeLayer.lineWidth = 1.0
      shapeLayer.fillColor = UIColor.clear.cgColor
  
      view.layer.addSublayer(shapeLayer)
  }
  
  
  //Create EAGL Context for the GLKView
  private func EAGLcontext() {
  
      context = EAGLContext(api: .openGLES2)
  
      EAGLContext.setCurrent(context)
  
      if let view = self.view as? GLKView, let context = context {
  
          view.context = context
          delegate = self
      }
  }
  
  override func glkView(_ view: GLKView, drawIn rect: CGRect) {
      //Set the color
      glClearColor(0.85, 0.85, 0.85, 1.0)
      glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
      }
  }
  extension ViewController: GLKViewControllerDelegate {
  func glkViewControllerUpdate(_ controller: GLKViewController) {
  
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
          }
      }
  </code></pre>
  
  <p>The expected result is the more points the user created, the curve continue to be drawn using the algorithm.</p>