javacatmull-rom-curve

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


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<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);
    }```

Solution

  • Try changing "alpha" to 0.0f