delta.js 744 B

123456789101112131415161718192021222324252627282930
  1. // Given an array of arcs in absolute (but already quantized!) coordinates,
  2. // converts to fixed-point delta encoding.
  3. // This is a destructive operation that modifies the given arcs!
  4. export default function(arcs) {
  5. var i = -1,
  6. n = arcs.length;
  7. while (++i < n) {
  8. var arc = arcs[i],
  9. j = 0,
  10. k = 1,
  11. m = arc.length,
  12. point = arc[0],
  13. x0 = point[0],
  14. y0 = point[1],
  15. x1,
  16. y1;
  17. while (++j < m) {
  18. point = arc[j], x1 = point[0], y1 = point[1];
  19. if (x1 !== x0 || y1 !== y0) arc[k++] = [x1 - x0, y1 - y0], x0 = x1, y0 = y1;
  20. }
  21. if (k === 1) arc[k++] = [0, 0]; // Each arc must be an array of two or more positions.
  22. arc.length = k;
  23. }
  24. return arcs;
  25. }