Using objects is the right thing to do here. You can always create a spline 
object and just:
    
    
    spline.v.add(fd1)
    
    
    Run
    
    
    import vmath
    
    type
      Seg = array[4, Vec2]
      Spline = object
        t: seq[float32]
        xy: seq[Vec2]
        v: seq[Vec2]
        a: seq[Vec2]
        length: seq[float32]
    
    proc bezier(segment: Seg, res: int): Spline =
      let
        t1  = 1 / float32(res)
        t2 = t1 * t1
        t3 = t2 * t1
        b0 = segment[0]
        b1 = segment[1]
        b2 = segment[2]
        b3 = segment[3]
        a  = -b0 + 3 * b1 + -3 * b2 + b3
        b  = 3 * b0 + -6 * b1 + 3 * b2
        c  = -3 * b0 + 3 * b1
      var
        d  = b0
        fd1 = a * t3 + b * t2 + c * t1  #initial velocity
        fd2 = 6 * a * t3 + 2 * b * t2   #initial acceleration
        fd3 = 6 * a * t3                #acceleration change
      
      var spline = Spline()
      spline.t = @[float32(0.0)]
      spline.xy = @[d]
      spline.v = @[fd1]
      spline.a = @[fd2]
      spline.length = @[length(d)]
      for i in 1 .. res:
        d += fd1
        fd1 += fd2    #speed
        fd2 += fd3    #acceleration
        spline.t.add(float32(i)*t1)
        spline.xy.add(d)
        spline.v.add(fd1)
        spline.a.add(fd2)
        spline.length.add(length(d))
      return spline
    
    
    Run

Reply via email to