Computes the length of the given cubic curve using recursive subdivision. - Java 2D Graphics

Java examples for 2D Graphics:Curve

Description

Computes the length of the given cubic curve using recursive subdivision.

Demo Code

/*/*w  ww .j  a  v  a 2  s  .  com*/
 * 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;
import java.awt.geom.Point2D;

public class Main {
    /**
     * Computes the length of the given cubic curve using recursive subdivision.
     * Recursion continues until the subdivided curves have the given flatness.
     * Flatness is the maximum distance of a controlpoint from the 
     * line connecting the endpoints. 
     * 
     * @param curve the curve
     * @param flatness the flatness
     * @return the length of the curve
     */
    static public double cubicCurveLength(CubicCurve2D curve,
            double flatness) {
        if (null == curve) {
            throw new NullPointerException("curve must be non-null!");
        } else if (flatness <= 0.0) {
            throw new IllegalArgumentException(
                    "flatness must be greater than 0!");
        }

        if (curve.getFlatness() > flatness) {
            CubicCurve2D left = new CubicCurve2D.Double();
            CubicCurve2D right = new CubicCurve2D.Double();
            curve.subdivide(left, right);

            return cubicCurveLength(left, flatness)
                    + cubicCurveLength(right, flatness);
        }

        return Point2D.distance(curve.getX1(), curve.getY1(),
                curve.getX2(), curve.getY2());
    }
}

Related Tutorials