Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function B(t) giving point at ratio 't' along curve.
- 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.
- var distances = [];
- var accumulated = 0;
- var currentPoint = B(0);
- for (i in 1...N+1) {
- var nextPoint = B(i/N);
- var distance = ... // some approximation to distance along curve between
- // B((i-1)/N) and B(i/N) . If N is large, even straight-line distance
- // would be fine.
- distances.push(accumulated += distance);
- }
- We now have a table, that gives us the distance along the curve at ratios 1/N, 2/N, 3/N ... 1
- We want to generate point B(x) parameterised by distance x instead of ratio t.
- We need to lookup the ratio 't' corresponding to an 'x' using our table.
- // disatnces = [10, 20, 30];
- // x = 5 -> i = 0
- // x = 15 -> i = 1
- function ratioAtDistance(x) {
- if (x < 0) throw "Cannot eval before curve";
- var i = 0;
- while (i < distances.length && distances[i] < x) i++;
- if (i >= distances.length) throw "Cannot eval after curve";
- var pDist = (i == 0) ? 0 : distances[i-1];
- return (i/N) + (1/N)*(x - distances[i])/(distances[i] - pDist);
- }
- We can then find point along curve at 'distance' x as B(ratioAtDistance(x))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement