0N/A/*
3909N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.geom;
0N/A
0N/Aimport java.awt.Shape;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.util.Arrays;
0N/Aimport java.io.Serializable;
0N/Aimport sun.awt.geom.Curve;
0N/A
3442N/Aimport static java.lang.Math.abs;
3442N/Aimport static java.lang.Math.max;
3442N/Aimport static java.lang.Math.ulp;
3442N/A
0N/A/**
0N/A * The <code>CubicCurve2D</code> class defines a cubic parametric curve
0N/A * segment in {@code (x,y)} coordinate space.
0N/A * <p>
0N/A * This class is only the abstract superclass for all objects which
0N/A * store a 2D cubic curve segment.
0N/A * The actual storage representation of the coordinates is left to
0N/A * the subclass.
0N/A *
0N/A * @author Jim Graham
0N/A * @since 1.2
0N/A */
0N/Apublic abstract class CubicCurve2D implements Shape, Cloneable {
0N/A
0N/A /**
0N/A * A cubic parametric curve segment specified with
0N/A * {@code float} coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static class Float extends CubicCurve2D implements Serializable {
0N/A /**
0N/A * The X coordinate of the start point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float x1;
0N/A
0N/A /**
0N/A * The Y coordinate of the start point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float y1;
0N/A
0N/A /**
0N/A * The X coordinate of the first control point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float ctrlx1;
0N/A
0N/A /**
0N/A * The Y coordinate of the first control point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float ctrly1;
0N/A
0N/A /**
0N/A * The X coordinate of the second control point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float ctrlx2;
0N/A
0N/A /**
0N/A * The Y coordinate of the second control point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float ctrly2;
0N/A
0N/A /**
0N/A * The X coordinate of the end point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float x2;
0N/A
0N/A /**
0N/A * The Y coordinate of the end point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float y2;
0N/A
0N/A /**
0N/A * Constructs and initializes a CubicCurve with coordinates
0N/A * (0, 0, 0, 0, 0, 0, 0, 0).
0N/A * @since 1.2
0N/A */
0N/A public Float() {
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes a {@code CubicCurve2D} from
0N/A * the specified {@code float} coordinates.
0N/A *
0N/A * @param x1 the X coordinate for the start point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param y1 the Y coordinate for the start point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param ctrlx1 the X coordinate for the first control point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param ctrly1 the Y coordinate for the first control point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param ctrlx2 the X coordinate for the second control point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param ctrly2 the Y coordinate for the second control point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param x2 the X coordinate for the end point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param y2 the Y coordinate for the end point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @since 1.2
0N/A */
0N/A public Float(float x1, float y1,
0N/A float ctrlx1, float ctrly1,
0N/A float ctrlx2, float ctrly2,
0N/A float x2, float y2)
0N/A {
0N/A setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX1() {
0N/A return (double) x1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY1() {
0N/A return (double) y1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getP1() {
0N/A return new Point2D.Float(x1, y1);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getCtrlX1() {
0N/A return (double) ctrlx1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getCtrlY1() {
0N/A return (double) ctrly1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getCtrlP1() {
0N/A return new Point2D.Float(ctrlx1, ctrly1);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getCtrlX2() {
0N/A return (double) ctrlx2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getCtrlY2() {
0N/A return (double) ctrly2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getCtrlP2() {
0N/A return new Point2D.Float(ctrlx2, ctrly2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX2() {
0N/A return (double) x2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY2() {
0N/A return (double) y2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getP2() {
0N/A return new Point2D.Float(x2, y2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setCurve(double x1, double y1,
0N/A double ctrlx1, double ctrly1,
0N/A double ctrlx2, double ctrly2,
0N/A double x2, double y2)
0N/A {
0N/A this.x1 = (float) x1;
0N/A this.y1 = (float) y1;
0N/A this.ctrlx1 = (float) ctrlx1;
0N/A this.ctrly1 = (float) ctrly1;
0N/A this.ctrlx2 = (float) ctrlx2;
0N/A this.ctrly2 = (float) ctrly2;
0N/A this.x2 = (float) x2;
0N/A this.y2 = (float) y2;
0N/A }
0N/A
0N/A /**
0N/A * Sets the location of the end points and control points
0N/A * of this curve to the specified {@code float} coordinates.
0N/A *
0N/A * @param x1 the X coordinate used to set the start point
0N/A * of this {@code CubicCurve2D}
0N/A * @param y1 the Y coordinate used to set the start point
0N/A * of this {@code CubicCurve2D}
0N/A * @param ctrlx1 the X coordinate used to set the first control point
0N/A * of this {@code CubicCurve2D}
0N/A * @param ctrly1 the Y coordinate used to set the first control point
0N/A * of this {@code CubicCurve2D}
0N/A * @param ctrlx2 the X coordinate used to set the second control point
0N/A * of this {@code CubicCurve2D}
0N/A * @param ctrly2 the Y coordinate used to set the second control point
0N/A * of this {@code CubicCurve2D}
0N/A * @param x2 the X coordinate used to set the end point
0N/A * of this {@code CubicCurve2D}
0N/A * @param y2 the Y coordinate used to set the end point
0N/A * of this {@code CubicCurve2D}
0N/A * @since 1.2
0N/A */
0N/A public void setCurve(float x1, float y1,
0N/A float ctrlx1, float ctrly1,
0N/A float ctrlx2, float ctrly2,
0N/A float x2, float y2)
0N/A {
0N/A this.x1 = x1;
0N/A this.y1 = y1;
0N/A this.ctrlx1 = ctrlx1;
0N/A this.ctrly1 = ctrly1;
0N/A this.ctrlx2 = ctrlx2;
0N/A this.ctrly2 = ctrly2;
0N/A this.x2 = x2;
0N/A this.y2 = y2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D getBounds2D() {
0N/A float left = Math.min(Math.min(x1, x2),
0N/A Math.min(ctrlx1, ctrlx2));
0N/A float top = Math.min(Math.min(y1, y2),
0N/A Math.min(ctrly1, ctrly2));
0N/A float right = Math.max(Math.max(x1, x2),
0N/A Math.max(ctrlx1, ctrlx2));
0N/A float bottom = Math.max(Math.max(y1, y2),
0N/A Math.max(ctrly1, ctrly2));
0N/A return new Rectangle2D.Float(left, top,
0N/A right - left, bottom - top);
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -1272015596714244385L;
0N/A }
0N/A
0N/A /**
0N/A * A cubic parametric curve segment specified with
0N/A * {@code double} coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static class Double extends CubicCurve2D implements Serializable {
0N/A /**
0N/A * The X coordinate of the start point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double x1;
0N/A
0N/A /**
0N/A * The Y coordinate of the start point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double y1;
0N/A
0N/A /**
0N/A * The X coordinate of the first control point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double ctrlx1;
0N/A
0N/A /**
0N/A * The Y coordinate of the first control point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double ctrly1;
0N/A
0N/A /**
0N/A * The X coordinate of the second control point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double ctrlx2;
0N/A
0N/A /**
0N/A * The Y coordinate of the second control point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double ctrly2;
0N/A
0N/A /**
0N/A * The X coordinate of the end point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double x2;
0N/A
0N/A /**
0N/A * The Y coordinate of the end point
0N/A * of the cubic curve segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double y2;
0N/A
0N/A /**
0N/A * Constructs and initializes a CubicCurve with coordinates
0N/A * (0, 0, 0, 0, 0, 0, 0, 0).
0N/A * @since 1.2
0N/A */
0N/A public Double() {
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes a {@code CubicCurve2D} from
0N/A * the specified {@code double} coordinates.
0N/A *
0N/A * @param x1 the X coordinate for the start point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param y1 the Y coordinate for the start point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param ctrlx1 the X coordinate for the first control point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param ctrly1 the Y coordinate for the first control point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param ctrlx2 the X coordinate for the second control point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param ctrly2 the Y coordinate for the second control point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param x2 the X coordinate for the end point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @param y2 the Y coordinate for the end point
0N/A * of the resulting {@code CubicCurve2D}
0N/A * @since 1.2
0N/A */
0N/A public Double(double x1, double y1,
0N/A double ctrlx1, double ctrly1,
0N/A double ctrlx2, double ctrly2,
0N/A double x2, double y2)
0N/A {
0N/A setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX1() {
0N/A return x1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY1() {
0N/A return y1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getP1() {
0N/A return new Point2D.Double(x1, y1);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getCtrlX1() {
0N/A return ctrlx1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getCtrlY1() {
0N/A return ctrly1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getCtrlP1() {
0N/A return new Point2D.Double(ctrlx1, ctrly1);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getCtrlX2() {
0N/A return ctrlx2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getCtrlY2() {
0N/A return ctrly2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getCtrlP2() {
0N/A return new Point2D.Double(ctrlx2, ctrly2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX2() {
0N/A return x2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY2() {
0N/A return y2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getP2() {
0N/A return new Point2D.Double(x2, y2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setCurve(double x1, double y1,
0N/A double ctrlx1, double ctrly1,
0N/A double ctrlx2, double ctrly2,
0N/A double x2, double y2)
0N/A {
0N/A this.x1 = x1;
0N/A this.y1 = y1;
0N/A this.ctrlx1 = ctrlx1;
0N/A this.ctrly1 = ctrly1;
0N/A this.ctrlx2 = ctrlx2;
0N/A this.ctrly2 = ctrly2;
0N/A this.x2 = x2;
0N/A this.y2 = y2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D getBounds2D() {
0N/A double left = Math.min(Math.min(x1, x2),
0N/A Math.min(ctrlx1, ctrlx2));
0N/A double top = Math.min(Math.min(y1, y2),
0N/A Math.min(ctrly1, ctrly2));
0N/A double right = Math.max(Math.max(x1, x2),
0N/A Math.max(ctrlx1, ctrlx2));
0N/A double bottom = Math.max(Math.max(y1, y2),
0N/A Math.max(ctrly1, ctrly2));
0N/A return new Rectangle2D.Double(left, top,
0N/A right - left, bottom - top);
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -4202960122839707295L;
0N/A }
0N/A
0N/A /**
0N/A * This is an abstract class that cannot be instantiated directly.
0N/A * Type-specific implementation subclasses are available for
0N/A * instantiation and provide a number of formats for storing
0N/A * the information necessary to satisfy the various accessor
0N/A * methods below.
0N/A *
0N/A * @see java.awt.geom.CubicCurve2D.Float
0N/A * @see java.awt.geom.CubicCurve2D.Double
0N/A * @since 1.2
0N/A */
0N/A protected CubicCurve2D() {
0N/A }
0N/A
0N/A /**
0N/A * Returns the X coordinate of the start point in double precision.
0N/A * @return the X coordinate of the start point of the
0N/A * {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getX1();
0N/A
0N/A /**
0N/A * Returns the Y coordinate of the start point in double precision.
0N/A * @return the Y coordinate of the start point of the
0N/A * {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getY1();
0N/A
0N/A /**
0N/A * Returns the start point.
0N/A * @return a {@code Point2D} that is the start point of
0N/A * the {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract Point2D getP1();
0N/A
0N/A /**
0N/A * Returns the X coordinate of the first control point in double precision.
0N/A * @return the X coordinate of the first control point of the
0N/A * {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getCtrlX1();
0N/A
0N/A /**
0N/A * Returns the Y coordinate of the first control point in double precision.
0N/A * @return the Y coordinate of the first control point of the
0N/A * {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getCtrlY1();
0N/A
0N/A /**
0N/A * Returns the first control point.
0N/A * @return a {@code Point2D} that is the first control point of
0N/A * the {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract Point2D getCtrlP1();
0N/A
0N/A /**
0N/A * Returns the X coordinate of the second control point
0N/A * in double precision.
0N/A * @return the X coordinate of the second control point of the
0N/A * {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getCtrlX2();
0N/A
0N/A /**
0N/A * Returns the Y coordinate of the second control point
0N/A * in double precision.
0N/A * @return the Y coordinate of the second control point of the
0N/A * {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getCtrlY2();
0N/A
0N/A /**
0N/A * Returns the second control point.
0N/A * @return a {@code Point2D} that is the second control point of
0N/A * the {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract Point2D getCtrlP2();
0N/A
0N/A /**
0N/A * Returns the X coordinate of the end point in double precision.
0N/A * @return the X coordinate of the end point of the
0N/A * {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getX2();
0N/A
0N/A /**
0N/A * Returns the Y coordinate of the end point in double precision.
0N/A * @return the Y coordinate of the end point of the
0N/A * {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getY2();
0N/A
0N/A /**
0N/A * Returns the end point.
0N/A * @return a {@code Point2D} that is the end point of
0N/A * the {@code CubicCurve2D}.
0N/A * @since 1.2
0N/A */
0N/A public abstract Point2D getP2();
0N/A
0N/A /**
0N/A * Sets the location of the end points and control points of this curve
0N/A * to the specified double coordinates.
0N/A *
0N/A * @param x1 the X coordinate used to set the start point
0N/A * of this {@code CubicCurve2D}
0N/A * @param y1 the Y coordinate used to set the start point
0N/A * of this {@code CubicCurve2D}
0N/A * @param ctrlx1 the X coordinate used to set the first control point
0N/A * of this {@code CubicCurve2D}
0N/A * @param ctrly1 the Y coordinate used to set the first control point
0N/A * of this {@code CubicCurve2D}
0N/A * @param ctrlx2 the X coordinate used to set the second control point
0N/A * of this {@code CubicCurve2D}
0N/A * @param ctrly2 the Y coordinate used to set the second control point
0N/A * of this {@code CubicCurve2D}
0N/A * @param x2 the X coordinate used to set the end point
0N/A * of this {@code CubicCurve2D}
0N/A * @param y2 the Y coordinate used to set the end point
0N/A * of this {@code CubicCurve2D}
0N/A * @since 1.2
0N/A */
0N/A public abstract void setCurve(double x1, double y1,
0N/A double ctrlx1, double ctrly1,
0N/A double ctrlx2, double ctrly2,
0N/A double x2, double y2);
0N/A
0N/A /**
0N/A * Sets the location of the end points and control points of this curve
0N/A * to the double coordinates at the specified offset in the specified
0N/A * array.
0N/A * @param coords a double array containing coordinates
0N/A * @param offset the index of <code>coords</code> from which to begin
0N/A * setting the end points and control points of this curve
0N/A * to the coordinates contained in <code>coords</code>
0N/A * @since 1.2
0N/A */
0N/A public void setCurve(double[] coords, int offset) {
0N/A setCurve(coords[offset + 0], coords[offset + 1],
0N/A coords[offset + 2], coords[offset + 3],
0N/A coords[offset + 4], coords[offset + 5],
0N/A coords[offset + 6], coords[offset + 7]);
0N/A }
0N/A
0N/A /**
0N/A * Sets the location of the end points and control points of this curve
0N/A * to the specified <code>Point2D</code> coordinates.
0N/A * @param p1 the first specified <code>Point2D</code> used to set the
0N/A * start point of this curve
0N/A * @param cp1 the second specified <code>Point2D</code> used to set the
0N/A * first control point of this curve
0N/A * @param cp2 the third specified <code>Point2D</code> used to set the
0N/A * second control point of this curve
0N/A * @param p2 the fourth specified <code>Point2D</code> used to set the
0N/A * end point of this curve
0N/A * @since 1.2
0N/A */
0N/A public void setCurve(Point2D p1, Point2D cp1, Point2D cp2, Point2D p2) {
0N/A setCurve(p1.getX(), p1.getY(), cp1.getX(), cp1.getY(),
0N/A cp2.getX(), cp2.getY(), p2.getX(), p2.getY());
0N/A }
0N/A
0N/A /**
0N/A * Sets the location of the end points and control points of this curve
0N/A * to the coordinates of the <code>Point2D</code> objects at the specified
0N/A * offset in the specified array.
0N/A * @param pts an array of <code>Point2D</code> objects
0N/A * @param offset the index of <code>pts</code> from which to begin setting
0N/A * the end points and control points of this curve to the
0N/A * points contained in <code>pts</code>
0N/A * @since 1.2
0N/A */
0N/A public void setCurve(Point2D[] pts, int offset) {
0N/A setCurve(pts[offset + 0].getX(), pts[offset + 0].getY(),
0N/A pts[offset + 1].getX(), pts[offset + 1].getY(),
0N/A pts[offset + 2].getX(), pts[offset + 2].getY(),
0N/A pts[offset + 3].getX(), pts[offset + 3].getY());
0N/A }
0N/A
0N/A /**
0N/A * Sets the location of the end points and control points of this curve
0N/A * to the same as those in the specified <code>CubicCurve2D</code>.
0N/A * @param c the specified <code>CubicCurve2D</code>
0N/A * @since 1.2
0N/A */
0N/A public void setCurve(CubicCurve2D c) {
0N/A setCurve(c.getX1(), c.getY1(), c.getCtrlX1(), c.getCtrlY1(),
0N/A c.getCtrlX2(), c.getCtrlY2(), c.getX2(), c.getY2());
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the flatness of the cubic curve specified
0N/A * by the indicated control points. The flatness is the maximum distance
0N/A * of a control point from the line connecting the end points.
0N/A *
0N/A * @param x1 the X coordinate that specifies the start point
0N/A * of a {@code CubicCurve2D}
0N/A * @param y1 the Y coordinate that specifies the start point
0N/A * of a {@code CubicCurve2D}
0N/A * @param ctrlx1 the X coordinate that specifies the first control point
0N/A * of a {@code CubicCurve2D}
0N/A * @param ctrly1 the Y coordinate that specifies the first control point
0N/A * of a {@code CubicCurve2D}
0N/A * @param ctrlx2 the X coordinate that specifies the second control point
0N/A * of a {@code CubicCurve2D}
0N/A * @param ctrly2 the Y coordinate that specifies the second control point
0N/A * of a {@code CubicCurve2D}
0N/A * @param x2 the X coordinate that specifies the end point
0N/A * of a {@code CubicCurve2D}
0N/A * @param y2 the Y coordinate that specifies the end point
0N/A * of a {@code CubicCurve2D}
0N/A * @return the square of the flatness of the {@code CubicCurve2D}
0N/A * represented by the specified coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static double getFlatnessSq(double x1, double y1,
0N/A double ctrlx1, double ctrly1,
0N/A double ctrlx2, double ctrly2,
0N/A double x2, double y2) {
0N/A return Math.max(Line2D.ptSegDistSq(x1, y1, x2, y2, ctrlx1, ctrly1),
0N/A Line2D.ptSegDistSq(x1, y1, x2, y2, ctrlx2, ctrly2));
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Returns the flatness of the cubic curve specified
0N/A * by the indicated control points. The flatness is the maximum distance
0N/A * of a control point from the line connecting the end points.
0N/A *
0N/A * @param x1 the X coordinate that specifies the start point
0N/A * of a {@code CubicCurve2D}
0N/A * @param y1 the Y coordinate that specifies the start point
0N/A * of a {@code CubicCurve2D}
0N/A * @param ctrlx1 the X coordinate that specifies the first control point
0N/A * of a {@code CubicCurve2D}
0N/A * @param ctrly1 the Y coordinate that specifies the first control point
0N/A * of a {@code CubicCurve2D}
0N/A * @param ctrlx2 the X coordinate that specifies the second control point
0N/A * of a {@code CubicCurve2D}
0N/A * @param ctrly2 the Y coordinate that specifies the second control point
0N/A * of a {@code CubicCurve2D}
0N/A * @param x2 the X coordinate that specifies the end point
0N/A * of a {@code CubicCurve2D}
0N/A * @param y2 the Y coordinate that specifies the end point
0N/A * of a {@code CubicCurve2D}
0N/A * @return the flatness of the {@code CubicCurve2D}
0N/A * represented by the specified coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static double getFlatness(double x1, double y1,
0N/A double ctrlx1, double ctrly1,
0N/A double ctrlx2, double ctrly2,
0N/A double x2, double y2) {
0N/A return Math.sqrt(getFlatnessSq(x1, y1, ctrlx1, ctrly1,
0N/A ctrlx2, ctrly2, x2, y2));
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the flatness of the cubic curve specified
0N/A * by the control points stored in the indicated array at the
0N/A * indicated index. The flatness is the maximum distance
0N/A * of a control point from the line connecting the end points.
0N/A * @param coords an array containing coordinates
0N/A * @param offset the index of <code>coords</code> from which to begin
0N/A * getting the end points and control points of the curve
0N/A * @return the square of the flatness of the <code>CubicCurve2D</code>
0N/A * specified by the coordinates in <code>coords</code> at
0N/A * the specified offset.
0N/A * @since 1.2
0N/A */
0N/A public static double getFlatnessSq(double coords[], int offset) {
0N/A return getFlatnessSq(coords[offset + 0], coords[offset + 1],
0N/A coords[offset + 2], coords[offset + 3],
0N/A coords[offset + 4], coords[offset + 5],
0N/A coords[offset + 6], coords[offset + 7]);
0N/A }
0N/A
0N/A /**
0N/A * Returns the flatness of the cubic curve specified
0N/A * by the control points stored in the indicated array at the
0N/A * indicated index. The flatness is the maximum distance
0N/A * of a control point from the line connecting the end points.
0N/A * @param coords an array containing coordinates
0N/A * @param offset the index of <code>coords</code> from which to begin
0N/A * getting the end points and control points of the curve
0N/A * @return the flatness of the <code>CubicCurve2D</code>
0N/A * specified by the coordinates in <code>coords</code> at
0N/A * the specified offset.
0N/A * @since 1.2
0N/A */
0N/A public static double getFlatness(double coords[], int offset) {
0N/A return getFlatness(coords[offset + 0], coords[offset + 1],
0N/A coords[offset + 2], coords[offset + 3],
0N/A coords[offset + 4], coords[offset + 5],
0N/A coords[offset + 6], coords[offset + 7]);
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the flatness of this curve. The flatness is the
0N/A * maximum distance of a control point from the line connecting the
0N/A * end points.
0N/A * @return the square of the flatness of this curve.
0N/A * @since 1.2
0N/A */
0N/A public double getFlatnessSq() {
0N/A return getFlatnessSq(getX1(), getY1(), getCtrlX1(), getCtrlY1(),
0N/A getCtrlX2(), getCtrlY2(), getX2(), getY2());
0N/A }
0N/A
0N/A /**
0N/A * Returns the flatness of this curve. The flatness is the
0N/A * maximum distance of a control point from the line connecting the
0N/A * end points.
0N/A * @return the flatness of this curve.
0N/A * @since 1.2
0N/A */
0N/A public double getFlatness() {
0N/A return getFlatness(getX1(), getY1(), getCtrlX1(), getCtrlY1(),
0N/A getCtrlX2(), getCtrlY2(), getX2(), getY2());
0N/A }
0N/A
0N/A /**
0N/A * Subdivides this cubic curve and stores the resulting two
0N/A * subdivided curves into the left and right curve parameters.
0N/A * Either or both of the left and right objects may be the same
0N/A * as this object or null.
0N/A * @param left the cubic curve object for storing for the left or
0N/A * first half of the subdivided curve
0N/A * @param right the cubic curve object for storing for the right or
0N/A * second half of the subdivided curve
0N/A * @since 1.2
0N/A */
0N/A public void subdivide(CubicCurve2D left, CubicCurve2D right) {
0N/A subdivide(this, left, right);
0N/A }
0N/A
0N/A /**
0N/A * Subdivides the cubic curve specified by the <code>src</code> parameter
0N/A * and stores the resulting two subdivided curves into the
0N/A * <code>left</code> and <code>right</code> curve parameters.
0N/A * Either or both of the <code>left</code> and <code>right</code> objects
0N/A * may be the same as the <code>src</code> object or <code>null</code>.
0N/A * @param src the cubic curve to be subdivided
0N/A * @param left the cubic curve object for storing the left or
0N/A * first half of the subdivided curve
0N/A * @param right the cubic curve object for storing the right or
0N/A * second half of the subdivided curve
0N/A * @since 1.2
0N/A */
0N/A public static void subdivide(CubicCurve2D src,
0N/A CubicCurve2D left,
0N/A CubicCurve2D right) {
0N/A double x1 = src.getX1();
0N/A double y1 = src.getY1();
0N/A double ctrlx1 = src.getCtrlX1();
0N/A double ctrly1 = src.getCtrlY1();
0N/A double ctrlx2 = src.getCtrlX2();
0N/A double ctrly2 = src.getCtrlY2();
0N/A double x2 = src.getX2();
0N/A double y2 = src.getY2();
0N/A double centerx = (ctrlx1 + ctrlx2) / 2.0;
0N/A double centery = (ctrly1 + ctrly2) / 2.0;
0N/A ctrlx1 = (x1 + ctrlx1) / 2.0;
0N/A ctrly1 = (y1 + ctrly1) / 2.0;
0N/A ctrlx2 = (x2 + ctrlx2) / 2.0;
0N/A ctrly2 = (y2 + ctrly2) / 2.0;
0N/A double ctrlx12 = (ctrlx1 + centerx) / 2.0;
0N/A double ctrly12 = (ctrly1 + centery) / 2.0;
0N/A double ctrlx21 = (ctrlx2 + centerx) / 2.0;
0N/A double ctrly21 = (ctrly2 + centery) / 2.0;
0N/A centerx = (ctrlx12 + ctrlx21) / 2.0;
0N/A centery = (ctrly12 + ctrly21) / 2.0;
0N/A if (left != null) {
0N/A left.setCurve(x1, y1, ctrlx1, ctrly1,
0N/A ctrlx12, ctrly12, centerx, centery);
0N/A }
0N/A if (right != null) {
0N/A right.setCurve(centerx, centery, ctrlx21, ctrly21,
0N/A ctrlx2, ctrly2, x2, y2);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Subdivides the cubic curve specified by the coordinates
0N/A * stored in the <code>src</code> array at indices <code>srcoff</code>
0N/A * through (<code>srcoff</code>&nbsp;+&nbsp;7) and stores the
0N/A * resulting two subdivided curves into the two result arrays at the
0N/A * corresponding indices.
0N/A * Either or both of the <code>left</code> and <code>right</code>
0N/A * arrays may be <code>null</code> or a reference to the same array
0N/A * as the <code>src</code> array.
0N/A * Note that the last point in the first subdivided curve is the
0N/A * same as the first point in the second subdivided curve. Thus,
0N/A * it is possible to pass the same array for <code>left</code>
0N/A * and <code>right</code> and to use offsets, such as <code>rightoff</code>
0N/A * equals (<code>leftoff</code> + 6), in order
0N/A * to avoid allocating extra storage for this common point.
0N/A * @param src the array holding the coordinates for the source curve
0N/A * @param srcoff the offset into the array of the beginning of the
0N/A * the 6 source coordinates
0N/A * @param left the array for storing the coordinates for the first
0N/A * half of the subdivided curve
0N/A * @param leftoff the offset into the array of the beginning of the
0N/A * the 6 left coordinates
0N/A * @param right the array for storing the coordinates for the second
0N/A * half of the subdivided curve
0N/A * @param rightoff the offset into the array of the beginning of the
0N/A * the 6 right coordinates
0N/A * @since 1.2
0N/A */
0N/A public static void subdivide(double src[], int srcoff,
0N/A double left[], int leftoff,
0N/A double right[], int rightoff) {
0N/A double x1 = src[srcoff + 0];
0N/A double y1 = src[srcoff + 1];
0N/A double ctrlx1 = src[srcoff + 2];
0N/A double ctrly1 = src[srcoff + 3];
0N/A double ctrlx2 = src[srcoff + 4];
0N/A double ctrly2 = src[srcoff + 5];
0N/A double x2 = src[srcoff + 6];
0N/A double y2 = src[srcoff + 7];
0N/A if (left != null) {
0N/A left[leftoff + 0] = x1;
0N/A left[leftoff + 1] = y1;
0N/A }
0N/A if (right != null) {
0N/A right[rightoff + 6] = x2;
0N/A right[rightoff + 7] = y2;
0N/A }
0N/A x1 = (x1 + ctrlx1) / 2.0;
0N/A y1 = (y1 + ctrly1) / 2.0;
0N/A x2 = (x2 + ctrlx2) / 2.0;
0N/A y2 = (y2 + ctrly2) / 2.0;
0N/A double centerx = (ctrlx1 + ctrlx2) / 2.0;
0N/A double centery = (ctrly1 + ctrly2) / 2.0;
0N/A ctrlx1 = (x1 + centerx) / 2.0;
0N/A ctrly1 = (y1 + centery) / 2.0;
0N/A ctrlx2 = (x2 + centerx) / 2.0;
0N/A ctrly2 = (y2 + centery) / 2.0;
0N/A centerx = (ctrlx1 + ctrlx2) / 2.0;
0N/A centery = (ctrly1 + ctrly2) / 2.0;
0N/A if (left != null) {
0N/A left[leftoff + 2] = x1;
0N/A left[leftoff + 3] = y1;
0N/A left[leftoff + 4] = ctrlx1;
0N/A left[leftoff + 5] = ctrly1;
0N/A left[leftoff + 6] = centerx;
0N/A left[leftoff + 7] = centery;
0N/A }
0N/A if (right != null) {
0N/A right[rightoff + 0] = centerx;
0N/A right[rightoff + 1] = centery;
0N/A right[rightoff + 2] = ctrlx2;
0N/A right[rightoff + 3] = ctrly2;
0N/A right[rightoff + 4] = x2;
0N/A right[rightoff + 5] = y2;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Solves the cubic whose coefficients are in the <code>eqn</code>
0N/A * array and places the non-complex roots back into the same array,
0N/A * returning the number of roots. The solved cubic is represented
0N/A * by the equation:
0N/A * <pre>
0N/A * eqn = {c, b, a, d}
0N/A * dx^3 + ax^2 + bx + c = 0
0N/A * </pre>
0N/A * A return value of -1 is used to distinguish a constant equation
0N/A * that might be always 0 or never 0 from an equation that has no
0N/A * zeroes.
0N/A * @param eqn an array containing coefficients for a cubic
0N/A * @return the number of roots, or -1 if the equation is a constant.
0N/A * @since 1.2
0N/A */
0N/A public static int solveCubic(double eqn[]) {
0N/A return solveCubic(eqn, eqn);
0N/A }
0N/A
0N/A /**
0N/A * Solve the cubic whose coefficients are in the <code>eqn</code>
0N/A * array and place the non-complex roots into the <code>res</code>
0N/A * array, returning the number of roots.
0N/A * The cubic solved is represented by the equation:
0N/A * eqn = {c, b, a, d}
0N/A * dx^3 + ax^2 + bx + c = 0
0N/A * A return value of -1 is used to distinguish a constant equation,
0N/A * which may be always 0 or never 0, from an equation which has no
0N/A * zeroes.
0N/A * @param eqn the specified array of coefficients to use to solve
0N/A * the cubic equation
0N/A * @param res the array that contains the non-complex roots
0N/A * resulting from the solution of the cubic equation
0N/A * @return the number of roots, or -1 if the equation is a constant
0N/A * @since 1.3
0N/A */
0N/A public static int solveCubic(double eqn[], double res[]) {
3442N/A // From Graphics Gems:
3442N/A // http://tog.acm.org/resources/GraphicsGems/gems/Roots3And4.c
3442N/A final double d = eqn[3];
3442N/A if (d == 0) {
0N/A return QuadCurve2D.solveQuadratic(eqn, res);
0N/A }
3442N/A
3442N/A /* normal form: x^3 + Ax^2 + Bx + C = 0 */
3442N/A final double A = eqn[2] / d;
3442N/A final double B = eqn[1] / d;
3442N/A final double C = eqn[0] / d;
3442N/A
3442N/A
3442N/A // substitute x = y - A/3 to eliminate quadratic term:
3442N/A // x^3 +Px + Q = 0
3442N/A //
3442N/A // Since we actually need P/3 and Q/2 for all of the
3442N/A // calculations that follow, we will calculate
3442N/A // p = P/3
3442N/A // q = Q/2
3442N/A // instead and use those values for simplicity of the code.
3442N/A double sq_A = A * A;
3442N/A double p = 1.0/3 * (-1.0/3 * sq_A + B);
3442N/A double q = 1.0/2 * (2.0/27 * A * sq_A - 1.0/3 * A * B + C);
3442N/A
3442N/A /* use Cardano's formula */
3442N/A
3442N/A double cb_p = p * p * p;
3442N/A double D = q * q + cb_p;
3442N/A
3442N/A final double sub = 1.0/3 * A;
3442N/A
3442N/A int num;
3442N/A if (D < 0) { /* Casus irreducibilis: three real solutions */
3442N/A // see: http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method
3442N/A double phi = 1.0/3 * Math.acos(-q / Math.sqrt(-cb_p));
3442N/A double t = 2 * Math.sqrt(-p);
3442N/A
0N/A if (res == eqn) {
3442N/A eqn = Arrays.copyOf(eqn, 4);
0N/A }
3442N/A
3442N/A res[ 0 ] = ( t * Math.cos(phi));
3442N/A res[ 1 ] = (-t * Math.cos(phi + Math.PI / 3));
3442N/A res[ 2 ] = (-t * Math.cos(phi - Math.PI / 3));
3442N/A num = 3;
3442N/A
3442N/A for (int i = 0; i < num; ++i) {
3442N/A res[ i ] -= sub;
3442N/A }
3442N/A
0N/A } else {
3442N/A // Please see the comment in fixRoots marked 'XXX' before changing
3442N/A // any of the code in this case.
3442N/A double sqrt_D = Math.sqrt(D);
3442N/A double u = Math.cbrt(sqrt_D - q);
3442N/A double v = - Math.cbrt(sqrt_D + q);
3442N/A double uv = u+v;
3442N/A
3442N/A num = 1;
3442N/A
3442N/A double err = 1200000000*ulp(abs(uv) + abs(sub));
3442N/A if (iszero(D, err) || within(u, v, err)) {
3442N/A if (res == eqn) {
3442N/A eqn = Arrays.copyOf(eqn, 4);
3442N/A }
3442N/A res[1] = -(uv / 2) - sub;
3442N/A num = 2;
0N/A }
3442N/A // this must be done after the potential Arrays.copyOf
3442N/A res[ 0 ] = uv - sub;
3442N/A }
3442N/A
3442N/A if (num > 1) { // num == 3 || num == 2
3442N/A num = fixRoots(eqn, res, num);
0N/A }
3442N/A if (num > 2 && (res[2] == res[1] || res[2] == res[0])) {
3442N/A num--;
3442N/A }
3442N/A if (num > 1 && res[1] == res[0]) {
3442N/A res[1] = res[--num]; // Copies res[2] to res[1] if needed
3442N/A }
3442N/A return num;
0N/A }
0N/A
3442N/A // preconditions: eqn != res && eqn[3] != 0 && num > 1
3442N/A // This method tries to improve the accuracy of the roots of eqn (which
3442N/A // should be in res). It also might eliminate roots in res if it decideds
3442N/A // that they're not real roots. It will not check for roots that the
3442N/A // computation of res might have missed, so this method should only be
3442N/A // used when the roots in res have been computed using an algorithm
3442N/A // that never underestimates the number of roots (such as solveCubic above)
3442N/A private static int fixRoots(double[] eqn, double[] res, int num) {
3442N/A double[] intervals = {eqn[1], 2*eqn[2], 3*eqn[3]};
3442N/A int critCount = QuadCurve2D.solveQuadratic(intervals, intervals);
3442N/A if (critCount == 2 && intervals[0] == intervals[1]) {
3442N/A critCount--;
3442N/A }
3442N/A if (critCount == 2 && intervals[0] > intervals[1]) {
3442N/A double tmp = intervals[0];
3442N/A intervals[0] = intervals[1];
3442N/A intervals[1] = tmp;
3442N/A }
3442N/A
3442N/A // below we use critCount to possibly filter out roots that shouldn't
3442N/A // have been computed. We require that eqn[3] != 0, so eqn is a proper
3442N/A // cubic, which means that its limits at -/+inf are -/+inf or +/-inf.
3442N/A // Therefore, if critCount==2, the curve is shaped like a sideways S,
3442N/A // and it could have 1-3 roots. If critCount==0 it is monotonic, and
3442N/A // if critCount==1 it is monotonic with a single point where it is
3442N/A // flat. In the last 2 cases there can only be 1 root. So in cases
3442N/A // where num > 1 but critCount < 2, we eliminate all roots in res
3442N/A // except one.
3442N/A
3442N/A if (num == 3) {
3442N/A double xe = getRootUpperBound(eqn);
3442N/A double x0 = -xe;
3442N/A
3442N/A Arrays.sort(res, 0, num);
3442N/A if (critCount == 2) {
3442N/A // this just tries to improve the accuracy of the computed
3442N/A // roots using Newton's method.
3442N/A res[0] = refineRootWithHint(eqn, x0, intervals[0], res[0]);
3442N/A res[1] = refineRootWithHint(eqn, intervals[0], intervals[1], res[1]);
3442N/A res[2] = refineRootWithHint(eqn, intervals[1], xe, res[2]);
3442N/A return 3;
3442N/A } else if (critCount == 1) {
3442N/A // we only need fx0 and fxe for the sign of the polynomial
3442N/A // at -inf and +inf respectively, so we don't need to do
3442N/A // fx0 = solveEqn(eqn, 3, x0); fxe = solveEqn(eqn, 3, xe)
3442N/A double fxe = eqn[3];
3442N/A double fx0 = -fxe;
3442N/A
3442N/A double x1 = intervals[0];
3442N/A double fx1 = solveEqn(eqn, 3, x1);
3442N/A
3442N/A // if critCount == 1 or critCount == 0, but num == 3 then
3442N/A // something has gone wrong. This branch and the one below
3442N/A // would ideally never execute, but if they do we can't know
3442N/A // which of the computed roots is closest to the real root;
3442N/A // therefore, we can't use refineRootWithHint. But even if
3442N/A // we did know, being here most likely means that the
3442N/A // curve is very flat close to two of the computed roots
3442N/A // (or maybe even all three). This might make Newton's method
3442N/A // fail altogether, which would be a pain to detect and fix.
3442N/A // This is why we use a very stable bisection method.
3442N/A if (oppositeSigns(fx0, fx1)) {
3442N/A res[0] = bisectRootWithHint(eqn, x0, x1, res[0]);
3442N/A } else if (oppositeSigns(fx1, fxe)) {
3442N/A res[0] = bisectRootWithHint(eqn, x1, xe, res[2]);
3442N/A } else /* fx1 must be 0 */ {
3442N/A res[0] = x1;
3442N/A }
3442N/A // return 1
3442N/A } else if (critCount == 0) {
3442N/A res[0] = bisectRootWithHint(eqn, x0, xe, res[1]);
3442N/A // return 1
3442N/A }
3442N/A } else if (num == 2 && critCount == 2) {
3442N/A // XXX: here we assume that res[0] has better accuracy than res[1].
3442N/A // This is true because this method is only used from solveCubic
3442N/A // which puts in res[0] the root that it would compute anyway even
3442N/A // if num==1. If this method is ever used from any other method, or
3442N/A // if the solveCubic implementation changes, this assumption should
3442N/A // be reevaluated, and the choice of goodRoot might have to become
3442N/A // goodRoot = (abs(eqn'(res[0])) > abs(eqn'(res[1]))) ? res[0] : res[1]
3442N/A // where eqn' is the derivative of eqn.
3442N/A double goodRoot = res[0];
3442N/A double badRoot = res[1];
3442N/A double x1 = intervals[0];
3442N/A double x2 = intervals[1];
3442N/A // If a cubic curve really has 2 roots, one of those roots must be
3442N/A // at a critical point. That can't be goodRoot, so we compute x to
3442N/A // be the farthest critical point from goodRoot. If there are two
3442N/A // roots, x must be the second one, so we evaluate eqn at x, and if
3442N/A // it is zero (or close enough) we put x in res[1] (or badRoot, if
3442N/A // |solveEqn(eqn, 3, badRoot)| < |solveEqn(eqn, 3, x)| but this
3442N/A // shouldn't happen often).
3442N/A double x = abs(x1 - goodRoot) > abs(x2 - goodRoot) ? x1 : x2;
3442N/A double fx = solveEqn(eqn, 3, x);
3442N/A
3442N/A if (iszero(fx, 10000000*ulp(x))) {
3442N/A double badRootVal = solveEqn(eqn, 3, badRoot);
3442N/A res[1] = abs(badRootVal) < abs(fx) ? badRoot : x;
3442N/A return 2;
3442N/A }
3442N/A } // else there can only be one root - goodRoot, and it is already in res[0]
3442N/A
3442N/A return 1;
3442N/A }
3442N/A
3442N/A // use newton's method.
3442N/A private static double refineRootWithHint(double[] eqn, double min, double max, double t) {
3442N/A if (!inInterval(t, min, max)) {
3442N/A return t;
3442N/A }
3442N/A double[] deriv = {eqn[1], 2*eqn[2], 3*eqn[3]};
3442N/A double origt = t;
0N/A for (int i = 0; i < 3; i++) {
3442N/A double slope = solveEqn(deriv, 2, t);
3442N/A double y = solveEqn(eqn, 3, t);
3442N/A double delta = - (y / slope);
3442N/A double newt = t + delta;
3442N/A
3442N/A if (slope == 0 || y == 0 || t == newt) {
3442N/A break;
3442N/A }
3442N/A
3442N/A t = newt;
3442N/A }
3442N/A if (within(t, origt, 1000*ulp(origt)) && inInterval(t, min, max)) {
3442N/A return t;
3442N/A }
3442N/A return origt;
3442N/A }
3442N/A
3442N/A private static double bisectRootWithHint(double[] eqn, double x0, double xe, double hint) {
3442N/A double delta1 = Math.min(abs(hint - x0) / 64, 0.0625);
3442N/A double delta2 = Math.min(abs(hint - xe) / 64, 0.0625);
3442N/A double x02 = hint - delta1;
3442N/A double xe2 = hint + delta2;
3442N/A double fx02 = solveEqn(eqn, 3, x02);
3442N/A double fxe2 = solveEqn(eqn, 3, xe2);
3442N/A while (oppositeSigns(fx02, fxe2)) {
3442N/A if (x02 >= xe2) {
3442N/A return x02;
0N/A }
3442N/A x0 = x02;
3442N/A xe = xe2;
3442N/A delta1 /= 64;
3442N/A delta2 /= 64;
3442N/A x02 = hint - delta1;
3442N/A xe2 = hint + delta2;
3442N/A fx02 = solveEqn(eqn, 3, x02);
3442N/A fxe2 = solveEqn(eqn, 3, xe2);
0N/A }
3442N/A if (fx02 == 0) {
3442N/A return x02;
3442N/A }
3442N/A if (fxe2 == 0) {
3442N/A return xe2;
3442N/A }
3442N/A
3442N/A return bisectRoot(eqn, x0, xe);
3442N/A }
3442N/A
3442N/A private static double bisectRoot(double[] eqn, double x0, double xe) {
3442N/A double fx0 = solveEqn(eqn, 3, x0);
3442N/A double m = x0 + (xe - x0) / 2;
3442N/A while (m != x0 && m != xe) {
3442N/A double fm = solveEqn(eqn, 3, m);
3442N/A if (fm == 0) {
3442N/A return m;
3442N/A }
3442N/A if (oppositeSigns(fx0, fm)) {
3442N/A xe = m;
3442N/A } else {
3442N/A fx0 = fm;
3442N/A x0 = m;
3442N/A }
3442N/A m = x0 + (xe-x0)/2;
3442N/A }
3442N/A return m;
3442N/A }
3442N/A
3442N/A private static boolean inInterval(double t, double min, double max) {
3442N/A return min <= t && t <= max;
3442N/A }
3442N/A
3442N/A private static boolean within(double x, double y, double err) {
3442N/A double d = y - x;
3442N/A return (d <= err && d >= -err);
3442N/A }
3442N/A
3442N/A private static boolean iszero(double x, double err) {
3442N/A return within(x, 0, err);
3442N/A }
3442N/A
3442N/A private static boolean oppositeSigns(double x1, double x2) {
3442N/A return (x1 < 0 && x2 > 0) || (x1 > 0 && x2 < 0);
0N/A }
0N/A
0N/A private static double solveEqn(double eqn[], int order, double t) {
0N/A double v = eqn[order];
0N/A while (--order >= 0) {
0N/A v = v * t + eqn[order];
0N/A }
0N/A return v;
0N/A }
0N/A
3442N/A /*
3442N/A * Computes M+1 where M is an upper bound for all the roots in of eqn.
3442N/A * See: http://en.wikipedia.org/wiki/Sturm%27s_theorem#Applications.
3442N/A * The above link doesn't contain a proof, but I [dlila] proved it myself
3442N/A * so the result is reliable. The proof isn't difficult, but it's a bit
3442N/A * long to include here.
3442N/A * Precondition: eqn must represent a cubic polynomial
3442N/A */
3442N/A private static double getRootUpperBound(double[] eqn) {
3442N/A double d = eqn[3];
3442N/A double a = eqn[2];
3442N/A double b = eqn[1];
3442N/A double c = eqn[0];
3442N/A
3442N/A double M = 1 + max(max(abs(a), abs(b)), abs(c)) / abs(d);
3442N/A M += ulp(M) + 1;
3442N/A return M;
0N/A }
0N/A
3442N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(double x, double y) {
0N/A if (!(x * 0.0 + y * 0.0 == 0.0)) {
0N/A /* Either x or y was infinite or NaN.
0N/A * A NaN always produces a negative response to any test
0N/A * and Infinity values cannot be "inside" any path so
0N/A * they should return false as well.
0N/A */
0N/A return false;
0N/A }
0N/A // We count the "Y" crossings to determine if the point is
0N/A // inside the curve bounded by its closing line.
0N/A double x1 = getX1();
0N/A double y1 = getY1();
0N/A double x2 = getX2();
0N/A double y2 = getY2();
0N/A int crossings =
0N/A (Curve.pointCrossingsForLine(x, y, x1, y1, x2, y2) +
0N/A Curve.pointCrossingsForCubic(x, y,
0N/A x1, y1,
0N/A getCtrlX1(), getCtrlY1(),
0N/A getCtrlX2(), getCtrlY2(),
0N/A x2, y2, 0));
0N/A return ((crossings & 1) == 1);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(Point2D p) {
0N/A return contains(p.getX(), p.getY());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean intersects(double x, double y, double w, double h) {
0N/A // Trivially reject non-existant rectangles
0N/A if (w <= 0 || h <= 0) {
0N/A return false;
0N/A }
0N/A
3353N/A int numCrossings = rectCrossings(x, y, w, h);
3353N/A // the intended return value is
3353N/A // numCrossings != 0 || numCrossings == Curve.RECT_INTERSECTS
3353N/A // but if (numCrossings != 0) numCrossings == INTERSECTS won't matter
3353N/A // and if !(numCrossings != 0) then numCrossings == 0, so
3353N/A // numCrossings != RECT_INTERSECT
3353N/A return numCrossings != 0;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean intersects(Rectangle2D r) {
0N/A return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(double x, double y, double w, double h) {
0N/A if (w <= 0 || h <= 0) {
0N/A return false;
0N/A }
3352N/A
3352N/A int numCrossings = rectCrossings(x, y, w, h);
3352N/A return !(numCrossings == 0 || numCrossings == Curve.RECT_INTERSECTS);
3352N/A }
3352N/A
3352N/A private int rectCrossings(double x, double y, double w, double h) {
3352N/A int crossings = 0;
3352N/A if (!(getX1() == getX2() && getY1() == getY2())) {
3352N/A crossings = Curve.rectCrossingsForLine(crossings,
3352N/A x, y,
3352N/A x+w, y+h,
3352N/A getX1(), getY1(),
3352N/A getX2(), getY2());
3352N/A if (crossings == Curve.RECT_INTERSECTS) {
3352N/A return crossings;
3352N/A }
0N/A }
3352N/A // we call this with the curve's direction reversed, because we wanted
3352N/A // to call rectCrossingsForLine first, because it's cheaper.
3352N/A return Curve.rectCrossingsForCubic(crossings,
3352N/A x, y,
3352N/A x+w, y+h,
3352N/A getX2(), getY2(),
3352N/A getCtrlX2(), getCtrlY2(),
3352N/A getCtrlX1(), getCtrlY1(),
3352N/A getX1(), getY1(), 0);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(Rectangle2D r) {
0N/A return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle getBounds() {
0N/A return getBounds2D().getBounds();
0N/A }
0N/A
0N/A /**
0N/A * Returns an iteration object that defines the boundary of the
0N/A * shape.
0N/A * The iterator for this class is not multi-threaded safe,
0N/A * which means that this <code>CubicCurve2D</code> class does not
0N/A * guarantee that modifications to the geometry of this
0N/A * <code>CubicCurve2D</code> object do not affect any iterations of
0N/A * that geometry that are already in process.
0N/A * @param at an optional <code>AffineTransform</code> to be applied to the
0N/A * coordinates as they are returned in the iteration, or <code>null</code>
0N/A * if untransformed coordinates are desired
0N/A * @return the <code>PathIterator</code> object that returns the
0N/A * geometry of the outline of this <code>CubicCurve2D</code>, one
0N/A * segment at a time.
0N/A * @since 1.2
0N/A */
0N/A public PathIterator getPathIterator(AffineTransform at) {
0N/A return new CubicIterator(this, at);
0N/A }
0N/A
0N/A /**
0N/A * Return an iteration object that defines the boundary of the
0N/A * flattened shape.
0N/A * The iterator for this class is not multi-threaded safe,
0N/A * which means that this <code>CubicCurve2D</code> class does not
0N/A * guarantee that modifications to the geometry of this
0N/A * <code>CubicCurve2D</code> object do not affect any iterations of
0N/A * that geometry that are already in process.
0N/A * @param at an optional <code>AffineTransform</code> to be applied to the
0N/A * coordinates as they are returned in the iteration, or <code>null</code>
0N/A * if untransformed coordinates are desired
0N/A * @param flatness the maximum amount that the control points
0N/A * for a given curve can vary from colinear before a subdivided
0N/A * curve is replaced by a straight line connecting the end points
0N/A * @return the <code>PathIterator</code> object that returns the
0N/A * geometry of the outline of this <code>CubicCurve2D</code>,
0N/A * one segment at a time.
0N/A * @since 1.2
0N/A */
0N/A public PathIterator getPathIterator(AffineTransform at, double flatness) {
0N/A return new FlatteningPathIterator(getPathIterator(at), flatness);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new object of the same class as this object.
0N/A *
0N/A * @return a clone of this instance.
0N/A * @exception OutOfMemoryError if there is not enough memory.
0N/A * @see java.lang.Cloneable
0N/A * @since 1.2
0N/A */
0N/A public Object clone() {
0N/A try {
0N/A return super.clone();
0N/A } catch (CloneNotSupportedException e) {
0N/A // this shouldn't happen, since we are Cloneable
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A}