在使用Catmull-Rom样条时,同一坐标点重复使用会导致问题。

huangapple go评论139阅读模式
英文:

Problems when using same coordinate twice in a Catmull-Rom Spline

问题

我目前正在研究Catmull-Rom样条曲线,并发现了一个我不确定如何解决的问题。当我使用一种方法来执行Catmull操作时,我必须提供一个包含4个点的ArrayList。但在某些情况下,我并不总是有这4个点,有时只有2个或3个。我想在有2个点的情况下,可以将这两个点重复添加两次以创建一条直线。但每次我这样做时,都没有得到任何东西。我尝试过调试,得到了输出"NaN"。你们中有谁知道我可以做什么吗?以下是我用于公式的代码:

private ArrayList<Vector> catmulRom(ArrayList<Vector> points) {
    ArrayList<Vector> newpoints = new ArrayList<>();
    Vector p0 = points.get(0);
    Vector p1 = points.get(1);
    Vector p2 = points.get(2);
    Vector p3 = points.get(3);

    double t0 = 0.0f;
    double t1 = getT(t0, p0, p1);
    double t2 = getT(t1, p1, p2);
    double t3 = getT(t2, p2, p3);

    for (double t = t1; t < t2; t += ((t2 - t1) / (float) numberOfPoints)) {
        Vector a1 = p0.clone().multiply((t1 - t) / (t1 - t0)).add(p1.clone().multiply((t - t0) / (t1 - t0)));
        Vector a2 = p1.clone().multiply((t2 - t) / (t2 - t1)).add(p2.clone().multiply((t - t1) / (t2 - t1)));
        Vector a3 = p2.clone().multiply((t3 - t) / (t3 - t2)).add(p3.clone().multiply((t - t2) / (t3 - t2)));

        Vector b1 = a1.clone().multiply((t2 - t) / (t2 - t0)).add(a2.clone().multiply((t - t0) / (t2 - t0)));
        Vector b2 = a2.clone().multiply((t3 - t) / (t3 - t1)).add(a3.clone().multiply((t - t1) / (t3 - t1)));

        Vector c = b1.clone().multiply((t2 - t) / (t2 - t1)).add(b2.clone().multiply((t - t1) / (t2 - t1)));

        newpoints.add(c);
    }
    return newpoints;
}

int numberOfPoints = 10;
public double alpha = 0.5f;
public double getT(double t, Vector p0, Vector p1) {
    double a = Math.pow((p1.getX() - p0.getX()), 2.0f) + Math.pow((p1.getY() - p0.getY()), 2.0f);
    double b = Math.pow(a, alpha * 0.5f);
    return (b + t);
}
英文:

I'm currently messing around with Catmull-Rom splines, and have found a problem that I'm not sure how to solve. So when I use a method to do the Catmull stuff, I have to give an ArrayList of 4 points. But in some instances, I do not always have those 4 points and sometimes have just 2 or 3. I thought I could (in the instance of 2 points) that I could just add both of the points twice to create a straight line. Whenever I do this, I didn't get anything. I tried debugging, and I got the output "NaN". Does any of you know what I could do? Here's the code I use for the formula:

        ArrayList&lt;Vector&gt; newpoints = new ArrayList&lt;&gt;();
Vector p0 = points.get(0);
Vector p1 = points.get(1);
Vector p2 = points.get(2);
Vector p3 = points.get(3);
double t0 = 0.0f;
double t1 = getT(t0, p0, p1);
double t2 = getT(t1, p1, p2);
double t3 = getT(t2, p2, p3);
for (double t=t1; t&lt;t2; t+=((t2-t1)/(float)numberOfPoints))
{
Vector a1 = p0.clone().multiply((t1-t)/(t1-t0)).add(p1.clone().multiply((t-t0)/(t1-t0)));
Vector a2 = p1.clone().multiply((t2-t)/(t2-t1)).add(p2.clone().multiply((t-t1)/(t2-t1)));
Vector a3 = p2.clone().multiply((t3-t)/(t3-t2)).add(p3.clone().multiply((t-t2)/(t3-t2)));
Vector b1 = a1.clone().multiply((t2-t)/(t2-t0)).add(a2.clone().multiply((t-t0)/(t2-t0)));
Vector b2 = a2.clone().multiply((t3-t)/(t3-t1)).add(a3.clone().multiply((t-t1)/(t3-t1)));
Vector c = b1.clone().multiply((t2-t)/(t2-t1)).add(b2.clone().multiply((t-t1)/(t2-t1)));
newpoints.add(c);
}
return newpoints;
}
int numberOfPoints = 10;
public double alpha = 0.5f;
public double getT(double t, Vector p0, Vector p1) {
double a = Math.pow((p1.getX()-p0.getX()), 2.0f) + Math.pow((p1.getY()-p0.getY()), 2.0f);
double b = Math.pow(a, alpha * 0.5f);
return (b + t);
}```
</details>
# 答案1
**得分**: 1
尝试将 "alpha" 更改为 0.0f
<details>
<summary>英文:</summary>
Try changing &quot;alpha&quot; to 0.0f
</details>

huangapple
  • 本文由 发表于 2020年8月12日 21:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63377220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定