计算三次贝塞尔曲线的弧长,曲线长度。为什么不工作? [英] Calculate the arclength, curve length of a cubic bezier curve. Why is not working?
问题描述
我正在使用此算法计算arclength(立方贝塞尔曲线的长度)
I'm calculating the arclength (length of a cubic bezier curve) with this algorithm
function getArcLength(path) {
var STEPS = 1000; // > precision
var t = 1 / STEPS;
var aX=0;
var aY=0;
var bX=0, bY=0;
var dX=0, dY=0;
var dS = 0;
var sumArc = 0;
var j = 0;
for (var i=0; i<STEPS; j = j + t) {
aX = bezierPoint(j, path[0], path[2], path[4], path[6]);
aY = bezierPoint(j, path[1], path[3], path[5], path[7]);
dX = aX - bX;
dY = aY - bY;
// deltaS. Pitagora
dS = Math.sqrt((dX * dX) + (dY * dY));
sumArc = sumArc + dS;
bX = aX;
bY = aY;
i++;
}
return sumArc;
}
但我得到的是类似于915.但是曲线是480而且没有更多。 (我肯定知道这是因为曲线几乎是一条线)
路径数组有这个值:
498 51 500 52 500 53 500 530
But what I get is something like 915. But the curve is 480 and no more. (I know for sure this because the curve is almost a line) The path array has this values: 498 51 500 52 500 53 500 530
bezierPoint函数是:
The bezierPoint function is:
function bezierPoint(t, o1, c1, c2, e1) {
var C1 = (e1 - (3.0 * c2) + (3.0 * c1) - o1);
var C2 = ((3.0 * c2) - (6.0 * c1) + (3.0 * o1));
var C3 = ((3.0 * c1) - (3.0 * o1));
var C4 = (o1);
return ((C1*t*t*t) + (C2*t*t) + (C3*t) + C4)
}
我做错了什么?
推荐答案
因为 bX
和 bY
被初始化为0,当i = 0时第一个段测量距离原点的距离到了路径的起点。这会增加额外的sqrt(498 ^ 2 + 51 ^ 2)长度。如果你初始化 bX = path [0]
和 bY = path [1]
,我认为它会起作用。
Because bX
and bY
are initialized to 0, the first segment when i = 0 measures the distance from the origin to the start of the path. This adds an extra sqrt(498^2+51^2) to the length. If you initialize bX = path[0]
and bY = path[1]
, I think it will work.
这篇关于计算三次贝塞尔曲线的弧长,曲线长度。为什么不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!