Advertisement
Guest User

Untitled

a guest
Feb 14th, 2013
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. Function B(t) giving point at ratio 't' along curve.
  2.  
  3. We're going to subdivide ratios [0,1] into N uniform slices (larger N gives more accurate lookup) and evaluate the cumulative distance along the segments.
  4.  
  5. var distances = [];
  6. var accumulated = 0;
  7. var currentPoint = B(0);
  8. for (i in 1...N+1) {
  9. var nextPoint = B(i/N);
  10. var distance = ... // some approximation to distance along curve between
  11. // B((i-1)/N) and B(i/N) . If N is large, even straight-line distance
  12. // would be fine.
  13. distances.push(accumulated += distance);
  14. }
  15.  
  16. We now have a table, that gives us the distance along the curve at ratios 1/N, 2/N, 3/N ... 1
  17.  
  18. We want to generate point B(x) parameterised by distance x instead of ratio t.
  19. We need to lookup the ratio 't' corresponding to an 'x' using our table.
  20.  
  21. // disatnces = [10, 20, 30];
  22. // x = 5 -> i = 0
  23. // x = 15 -> i = 1
  24.  
  25. function ratioAtDistance(x) {
  26. if (x < 0) throw "Cannot eval before curve";
  27. var i = 0;
  28. while (i < distances.length && distances[i] < x) i++;
  29. if (i >= distances.length) throw "Cannot eval after curve";
  30.  
  31. var pDist = (i == 0) ? 0 : distances[i-1];
  32. return (i/N) + (1/N)*(x - distances[i])/(distances[i] - pDist);
  33. }
  34.  
  35. We can then find point along curve at 'distance' x as B(ratioAtDistance(x))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement