Solves the cubic curve giving parameterized t values at points where the curve has an y value matching the given value. - Java 2D Graphics

Java examples for 2D Graphics:Curve

Description

Solves the cubic curve giving parameterized t values at points where the curve has an y value matching the given value.

Demo Code

/*//from  w  ww  .  j  a  va 2  s . c om
 * Copyright (C) 2005 Jordan Kiang
 * jordan-at-kiang.org
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
//package com.java2s;
import java.awt.geom.CubicCurve2D;

public class Main {
    /**
     * Solves the cubic curve giving parameterized t values at
     * points where the curve has an y value matching the given value.
     * Writes as many solutions into the given array as will fit. 
     * 
     * @param curve the curve
     * @param y the value of y to solve for
     * @param solutions an array to write solutions into
     * @return the number of solutions
     */
    static public int solveCubicCurveForY(CubicCurve2D curve, double y,
            double[] solutions) {
        double a = getCubicAy(curve);
        double b = getCubicBy(curve);
        double c = getCubicCy(curve);
        double d = curve.getY1() - y;

        double[] eqn = new double[] { d, c, b, a };

        int rootCount = CubicCurve2D.solveCubic(eqn);
        return copyValidSolutions(rootCount, eqn, solutions);
    }

    static private double getCubicAy(CubicCurve2D curve) {
        return curve.getY2() - curve.getY1() - getCubicBy(curve)
                - getCubicCy(curve);
    }

    static private double getCubicBy(CubicCurve2D curve) {
        return 3.0 * (curve.getCtrlY2() - curve.getCtrlY1())
                - getCubicCy(curve);
    }

    static private double getCubicCy(CubicCurve2D curve) {
        return 3.0 * (curve.getCtrlY1() - curve.getY1());
    }

    static private int copyValidSolutions(int rootCount, double[] roots,
            double[] solutions) {
        int solutionCount = 0;

        for (int i = 0; i < rootCount; i++) {
            if (roots[i] >= 0 && roots[i] <= 1.0) {

                // The solve functions can give multiple roots.
                // We only want unique roots, so we check the next solution against the previous roots.
                boolean unique = true;
                for (int j = 0; j < solutionCount; j++) {
                    if (roots[i] == roots[j]) {
                        unique = false;
                        break;
                    }
                }

                if (unique) {
                    // If the given solution array is too small, then just write as many as will fit.
                    if (solutionCount < solutions.length) {
                        solutions[solutionCount] = roots[i];
                    }

                    solutionCount++;
                }
            }
        }

        return solutionCount;
    }
}

Related Tutorials