我编写了以下代码来计算三次贝塞尔曲线的长度。我的想法来自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;
}
答案 0 :(得分:3)
代码public StreamedContent getPreviewData() {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() != PhaseId.RENDER_RESPONSE) {
String id = context.getExternalContext().getRequestParameterMap().get("id");
GalleryMediaContent mediaContent = previewImageResolver.get(id);
if (Objects.nonNull(mediaContent)) {
return mediaContent.getPreviewData();
}
}
return new DefaultStreamedContent();
}
使integer division,成为结果var t = 1 / steps;
为零
还要注意,t
是在每个循环后执行,因此在第一次迭代j = j + t
此缺陷会导致这样的问题:j==0
和b
在第一次迭代中是相等的,因为a
仍为= 0。因此,您可以按以下间隔计算段长度:j
-忽略0-0, 0-0.1, 0.1-0.2...0.7-0.8,0.8-0.9
间隔