methods for calculate bezier length
in
Programming Questions
•
1 year ago
I came up with this, seems to work fine.
I'm just wondering are there any other methods?
- void setup() {
- size(400, 400);
- noFill();
- bezier(255, 60, 30, 30, 270, 270, 45, 240);
- println(bezierLength(255, 60, 30, 30, 270, 270, 45, 240, 0.01));
- }
- float bezierLength(float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2, float precision) {
- if (precision <= 0 || precision >= 1) return -1;
- float l = 0;
- float i = 0;
- while (i+precision <= 1) {
- float xPos1 = bezierPoint(x1, cx1, cx2, x2, i);
- float xPos2 = bezierPoint(x1, cx1, cx2, x2, i+precision);
- float yPos1 = bezierPoint(y1, cy1, cy2, y2, i);
- float yPos2 = bezierPoint(y1, cy1, cy2, y2, i+precision);
- l += dist(xPos1, yPos1, xPos2, yPos2);
- println(i+precision);
- i += precision;
- }
- return l;
- }
1