CubicCurve2D.java revision 2362
0N/A/*
2362N/A * Copyright (c) 1997, 2006, 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
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[]) {
0N/A // From Numerical Recipes, 5.6, Quadratic and Cubic Equations
0N/A double d = eqn[3];
0N/A if (d == 0.0) {
0N/A // The cubic has degenerated to quadratic (or line or ...).
0N/A return QuadCurve2D.solveQuadratic(eqn, res);
0N/A }
0N/A double a = eqn[2] / d;
0N/A double b = eqn[1] / d;
0N/A double c = eqn[0] / d;
0N/A int roots = 0;
0N/A double Q = (a * a - 3.0 * b) / 9.0;
0N/A double R = (2.0 * a * a * a - 9.0 * a * b + 27.0 * c) / 54.0;
0N/A double R2 = R * R;
0N/A double Q3 = Q * Q * Q;
0N/A a = a / 3.0;
0N/A if (R2 < Q3) {
0N/A double theta = Math.acos(R / Math.sqrt(Q3));
0N/A Q = -2.0 * Math.sqrt(Q);
0N/A if (res == eqn) {
0N/A // Copy the eqn so that we don't clobber it with the
0N/A // roots. This is needed so that fixRoots can do its
0N/A // work with the original equation.
0N/A eqn = new double[4];
0N/A System.arraycopy(res, 0, eqn, 0, 4);
0N/A }
0N/A res[roots++] = Q * Math.cos(theta / 3.0) - a;
0N/A res[roots++] = Q * Math.cos((theta + Math.PI * 2.0)/ 3.0) - a;
0N/A res[roots++] = Q * Math.cos((theta - Math.PI * 2.0)/ 3.0) - a;
0N/A fixRoots(res, eqn);
0N/A } else {
0N/A boolean neg = (R < 0.0);
0N/A double S = Math.sqrt(R2 - Q3);
0N/A if (neg) {
0N/A R = -R;
0N/A }
0N/A double A = Math.pow(R + S, 1.0 / 3.0);
0N/A if (!neg) {
0N/A A = -A;
0N/A }
0N/A double B = (A == 0.0) ? 0.0 : (Q / A);
0N/A res[roots++] = (A + B) - a;
0N/A }
0N/A return roots;
0N/A }
0N/A
0N/A /*
0N/A * This pruning step is necessary since solveCubic uses the
0N/A * cosine function to calculate the roots when there are 3
0N/A * of them. Since the cosine method can have an error of
0N/A * +/- 1E-14 we need to make sure that we don't make any
0N/A * bad decisions due to an error.
0N/A *
0N/A * If the root is not near one of the endpoints, then we will
0N/A * only have a slight inaccuracy in calculating the x intercept
0N/A * which will only cause a slightly wrong answer for some
0N/A * points very close to the curve. While the results in that
0N/A * case are not as accurate as they could be, they are not
0N/A * disastrously inaccurate either.
0N/A *
0N/A * On the other hand, if the error happens near one end of
0N/A * the curve, then our processing to reject values outside
0N/A * of the t=[0,1] range will fail and the results of that
0N/A * failure will be disastrous since for an entire horizontal
0N/A * range of test points, we will either overcount or undercount
0N/A * the crossings and get a wrong answer for all of them, even
0N/A * when they are clearly and obviously inside or outside the
0N/A * curve.
0N/A *
0N/A * To work around this problem, we try a couple of Newton-Raphson
0N/A * iterations to see if the true root is closer to the endpoint
0N/A * or further away. If it is further away, then we can stop
0N/A * since we know we are on the right side of the endpoint. If
0N/A * we change direction, then either we are now being dragged away
0N/A * from the endpoint in which case the first condition will cause
0N/A * us to stop, or we have passed the endpoint and are headed back.
0N/A * In the second case, we simply evaluate the slope at the
0N/A * endpoint itself and place ourselves on the appropriate side
0N/A * of it or on it depending on that result.
0N/A */
0N/A private static void fixRoots(double res[], double eqn[]) {
0N/A final double EPSILON = 1E-5;
0N/A for (int i = 0; i < 3; i++) {
0N/A double t = res[i];
0N/A if (Math.abs(t) < EPSILON) {
0N/A res[i] = findZero(t, 0, eqn);
0N/A } else if (Math.abs(t - 1) < EPSILON) {
0N/A res[i] = findZero(t, 1, eqn);
0N/A }
0N/A }
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
0N/A private static double findZero(double t, double target, double eqn[]) {
0N/A double slopeqn[] = {eqn[1], 2*eqn[2], 3*eqn[3]};
0N/A double slope;
0N/A double origdelta = 0;
0N/A double origt = t;
0N/A while (true) {
0N/A slope = solveEqn(slopeqn, 2, t);
0N/A if (slope == 0) {
0N/A // At a local minima - must return
0N/A return t;
0N/A }
0N/A double y = solveEqn(eqn, 3, t);
0N/A if (y == 0) {
0N/A // Found it! - return it
0N/A return t;
0N/A }
0N/A // assert(slope != 0 && y != 0);
0N/A double delta = - (y / slope);
0N/A // assert(delta != 0);
0N/A if (origdelta == 0) {
0N/A origdelta = delta;
0N/A }
0N/A if (t < target) {
0N/A if (delta < 0) return t;
0N/A } else if (t > target) {
0N/A if (delta > 0) return t;
0N/A } else { /* t == target */
0N/A return (delta > 0
0N/A ? (target + java.lang.Double.MIN_VALUE)
0N/A : (target - java.lang.Double.MIN_VALUE));
0N/A }
0N/A double newt = t + delta;
0N/A if (t == newt) {
0N/A // The deltas are so small that we aren't moving...
0N/A return t;
0N/A }
0N/A if (delta * origdelta < 0) {
0N/A // We have reversed our path.
0N/A int tag = (origt < t
0N/A ? getTag(target, origt, t)
0N/A : getTag(target, t, origt));
0N/A if (tag != INSIDE) {
0N/A // Local minima found away from target - return the middle
0N/A return (origt + t) / 2;
0N/A }
0N/A // Local minima somewhere near target - move to target
0N/A // and let the slope determine the resulting t.
0N/A t = target;
0N/A } else {
0N/A t = newt;
0N/A }
0N/A }
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) {
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 * Fill an array with the coefficients of the parametric equation
0N/A * in t, ready for solving against val with solveCubic.
0N/A * We currently have:
0N/A * <pre>
0N/A * val = P(t) = C1(1-t)^3 + 3CP1 t(1-t)^2 + 3CP2 t^2(1-t) + C2 t^3
0N/A * = C1 - 3C1t + 3C1t^2 - C1t^3 +
0N/A * 3CP1t - 6CP1t^2 + 3CP1t^3 +
0N/A * 3CP2t^2 - 3CP2t^3 +
0N/A * C2t^3
0N/A * 0 = (C1 - val) +
0N/A * (3CP1 - 3C1) t +
0N/A * (3C1 - 6CP1 + 3CP2) t^2 +
0N/A * (C2 - 3CP2 + 3CP1 - C1) t^3
0N/A * 0 = C + Bt + At^2 + Dt^3
0N/A * C = C1 - val
0N/A * B = 3*CP1 - 3*C1
0N/A * A = 3*CP2 - 6*CP1 + 3*C1
0N/A * D = C2 - 3*CP2 + 3*CP1 - C1
0N/A * </pre>
0N/A */
0N/A private static void fillEqn(double eqn[], double val,
0N/A double c1, double cp1, double cp2, double c2) {
0N/A eqn[0] = c1 - val;
0N/A eqn[1] = (cp1 - c1) * 3.0;
0N/A eqn[2] = (cp2 - cp1 - cp1 + c1) * 3.0;
0N/A eqn[3] = c2 + (cp1 - cp2) * 3.0 - c1;
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * Evaluate the t values in the first num slots of the vals[] array
0N/A * and place the evaluated values back into the same array. Only
0N/A * evaluate t values that are within the range <0, 1>, including
0N/A * the 0 and 1 ends of the range iff the include0 or include1
0N/A * booleans are true. If an "inflection" equation is handed in,
0N/A * then any points which represent a point of inflection for that
0N/A * cubic equation are also ignored.
0N/A */
0N/A private static int evalCubic(double vals[], int num,
0N/A boolean include0,
0N/A boolean include1,
0N/A double inflect[],
0N/A double c1, double cp1,
0N/A double cp2, double c2) {
0N/A int j = 0;
0N/A for (int i = 0; i < num; i++) {
0N/A double t = vals[i];
0N/A if ((include0 ? t >= 0 : t > 0) &&
0N/A (include1 ? t <= 1 : t < 1) &&
0N/A (inflect == null ||
0N/A inflect[1] + (2*inflect[2] + 3*inflect[3]*t)*t != 0))
0N/A {
0N/A double u = 1 - t;
0N/A vals[j++] = c1*u*u*u + 3*cp1*t*u*u + 3*cp2*t*t*u + c2*t*t*t;
0N/A }
0N/A }
0N/A return j;
0N/A }
0N/A
0N/A private static final int BELOW = -2;
0N/A private static final int LOWEDGE = -1;
0N/A private static final int INSIDE = 0;
0N/A private static final int HIGHEDGE = 1;
0N/A private static final int ABOVE = 2;
0N/A
0N/A /*
0N/A * Determine where coord lies with respect to the range from
0N/A * low to high. It is assumed that low <= high. The return
0N/A * value is one of the 5 values BELOW, LOWEDGE, INSIDE, HIGHEDGE,
0N/A * or ABOVE.
0N/A */
0N/A private static int getTag(double coord, double low, double high) {
0N/A if (coord <= low) {
0N/A return (coord < low ? BELOW : LOWEDGE);
0N/A }
0N/A if (coord >= high) {
0N/A return (coord > high ? ABOVE : HIGHEDGE);
0N/A }
0N/A return INSIDE;
0N/A }
0N/A
0N/A /*
0N/A * Determine if the pttag represents a coordinate that is already
0N/A * in its test range, or is on the border with either of the two
0N/A * opttags representing another coordinate that is "towards the
0N/A * inside" of that test range. In other words, are either of the
0N/A * two "opt" points "drawing the pt inward"?
0N/A */
0N/A private static boolean inwards(int pttag, int opt1tag, int opt2tag) {
0N/A switch (pttag) {
0N/A case BELOW:
0N/A case ABOVE:
0N/A default:
0N/A return false;
0N/A case LOWEDGE:
0N/A return (opt1tag >= INSIDE || opt2tag >= INSIDE);
0N/A case INSIDE:
0N/A return true;
0N/A case HIGHEDGE:
0N/A return (opt1tag <= INSIDE || opt2tag <= INSIDE);
0N/A }
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
0N/A // Trivially accept if either endpoint is inside the rectangle
0N/A // (not on its border since it may end there and not go inside)
0N/A // Record where they lie with respect to the rectangle.
0N/A // -1 => left, 0 => inside, 1 => right
0N/A double x1 = getX1();
0N/A double y1 = getY1();
0N/A int x1tag = getTag(x1, x, x+w);
0N/A int y1tag = getTag(y1, y, y+h);
0N/A if (x1tag == INSIDE && y1tag == INSIDE) {
0N/A return true;
0N/A }
0N/A double x2 = getX2();
0N/A double y2 = getY2();
0N/A int x2tag = getTag(x2, x, x+w);
0N/A int y2tag = getTag(y2, y, y+h);
0N/A if (x2tag == INSIDE && y2tag == INSIDE) {
0N/A return true;
0N/A }
0N/A
0N/A double ctrlx1 = getCtrlX1();
0N/A double ctrly1 = getCtrlY1();
0N/A double ctrlx2 = getCtrlX2();
0N/A double ctrly2 = getCtrlY2();
0N/A int ctrlx1tag = getTag(ctrlx1, x, x+w);
0N/A int ctrly1tag = getTag(ctrly1, y, y+h);
0N/A int ctrlx2tag = getTag(ctrlx2, x, x+w);
0N/A int ctrly2tag = getTag(ctrly2, y, y+h);
0N/A
0N/A // Trivially reject if all points are entirely to one side of
0N/A // the rectangle.
0N/A if (x1tag < INSIDE && x2tag < INSIDE &&
0N/A ctrlx1tag < INSIDE && ctrlx2tag < INSIDE)
0N/A {
0N/A return false; // All points left
0N/A }
0N/A if (y1tag < INSIDE && y2tag < INSIDE &&
0N/A ctrly1tag < INSIDE && ctrly2tag < INSIDE)
0N/A {
0N/A return false; // All points above
0N/A }
0N/A if (x1tag > INSIDE && x2tag > INSIDE &&
0N/A ctrlx1tag > INSIDE && ctrlx2tag > INSIDE)
0N/A {
0N/A return false; // All points right
0N/A }
0N/A if (y1tag > INSIDE && y2tag > INSIDE &&
0N/A ctrly1tag > INSIDE && ctrly2tag > INSIDE)
0N/A {
0N/A return false; // All points below
0N/A }
0N/A
0N/A // Test for endpoints on the edge where either the segment
0N/A // or the curve is headed "inwards" from them
0N/A // Note: These tests are a superset of the fast endpoint tests
0N/A // above and thus repeat those tests, but take more time
0N/A // and cover more cases
0N/A if (inwards(x1tag, x2tag, ctrlx1tag) &&
0N/A inwards(y1tag, y2tag, ctrly1tag))
0N/A {
0N/A // First endpoint on border with either edge moving inside
0N/A return true;
0N/A }
0N/A if (inwards(x2tag, x1tag, ctrlx2tag) &&
0N/A inwards(y2tag, y1tag, ctrly2tag))
0N/A {
0N/A // Second endpoint on border with either edge moving inside
0N/A return true;
0N/A }
0N/A
0N/A // Trivially accept if endpoints span directly across the rectangle
0N/A boolean xoverlap = (x1tag * x2tag <= 0);
0N/A boolean yoverlap = (y1tag * y2tag <= 0);
0N/A if (x1tag == INSIDE && x2tag == INSIDE && yoverlap) {
0N/A return true;
0N/A }
0N/A if (y1tag == INSIDE && y2tag == INSIDE && xoverlap) {
0N/A return true;
0N/A }
0N/A
0N/A // We now know that both endpoints are outside the rectangle
0N/A // but the 4 points are not all on one side of the rectangle.
0N/A // Therefore the curve cannot be contained inside the rectangle,
0N/A // but the rectangle might be contained inside the curve, or
0N/A // the curve might intersect the boundary of the rectangle.
0N/A
0N/A double[] eqn = new double[4];
0N/A double[] res = new double[4];
0N/A if (!yoverlap) {
0N/A // Both y coordinates for the closing segment are above or
0N/A // below the rectangle which means that we can only intersect
0N/A // if the curve crosses the top (or bottom) of the rectangle
0N/A // in more than one place and if those crossing locations
0N/A // span the horizontal range of the rectangle.
0N/A fillEqn(eqn, (y1tag < INSIDE ? y : y+h), y1, ctrly1, ctrly2, y2);
0N/A int num = solveCubic(eqn, res);
0N/A num = evalCubic(res, num, true, true, null,
0N/A x1, ctrlx1, ctrlx2, x2);
0N/A // odd counts imply the crossing was out of [0,1] bounds
0N/A // otherwise there is no way for that part of the curve to
0N/A // "return" to meet its endpoint
0N/A return (num == 2 &&
0N/A getTag(res[0], x, x+w) * getTag(res[1], x, x+w) <= 0);
0N/A }
0N/A
0N/A // Y ranges overlap. Now we examine the X ranges
0N/A if (!xoverlap) {
0N/A // Both x coordinates for the closing segment are left of
0N/A // or right of the rectangle which means that we can only
0N/A // intersect if the curve crosses the left (or right) edge
0N/A // of the rectangle in more than one place and if those
0N/A // crossing locations span the vertical range of the rectangle.
0N/A fillEqn(eqn, (x1tag < INSIDE ? x : x+w), x1, ctrlx1, ctrlx2, x2);
0N/A int num = solveCubic(eqn, res);
0N/A num = evalCubic(res, num, true, true, null,
0N/A y1, ctrly1, ctrly2, y2);
0N/A // odd counts imply the crossing was out of [0,1] bounds
0N/A // otherwise there is no way for that part of the curve to
0N/A // "return" to meet its endpoint
0N/A return (num == 2 &&
0N/A getTag(res[0], y, y+h) * getTag(res[1], y, y+h) <= 0);
0N/A }
0N/A
0N/A // The X and Y ranges of the endpoints overlap the X and Y
0N/A // ranges of the rectangle, now find out how the endpoint
0N/A // line segment intersects the Y range of the rectangle
0N/A double dx = x2 - x1;
0N/A double dy = y2 - y1;
0N/A double k = y2 * x1 - x2 * y1;
0N/A int c1tag, c2tag;
0N/A if (y1tag == INSIDE) {
0N/A c1tag = x1tag;
0N/A } else {
0N/A c1tag = getTag((k + dx * (y1tag < INSIDE ? y : y+h)) / dy, x, x+w);
0N/A }
0N/A if (y2tag == INSIDE) {
0N/A c2tag = x2tag;
0N/A } else {
0N/A c2tag = getTag((k + dx * (y2tag < INSIDE ? y : y+h)) / dy, x, x+w);
0N/A }
0N/A // If the part of the line segment that intersects the Y range
0N/A // of the rectangle crosses it horizontally - trivially accept
0N/A if (c1tag * c2tag <= 0) {
0N/A return true;
0N/A }
0N/A
0N/A // Now we know that both the X and Y ranges intersect and that
0N/A // the endpoint line segment does not directly cross the rectangle.
0N/A //
0N/A // We can almost treat this case like one of the cases above
0N/A // where both endpoints are to one side, except that we may
0N/A // get one or three intersections of the curve with the vertical
0N/A // side of the rectangle. This is because the endpoint segment
0N/A // accounts for the other intersection in an even pairing. Thus,
0N/A // with the endpoint crossing we end up with 2 or 4 total crossings.
0N/A //
0N/A // (Remember there is overlap in both the X and Y ranges which
0N/A // means that the segment itself must cross at least one vertical
0N/A // edge of the rectangle - in particular, the "near vertical side"
0N/A // - leaving an odd number of intersections for the curve.)
0N/A //
0N/A // Now we calculate the y tags of all the intersections on the
0N/A // "near vertical side" of the rectangle. We will have one with
0N/A // the endpoint segment, and one or three with the curve. If
0N/A // any pair of those vertical intersections overlap the Y range
0N/A // of the rectangle, we have an intersection. Otherwise, we don't.
0N/A
0N/A // c1tag = vertical intersection class of the endpoint segment
0N/A //
0N/A // Choose the y tag of the endpoint that was not on the same
0N/A // side of the rectangle as the subsegment calculated above.
0N/A // Note that we can "steal" the existing Y tag of that endpoint
0N/A // since it will be provably the same as the vertical intersection.
0N/A c1tag = ((c1tag * x1tag <= 0) ? y1tag : y2tag);
0N/A
0N/A // Now we have to calculate an array of solutions of the curve
0N/A // with the "near vertical side" of the rectangle. Then we
0N/A // need to sort the tags and do a pairwise range test to see
0N/A // if either of the pairs of crossings spans the Y range of
0N/A // the rectangle.
0N/A //
0N/A // Note that the c2tag can still tell us which vertical edge
0N/A // to test against.
0N/A fillEqn(eqn, (c2tag < INSIDE ? x : x+w), x1, ctrlx1, ctrlx2, x2);
0N/A int num = solveCubic(eqn, res);
0N/A num = evalCubic(res, num, true, true, null, y1, ctrly1, ctrly2, y2);
0N/A
0N/A // Now put all of the tags into a bucket and sort them. There
0N/A // is an intersection iff one of the pairs of tags "spans" the
0N/A // Y range of the rectangle.
0N/A int tags[] = new int[num+1];
0N/A for (int i = 0; i < num; i++) {
0N/A tags[i] = getTag(res[i], y, y+h);
0N/A }
0N/A tags[num] = c1tag;
0N/A Arrays.sort(tags);
0N/A return ((num >= 1 && tags[0] * tags[1] <= 0) ||
0N/A (num >= 3 && tags[2] * tags[3] <= 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 }
0N/A // Assertion: Cubic curves closed by connecting their
0N/A // endpoints form either one or two convex halves with
0N/A // the closing line segment as an edge of both sides.
0N/A if (!(contains(x, y) &&
0N/A contains(x + w, y) &&
0N/A contains(x + w, y + h) &&
0N/A contains(x, y + h))) {
0N/A return false;
0N/A }
0N/A // Either the rectangle is entirely inside one of the convex
0N/A // halves or it crosses from one to the other, in which case
0N/A // it must intersect the closing line segment.
0N/A Rectangle2D rect = new Rectangle2D.Double(x, y, w, h);
0N/A return !rect.intersectsLine(getX1(), getY1(), getX2(), getY2());
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}