Last active
October 7, 2021 21:52
-
-
Save jeremyabel/ce6040ea999218c6517c44eb658aff9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var order, pathPoints, knots; // the regular stuff goes in these | |
var nSegments = 500; | |
var segments = [0], p0; | |
for (var i = 0; i <= nSegments; i++) { | |
var t = i / nSegments; | |
var p1 = bSpline(t, order, pathPoints, knots); | |
if (p0) { | |
var dx = p0[0] - p1[0]; | |
var dy = p0[1] - p1[1]; | |
var d = Math.sqrt(dx * dx + dy * dy); | |
segments.push(segments[segments.length - 1] + d); | |
} else { | |
p0 = [0, 0]; | |
} | |
p0[0] = p1[0]; | |
p0[1] = p1[1]; | |
} | |
var bSplinePoints = []; | |
for (var i = 0; i < nSegments; i++) { | |
var u = i / a.length; | |
var t = 0; | |
// From https://gist.github.com/DUznanski/ebdca354df614e78b4cd69fb13e138c3 | |
// Also see: http://davis.wpi.edu/~matt/courses/biomed/reparam.htm | |
if (u == 1) { | |
t = 1; | |
} else { | |
// Convert u from 0..1 to the space given by the curve. The last index of the array | |
// corresponds to the arc length of the curve | |
var segmentCount = segments.length - 1; | |
var d = u * segments[segmentCount]; | |
var k = 0; | |
// Find the place in the segment distance list where d is | |
while (segments[k] < d) { | |
k++; | |
} | |
// We overshoot, so go back one | |
k = Math.max(0, --k); | |
var lo = segments[k]; | |
var hi = segments[k + 1]; | |
// Find out how far we've gone along the segment defined by the current and previous points | |
var segmentIndex = (d - lo) / (hi - lo); | |
var totalSegmentsPassed = k + segmentIndex; | |
t = totalSegmentsPassed / segmentCount; | |
} | |
bSplinePoints.push(t, order, pathPoints, knots); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment