title: understand inkscape bezier flattening algorithm
body: <p>Hello I'm working translating svg to hpgl format. and the plotter i use understand only the first version of hpgl that doesn't understand bezier curves. so i'm trying to convert the inkscape function that flattens the curves to line segment from python to javascript.
  There are 4 files that i'm reading and i'm kind of lost in all the math stuff.
  in the <a href="https://gitlab.com/inkscape/inkscape/blob/master/share/extensions/hpgl_encoder.py" rel="nofollow noreferrer">hpgle_encode.py</a> there is a call to fucntion named <a href="https://gitlab.com/inkscape/inkscape/blob/master/share/extensions/cspsubdiv.py" rel="nofollow noreferrer">cspsubdiv</a> i think it stands for cubic super path sub division and from what i understood it is a recursive call that split every bezier cuvre in two (with De Casteljau's Algorithm) until the number of polyline is flat enough. this is determined by the distanceToPoint found in the <a href="https://gitlab.com/inkscape/inkscape/blob/master/share/extensions/ffgeom.py" rel="nofollow noreferrer">ffgeom</a>. What i don't understand is what c1 and c2 stand for and what mathematical concept are behind distance calculation of distance. also i don't understand the recursive part of the subdiv function. i could't debug the inkscape extension so i couldn't see what sp [i] represented in:</p>
  
  <pre><code>def subdiv(sp,flat,i=1):
      while i &lt; len(sp):
          p0 = sp[i-1][1]
          p1 = sp[i-1][2]
          p2 = sp[i][0]
          p3 = sp[i][1]
  
          b = (p0,p1,p2,p3)
          m = maxdist(b)
          if m &lt;= flat:
              i += 1
          else:
              one, two = beziersplitatt(b,0.5)
              sp[i-1][2] = one[1]
              sp[i][0] = two[2]
              p = [one[2],one[3],two[1]]
              sp[i:1] = [p]
  </code></pre>