Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++ port of this demo code: http://jsfiddle.net/MadLittleMods/thcstaay/
- float approximateArcLengthOfBezierCurve(size_t controlPointListSize, Vector2 *controlPointListPointer, int resolution, Vector2 (*pointOnCurveFunc)(size_t, Vector2*, float)) {
- // Resolution is the number of segments we use
- resolution = resolution ? resolution : 500;
- float resultantArcLength = 0;
- float arcLengthMap[resolution+1];
- Vector2 controlPointList[controlPointListSize];
- for(int i = 0; i < controlPointListSize; i++) {
- controlPointList[i] = controlPointListPointer[i];
- }
- Vector2 prevPoint = controlPointList[0];
- Vector2 nextPoint;
- int mapIndex = 0;
- for(int i = 0; i < resolution; i++) {
- float t = clamp(i*(1/resolution), 0, 1);
- nextPoint = (*pointOnCurveFunc)(controlPointListSize, controlPointList, t);
- resultantArcLength += distance(prevPoint, nextPoint);
- prevPoint = nextPoint;
- arcLengthMap[mapIndex] = {
- t: t,
- arcLength: resultantArcLength
- };
- mapIndex++;
- }
- // Last stretch to the endpoint
- nextPoint = controlPointList[controlPointList.length-1];
- resultantArcLength += distance(prevPoint, nextPoint);
- arcLengthMap[mapIndex] = {
- t: 1,
- arcLength: resultantArcLength
- };
- mapIndex++;
- return {
- arcLength: resultantArcLength,
- arcLengthMap: arcLengthMap
- };
- }
- float distance(Vector2 p0, Vector2 p1) {
- return sqrt(pow(p1.x-p0.x, 2) + pow(p1.y-p0.y, 2));
- }
- float clamp(float val, float minVal, float maxVal)
- {
- float tempMax = max(val, minVal);
- return min(tempMax, maxVal);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement