Checks whether two quads curve intersect. - Java java.lang

Java examples for java.lang:Math Curve

Description

Checks whether two quads curve intersect.

Demo Code



public class Main{
    public static final float EPSILON = FloatMath.pow(10, -14);
    /**//from  ww w . ja va 2 s  .  c om
     * Checks whether two quads (x1, y1) - (x2, y2) - (x3, y3) and (qx1, qy1) - (qx2, qy2) - (qx3,
     * qy3) intersect. The result is saved to {@code params}. Thus {@code params} must be of length
     * at least 4.
     *
     * @return the number of roots that lie in the interval.
     */
    public static int intersectQuads(float x1, float y1, float x2,
            float y2, float x3, float y3, float qx1, float qy1, float qx2,
            float qy2, float qx3, float qy3, float[] params) {
        float[] initParams = new float[2];
        float[] xCoefs1 = new float[3];
        float[] yCoefs1 = new float[3];
        float[] xCoefs2 = new float[3];
        float[] yCoefs2 = new float[3];
        int quantity = 0;

        xCoefs1[0] = x1 - 2 * x2 + x3;
        xCoefs1[1] = -2 * x1 + 2 * x2;
        xCoefs1[2] = x1;

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

        xCoefs2[0] = qx1 - 2 * qx2 + qx3;
        xCoefs2[1] = -2 * qx1 + 2 * qx2;
        xCoefs2[2] = qx1;

        yCoefs2[0] = qy1 - 2 * qy2 + qy3;
        yCoefs2[1] = -2 * qy1 + 2 * qy2;
        yCoefs2[2] = qy1;

        // initialize params[0] and params[1]
        params[0] = params[1] = 0.25f;
        quadNewton(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;
        }
        // initialize params
        params[0] = params[1] = 0.75f;
        quadNewton(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 quadNewton(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) {
            t = t1;
            s = s1;
            d = -(2 * t * xCoefs1[0] + xCoefs1[1])
                    * (2 * s * yCoefs2[0] + yCoefs2[1])
                    + (2 * s * xCoefs2[0] + xCoefs2[1])
                    * (2 * t * yCoefs1[0] + yCoefs1[1]);

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

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

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

Related Tutorials