三次贝塞尔曲线的弧长始终为零 [英] Cubic bezier curve arc length is always zero
问题描述
我编写了以下代码来计算三次贝塞尔曲线的长度.我的想法来自计算三次贝塞尔曲线的弧长,曲线长度.为什么不起作用?.问题在于它总是产生零长度.
I've written the following code to calculate the length of a cubic bezier curve. I got the idea from Calculate the arclength, curve length of a cubic bezier curve. Why is not working?. The problem is it always produces a length of zero.
public Vector2 SegmentAtPoint(int segmentIndex, float t)
{
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
oneMinusT * oneMinusT * oneMinusT * points[segmentIndex * 3] +
3f * oneMinusT * oneMinusT * t * points[segmentIndex * 3 + 1] +
3f * oneMinusT * t * t * points[segmentIndex * 3 + 2] +
t * t * t * points[segmentIndex * 3 + 3];
}
public float SegmentLength(int segmentIndex) {
var steps = 10;
var t = 1 / steps;
var sumArc = 0.0f;
var j = 0.0f;
var a = new Vector2(0.0f, 0.0f);
var b = points[segmentIndex * 3];
var dX = 0.0f;
var dY = 0.0f;
var dS = 0.0f;
for (int i = 0; i < steps; j = j + t)
{
a = SegmentAtPoint(segmentIndex, j);
dX = a.x - b.x;
dY = a.y - b.y;
dS = Mathf.Sqrt((dX * dX) + (dY * dY));
sumArc = sumArc + dS;
b.x = a.x;
b.y = a.y;
i++;
}
return sumArc;
}
推荐答案
代码var t = 1 / steps;
使还请注意,j = j + t
在每个循环后之后执行,因此在第一次迭代时j==0
Also note that j = j + t
is executed after every loop, so at the first iteration j==0
此缺陷会导致这样的问题:在第一次迭代中,b
和a
都相等,因为j
仍然保持= 0.因此,您可以按以下间隔计算段长度:0-0, 0-0.1, 0.1-0.2...0.7-0.8,0.8-0.9
-忽略0.9-1.0
间隔
This flaw causes such problem: both b
and a
are equal at the first iteration because j
still remains =0. So you calculate segment lengths on intervals: 0-0, 0-0.1, 0.1-0.2...0.7-0.8,0.8-0.9
- ignoring 0.9-1.0
interval
这篇关于三次贝塞尔曲线的弧长始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!