Created
December 30, 2011 17:54
-
-
Save fjolnir/1540797 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
static void bezier_getLineSegments(bezier_t curve, int count, vec3_t *outPoints, float *outLengths, float *outDeltas, float *outTotalLength) | |
{ | |
vec3_t points[count]; | |
float lengths[count]; | |
lengths[count-1] = 0.0f; | |
float deltas[count]; | |
float totalLength; | |
float t = 0.0f; | |
float interval = 1.0f/(float)(count-1); | |
for(int i = 0; i < count; ++i) { | |
points[i] = bezier_getPoint(curve, t); | |
deltas[i] = t; | |
if(i > 0) { | |
lengths[i-1] = vec3_dist(points[i], points[i-1]); | |
totalLength += lengths[i-1]; | |
} | |
t += interval; | |
} | |
if(outPoints) memcpy(outPoints, points, sizeof(points)); | |
if(outLengths) memcpy(outLengths, lengths, sizeof(lengths)); | |
if(outDeltas) memcpy(outDeltas, deltas, sizeof(deltas)); | |
if(outTotalLength) *outTotalLength = totalLength; | |
} | |
static vec3_t bezier_getPointUsingLineSegments(float t, int count, vec3_t *points, float *lengths, float *deltas, float totalLength) | |
{ | |
float desiredDistance = t * totalLength; | |
assert(desiredDistance >= 0.0f && desiredDistance <= totalLength); | |
float distanceTravelled = 0.0f; | |
for(int i = 0; i < count - 1; ++i) { | |
float nextDistanceTravelled = distanceTravelled + lengths[i]; | |
if(desiredDistance < nextDistanceTravelled) { | |
float interpolation = (desiredDistance - distanceTravelled) / lengths[i]; | |
vec3_t p1 = points[i]; | |
vec3_t p2 = points[i+1]; | |
vec3_t movementVector = vec3_scalarMul(vec3_sub(p2, p1), interpolation); | |
return vec3_add(p1, movementVector); | |
} | |
distanceTravelled = nextDistanceTravelled; | |
} | |
return points[count-1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment