三次贝塞尔曲线弧长始终为零
Cubic bezier curve arc length is always zero
我编写了以下代码来计算三次贝塞尔曲线的长度。我从 Calculate the arclength, curve length of a cubic bezier curve. Why is not working? 那里得到了灵感。问题是它总是产生零长度。
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;
使得 integer division, 所以结果 t
为零
还要注意 j = j + t
在每个循环 之后 执行,所以在第一次迭代时 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
间隔
我编写了以下代码来计算三次贝塞尔曲线的长度。我从 Calculate the arclength, curve length of a cubic bezier curve. Why is not working? 那里得到了灵感。问题是它总是产生零长度。
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;
使得 integer division, 所以结果 t
为零
还要注意 j = j + t
在每个循环 之后 执行,所以在第一次迭代时 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
间隔