       # xn1 =   dx / sqrt(dx^2+dy^2)
       # yn1 = - dy / sqrt(dx^2+dy^2)
 
-      qxy = Math.sqrt( dx*dx+dy*dy)
+      #qxy = Math.sqrt( dx*dx+dy*dy)
       #zn1 = 0
-      if qxy < 1e-3
-        xn1 = 1.0
-        yn1 = 0.0
-      else
-        iqxy = 1 / qxy
-        xn1 = dy * iqxy
-        yn1 = -dx * iqxy
+      #if qxy < 1e-3
+      #  xn1 = 1.0
+      #  yn1 = 0.0
+      #else
+      #  iqxy = 1 / qxy
+      #  xn1 = dy * iqxy
+      #  yn1 = -dx * iqxy
       #now calculate the third vector, as a X-product of
-      # (xn1, yn1, 0) X (dx, dy, dz)
+      # (xn1, yn1, zn1) X (dx, dy, dz)
       # -dz*yn1*i +dz*xn1*j+ k*(dx*yn1-dy*xn1)
-      iqxyz = 1.0 / Math.sqrt( dx*dx+dy*dy+dz*dz)
-      xn2 = -dz * iqxyz * yn1
-      yn2 =  dz * iqxyz * xn1
+      xn2 = (dy*zn1-dz*yn1) * iqxyz
+      yn2 = (dz*xn1-dx*zn1) * iqxyz
       zn2 = (dx*yn1-dy*xn1) * iqxyz
 
       vindex = curV / 3 | 0

      
    r = @tubeRadius

    #x0 = xys[0][i]
    #y0 = xys[0][i]
    dz = @stepZ


    # x, y pairs of the tube cross-section
    @tubeShape = nGonVertices (options.tubeSides ? 3), (options.tubeRadius ? 0.1)

    # Normals from the previous call
    @prevNormals = new Float32Array @nCells*3
    for i in [0...@nCells*3] by 3
      @prevNormals[i] = 1
      @prevNormals[i+1] = 0
      @prevNormals[i+2] = 0
    return
    
  #to make chunk of N segments, we need n+3 states.
  # before-first, first, ... , last, after-last.
