Checks whether two cubic curves intersect. - Java java.lang

Java examples for java.lang:Math Curve

Description

Checks whether two cubic curves intersect.

Demo Code



public class Main{
    public static final float EPSILON = FloatMath.pow(10, -14);
    /**/*from w ww  .ja  v a2s.  c  om*/
     * Checks whether two cubic curves (x1, y1) - (x2, y2) - (x3, y3) - (x4, y4) and (cx1, cy1) -
     * (cx2, cy2) - (cx3, cy3) - (cx4, cy4) intersect. The result is saved to {@code params}. Thus
     * {@code params} must be of length at least 6.
     *
     * @return the number of intersection points that lie in the interval.
     */
    public static int intersectCubics(float x1, float y1, float x2,
            float y2, float x3, float y3, float x4, float y4, float cx1,
            float cy1, float cx2, float cy2, float cx3, float cy3,
            float cx4, float cy4, float[] params) {
        int quantity = 0;
        float[] initParams = new float[3];
        float[] xCoefs1 = new float[4];
        float[] yCoefs1 = new float[4];
        float[] xCoefs2 = new float[4];
        float[] yCoefs2 = new float[4];
        xCoefs1[0] = -x1 + 3 * x2 - 3 * x3 + x4;
        xCoefs1[1] = 3 * x1 - 6 * x2 + 3 * x3;
        xCoefs1[2] = -3 * x1 + 3 * x2;
        xCoefs1[3] = x1;

        yCoefs1[0] = -y1 + 3 * y2 - 3 * y3 + y4;
        yCoefs1[1] = 3 * y1 - 6 * y2 + 3 * y3;
        yCoefs1[2] = -3 * y1 + 3 * y2;
        yCoefs1[3] = y1;

        xCoefs2[0] = -cx1 + 3 * cx2 - 3 * cx3 + cx4;
        xCoefs2[1] = 3 * cx1 - 6 * cx2 + 3 * cx3;
        xCoefs2[2] = -3 * cx1 + 3 * cx2;
        xCoefs2[3] = cx1;

        yCoefs2[0] = -cy1 + 3 * cy2 - 3 * cy3 + cy4;
        yCoefs2[1] = 3 * cy1 - 6 * cy2 + 3 * cy3;
        yCoefs2[2] = -3 * cy1 + 3 * cy2;
        yCoefs2[3] = cy1;

        // TODO
        params[0] = params[1] = 0.25f;
        cubicNewton(xCoefs1, yCoefs1, xCoefs2, yCoefs2, initParams);
        if (initParams[0] <= 1 && initParams[0] >= 0 && initParams[1] >= 0
                && initParams[1] <= 1) {
            params[2 * quantity] = initParams[0];
            params[2 * quantity + 1] = initParams[1];
            ++quantity;
        }

        params[0] = params[1] = 0.5f;
        cubicNewton(xCoefs1, yCoefs1, xCoefs2, yCoefs2, params);
        if (initParams[0] <= 1 && initParams[0] >= 0 && initParams[1] >= 0
                && initParams[1] <= 1) {
            params[2 * quantity] = initParams[0];
            params[2 * quantity + 1] = initParams[1];
            ++quantity;
        }

        params[0] = params[1] = 0.75f;
        cubicNewton(xCoefs1, yCoefs1, xCoefs2, yCoefs2, params);
        if (initParams[0] <= 1 && initParams[0] >= 0 && initParams[1] >= 0
                && initParams[1] <= 1) {
            params[2 * quantity] = initParams[0];
            params[2 * quantity + 1] = initParams[1];
            ++quantity;
        }
        return quantity;
    }
    private static void cubicNewton(float[] xCoefs1, float[] yCoefs1,
            float[] xCoefs2, float[] yCoefs2, float[] params) {
        float t = 0f, s = 0f;
        float t1 = params[0];
        float s1 = params[1];
        float d, dt, ds;

        while (Math.sqrt((t - t1) * (t - t1) + (s - s1) * (s - s1)) > EPSILON) {
            d = -(3 * t * t * xCoefs1[0] + 2 * t * xCoefs1[1] + xCoefs1[2])
                    * (3 * s * s * yCoefs2[0] + 2 * s * yCoefs2[1] + yCoefs2[2])
                    + (3 * t * t * yCoefs1[0] + 2 * t * yCoefs1[1] + yCoefs1[2])
                    * (3 * s * s * xCoefs2[0] + 2 * s * xCoefs2[1] + xCoefs2[2]);

            dt = (t * t * t * xCoefs1[0] + t * t * xCoefs1[1] + t
                    * xCoefs1[2] + xCoefs1[3] - s * s * s * xCoefs2[0] - s
                    * s * xCoefs2[1] - s * xCoefs2[2] - xCoefs2[3])
                    * (-3 * s * s * yCoefs2[0] - 2 * s * yCoefs2[1] - yCoefs2[2])
                    + (t * t * t * yCoefs1[0] + t * t * yCoefs1[1] + t
                            * yCoefs1[2] + yCoefs1[3] - s * s * s
                            * yCoefs2[0] - s * s * yCoefs2[1] - s
                            * yCoefs2[2] - yCoefs2[3])
                    * (3 * s * s * xCoefs2[0] + 2 * s * xCoefs2[1] + xCoefs2[2]);

            ds = (3 * t * t * xCoefs1[0] + 2 * t * xCoefs1[1] + xCoefs1[2])
                    * (t * t * t * yCoefs1[0] + t * t * yCoefs1[1] + t
                            * yCoefs1[2] + yCoefs1[3] - s * s * s
                            * yCoefs2[0] - s * s * yCoefs2[1] - s
                            * yCoefs2[2] - yCoefs2[3])
                    - (3 * t * t * yCoefs1[0] + 2 * t * yCoefs1[1] + yCoefs1[2])
                    * (t * t * t * xCoefs1[0] + t * t * xCoefs1[1] + t
                            * xCoefs1[2] + xCoefs1[3] - s * s * s
                            * xCoefs2[0] - s * s * xCoefs2[1] - s
                            * xCoefs2[2] - xCoefs2[3]);

            t1 = t - dt / d;
            s1 = s - ds / d;
        }
        params[0] = t1;
        params[1] = s1;
    }
}

Related Tutorials