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

Java examples for 2D Graphics:Curve

Description

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

Demo Code

/*//from w  w w.j  av a  2s.  c  o m
 * 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.Point2D;
import java.awt.geom.QuadCurve2D;

public class Main {
    /**
     * Computes the length of the given quadratic 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 on the curve.
     * 
     * @param curve the curve
     * @param flatness the flatness
     * @return the length of the curve
     */
    static public double quadCurveLength(QuadCurve2D 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) {
            QuadCurve2D left = new QuadCurve2D.Double();
            QuadCurve2D right = new QuadCurve2D.Double();
            curve.subdivide(left, right);

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

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

Related Tutorials