|
Java example source code file (CubicCurve2D.java)
The CubicCurve2D.java Java example source code/* * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.awt.geom; import java.awt.Shape; import java.awt.Rectangle; import java.util.Arrays; import java.io.Serializable; import sun.awt.geom.Curve; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.ulp; /** * The <code>CubicCurve2D class defines a cubic parametric curve * segment in {@code (x,y)} coordinate space. * <p> * This class is only the abstract superclass for all objects which * store a 2D cubic curve segment. * The actual storage representation of the coordinates is left to * the subclass. * * @author Jim Graham * @since 1.2 */ public abstract class CubicCurve2D implements Shape, Cloneable { /** * A cubic parametric curve segment specified with * {@code float} coordinates. * @since 1.2 */ public static class Float extends CubicCurve2D implements Serializable { /** * The X coordinate of the start point * of the cubic curve segment. * @since 1.2 * @serial */ public float x1; /** * The Y coordinate of the start point * of the cubic curve segment. * @since 1.2 * @serial */ public float y1; /** * The X coordinate of the first control point * of the cubic curve segment. * @since 1.2 * @serial */ public float ctrlx1; /** * The Y coordinate of the first control point * of the cubic curve segment. * @since 1.2 * @serial */ public float ctrly1; /** * The X coordinate of the second control point * of the cubic curve segment. * @since 1.2 * @serial */ public float ctrlx2; /** * The Y coordinate of the second control point * of the cubic curve segment. * @since 1.2 * @serial */ public float ctrly2; /** * The X coordinate of the end point * of the cubic curve segment. * @since 1.2 * @serial */ public float x2; /** * The Y coordinate of the end point * of the cubic curve segment. * @since 1.2 * @serial */ public float y2; /** * Constructs and initializes a CubicCurve with coordinates * (0, 0, 0, 0, 0, 0, 0, 0). * @since 1.2 */ public Float() { } /** * Constructs and initializes a {@code CubicCurve2D} from * the specified {@code float} coordinates. * * @param x1 the X coordinate for the start point * of the resulting {@code CubicCurve2D} * @param y1 the Y coordinate for the start point * of the resulting {@code CubicCurve2D} * @param ctrlx1 the X coordinate for the first control point * of the resulting {@code CubicCurve2D} * @param ctrly1 the Y coordinate for the first control point * of the resulting {@code CubicCurve2D} * @param ctrlx2 the X coordinate for the second control point * of the resulting {@code CubicCurve2D} * @param ctrly2 the Y coordinate for the second control point * of the resulting {@code CubicCurve2D} * @param x2 the X coordinate for the end point * of the resulting {@code CubicCurve2D} * @param y2 the Y coordinate for the end point * of the resulting {@code CubicCurve2D} * @since 1.2 */ public Float(float x1, float y1, float ctrlx1, float ctrly1, float ctrlx2, float ctrly2, float x2, float y2) { setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2); } /** * {@inheritDoc} * @since 1.2 */ public double getX1() { return (double) x1; } /** * {@inheritDoc} * @since 1.2 */ public double getY1() { return (double) y1; } /** * {@inheritDoc} * @since 1.2 */ public Point2D getP1() { return new Point2D.Float(x1, y1); } /** * {@inheritDoc} * @since 1.2 */ public double getCtrlX1() { return (double) ctrlx1; } /** * {@inheritDoc} * @since 1.2 */ public double getCtrlY1() { return (double) ctrly1; } /** * {@inheritDoc} * @since 1.2 */ public Point2D getCtrlP1() { return new Point2D.Float(ctrlx1, ctrly1); } /** * {@inheritDoc} * @since 1.2 */ public double getCtrlX2() { return (double) ctrlx2; } /** * {@inheritDoc} * @since 1.2 */ public double getCtrlY2() { return (double) ctrly2; } /** * {@inheritDoc} * @since 1.2 */ public Point2D getCtrlP2() { return new Point2D.Float(ctrlx2, ctrly2); } /** * {@inheritDoc} * @since 1.2 */ public double getX2() { return (double) x2; } /** * {@inheritDoc} * @since 1.2 */ public double getY2() { return (double) y2; } /** * {@inheritDoc} * @since 1.2 */ public Point2D getP2() { return new Point2D.Float(x2, y2); } /** * {@inheritDoc} * @since 1.2 */ public void setCurve(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2) { this.x1 = (float) x1; this.y1 = (float) y1; this.ctrlx1 = (float) ctrlx1; this.ctrly1 = (float) ctrly1; this.ctrlx2 = (float) ctrlx2; this.ctrly2 = (float) ctrly2; this.x2 = (float) x2; this.y2 = (float) y2; } /** * Sets the location of the end points and control points * of this curve to the specified {@code float} coordinates. * * @param x1 the X coordinate used to set the start point * of this {@code CubicCurve2D} * @param y1 the Y coordinate used to set the start point * of this {@code CubicCurve2D} * @param ctrlx1 the X coordinate used to set the first control point * of this {@code CubicCurve2D} * @param ctrly1 the Y coordinate used to set the first control point * of this {@code CubicCurve2D} * @param ctrlx2 the X coordinate used to set the second control point * of this {@code CubicCurve2D} * @param ctrly2 the Y coordinate used to set the second control point * of this {@code CubicCurve2D} * @param x2 the X coordinate used to set the end point * of this {@code CubicCurve2D} * @param y2 the Y coordinate used to set the end point * of this {@code CubicCurve2D} * @since 1.2 */ public void setCurve(float x1, float y1, float ctrlx1, float ctrly1, float ctrlx2, float ctrly2, float x2, float y2) { this.x1 = x1; this.y1 = y1; this.ctrlx1 = ctrlx1; this.ctrly1 = ctrly1; this.ctrlx2 = ctrlx2; this.ctrly2 = ctrly2; this.x2 = x2; this.y2 = y2; } /** * {@inheritDoc} * @since 1.2 */ public Rectangle2D getBounds2D() { float left = Math.min(Math.min(x1, x2), Math.min(ctrlx1, ctrlx2)); float top = Math.min(Math.min(y1, y2), Math.min(ctrly1, ctrly2)); float right = Math.max(Math.max(x1, x2), Math.max(ctrlx1, ctrlx2)); float bottom = Math.max(Math.max(y1, y2), Math.max(ctrly1, ctrly2)); return new Rectangle2D.Float(left, top, right - left, bottom - top); } /* * JDK 1.6 serialVersionUID */ private static final long serialVersionUID = -1272015596714244385L; } /** * A cubic parametric curve segment specified with * {@code double} coordinates. * @since 1.2 */ public static class Double extends CubicCurve2D implements Serializable { /** * The X coordinate of the start point * of the cubic curve segment. * @since 1.2 * @serial */ public double x1; /** * The Y coordinate of the start point * of the cubic curve segment. * @since 1.2 * @serial */ public double y1; /** * The X coordinate of the first control point * of the cubic curve segment. * @since 1.2 * @serial */ public double ctrlx1; /** * The Y coordinate of the first control point * of the cubic curve segment. * @since 1.2 * @serial */ public double ctrly1; /** * The X coordinate of the second control point * of the cubic curve segment. * @since 1.2 * @serial */ public double ctrlx2; /** * The Y coordinate of the second control point * of the cubic curve segment. * @since 1.2 * @serial */ public double ctrly2; /** * The X coordinate of the end point * of the cubic curve segment. * @since 1.2 * @serial */ public double x2; /** * The Y coordinate of the end point * of the cubic curve segment. * @since 1.2 * @serial */ public double y2; /** * Constructs and initializes a CubicCurve with coordinates * (0, 0, 0, 0, 0, 0, 0, 0). * @since 1.2 */ public Double() { } /** * Constructs and initializes a {@code CubicCurve2D} from * the specified {@code double} coordinates. * * @param x1 the X coordinate for the start point * of the resulting {@code CubicCurve2D} * @param y1 the Y coordinate for the start point * of the resulting {@code CubicCurve2D} * @param ctrlx1 the X coordinate for the first control point * of the resulting {@code CubicCurve2D} * @param ctrly1 the Y coordinate for the first control point * of the resulting {@code CubicCurve2D} * @param ctrlx2 the X coordinate for the second control point * of the resulting {@code CubicCurve2D} * @param ctrly2 the Y coordinate for the second control point * of the resulting {@code CubicCurve2D} * @param x2 the X coordinate for the end point * of the resulting {@code CubicCurve2D} * @param y2 the Y coordinate for the end point * of the resulting {@code CubicCurve2D} * @since 1.2 */ public Double(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2) { setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2); } /** * {@inheritDoc} * @since 1.2 */ public double getX1() { return x1; } /** * {@inheritDoc} * @since 1.2 */ public double getY1() { return y1; } /** * {@inheritDoc} * @since 1.2 */ public Point2D getP1() { return new Point2D.Double(x1, y1); } /** * {@inheritDoc} * @since 1.2 */ public double getCtrlX1() { return ctrlx1; } /** * {@inheritDoc} * @since 1.2 */ public double getCtrlY1() { return ctrly1; } /** * {@inheritDoc} * @since 1.2 */ public Point2D getCtrlP1() { return new Point2D.Double(ctrlx1, ctrly1); } /** * {@inheritDoc} * @since 1.2 */ public double getCtrlX2() { return ctrlx2; } /** * {@inheritDoc} * @since 1.2 */ public double getCtrlY2() { return ctrly2; } /** * {@inheritDoc} * @since 1.2 */ public Point2D getCtrlP2() { return new Point2D.Double(ctrlx2, ctrly2); } /** * {@inheritDoc} * @since 1.2 */ public double getX2() { return x2; } /** * {@inheritDoc} * @since 1.2 */ public double getY2() { return y2; } /** * {@inheritDoc} * @since 1.2 */ public Point2D getP2() { return new Point2D.Double(x2, y2); } /** * {@inheritDoc} * @since 1.2 */ public void setCurve(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2) { this.x1 = x1; this.y1 = y1; this.ctrlx1 = ctrlx1; this.ctrly1 = ctrly1; this.ctrlx2 = ctrlx2; this.ctrly2 = ctrly2; this.x2 = x2; this.y2 = y2; } /** * {@inheritDoc} * @since 1.2 */ public Rectangle2D getBounds2D() { double left = Math.min(Math.min(x1, x2), Math.min(ctrlx1, ctrlx2)); double top = Math.min(Math.min(y1, y2), Math.min(ctrly1, ctrly2)); double right = Math.max(Math.max(x1, x2), Math.max(ctrlx1, ctrlx2)); double bottom = Math.max(Math.max(y1, y2), Math.max(ctrly1, ctrly2)); return new Rectangle2D.Double(left, top, right - left, bottom - top); } /* * JDK 1.6 serialVersionUID */ private static final long serialVersionUID = -4202960122839707295L; } /** * This is an abstract class that cannot be instantiated directly. * Type-specific implementation subclasses are available for * instantiation and provide a number of formats for storing * the information necessary to satisfy the various accessor * methods below. * * @see java.awt.geom.CubicCurve2D.Float * @see java.awt.geom.CubicCurve2D.Double * @since 1.2 */ protected CubicCurve2D() { } /** * Returns the X coordinate of the start point in double precision. * @return the X coordinate of the start point of the * {@code CubicCurve2D}. * @since 1.2 */ public abstract double getX1(); /** * Returns the Y coordinate of the start point in double precision. * @return the Y coordinate of the start point of the * {@code CubicCurve2D}. * @since 1.2 */ public abstract double getY1(); /** * Returns the start point. * @return a {@code Point2D} that is the start point of * the {@code CubicCurve2D}. * @since 1.2 */ public abstract Point2D getP1(); /** * Returns the X coordinate of the first control point in double precision. * @return the X coordinate of the first control point of the * {@code CubicCurve2D}. * @since 1.2 */ public abstract double getCtrlX1(); /** * Returns the Y coordinate of the first control point in double precision. * @return the Y coordinate of the first control point of the * {@code CubicCurve2D}. * @since 1.2 */ public abstract double getCtrlY1(); /** * Returns the first control point. * @return a {@code Point2D} that is the first control point of * the {@code CubicCurve2D}. * @since 1.2 */ public abstract Point2D getCtrlP1(); /** * Returns the X coordinate of the second control point * in double precision. * @return the X coordinate of the second control point of the * {@code CubicCurve2D}. * @since 1.2 */ public abstract double getCtrlX2(); /** * Returns the Y coordinate of the second control point * in double precision. * @return the Y coordinate of the second control point of the * {@code CubicCurve2D}. * @since 1.2 */ public abstract double getCtrlY2(); /** * Returns the second control point. * @return a {@code Point2D} that is the second control point of * the {@code CubicCurve2D}. * @since 1.2 */ public abstract Point2D getCtrlP2(); /** * Returns the X coordinate of the end point in double precision. * @return the X coordinate of the end point of the * {@code CubicCurve2D}. * @since 1.2 */ public abstract double getX2(); /** * Returns the Y coordinate of the end point in double precision. * @return the Y coordinate of the end point of the * {@code CubicCurve2D}. * @since 1.2 */ public abstract double getY2(); /** * Returns the end point. * @return a {@code Point2D} that is the end point of * the {@code CubicCurve2D}. * @since 1.2 */ public abstract Point2D getP2(); /** * Sets the location of the end points and control points of this curve * to the specified double coordinates. * * @param x1 the X coordinate used to set the start point * of this {@code CubicCurve2D} * @param y1 the Y coordinate used to set the start point * of this {@code CubicCurve2D} * @param ctrlx1 the X coordinate used to set the first control point * of this {@code CubicCurve2D} * @param ctrly1 the Y coordinate used to set the first control point * of this {@code CubicCurve2D} * @param ctrlx2 the X coordinate used to set the second control point * of this {@code CubicCurve2D} * @param ctrly2 the Y coordinate used to set the second control point * of this {@code CubicCurve2D} * @param x2 the X coordinate used to set the end point * of this {@code CubicCurve2D} * @param y2 the Y coordinate used to set the end point * of this {@code CubicCurve2D} * @since 1.2 */ public abstract void setCurve(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2); /** * Sets the location of the end points and control points of this curve * to the double coordinates at the specified offset in the specified * array. * @param coords a double array containing coordinates * @param offset the index of <code>coords from which to begin * setting the end points and control points of this curve * to the coordinates contained in <code>coords * @since 1.2 */ public void setCurve(double[] coords, int offset) { setCurve(coords[offset + 0], coords[offset + 1], coords[offset + 2], coords[offset + 3], coords[offset + 4], coords[offset + 5], coords[offset + 6], coords[offset + 7]); } /** * Sets the location of the end points and control points of this curve * to the specified <code>Point2D coordinates. * @param p1 the first specified <code>Point2D used to set the * start point of this curve * @param cp1 the second specified <code>Point2D used to set the * first control point of this curve * @param cp2 the third specified <code>Point2D used to set the * second control point of this curve * @param p2 the fourth specified <code>Point2D used to set the * end point of this curve * @since 1.2 */ public void setCurve(Point2D p1, Point2D cp1, Point2D cp2, Point2D p2) { setCurve(p1.getX(), p1.getY(), cp1.getX(), cp1.getY(), cp2.getX(), cp2.getY(), p2.getX(), p2.getY()); } /** * Sets the location of the end points and control points of this curve * to the coordinates of the <code>Point2D objects at the specified * offset in the specified array. * @param pts an array of <code>Point2D objects * @param offset the index of <code>pts from which to begin setting * the end points and control points of this curve to the * points contained in <code>pts * @since 1.2 */ public void setCurve(Point2D[] pts, int offset) { setCurve(pts[offset + 0].getX(), pts[offset + 0].getY(), pts[offset + 1].getX(), pts[offset + 1].getY(), pts[offset + 2].getX(), pts[offset + 2].getY(), pts[offset + 3].getX(), pts[offset + 3].getY()); } /** * Sets the location of the end points and control points of this curve * to the same as those in the specified <code>CubicCurve2D. * @param c the specified <code>CubicCurve2D * @since 1.2 */ public void setCurve(CubicCurve2D c) { setCurve(c.getX1(), c.getY1(), c.getCtrlX1(), c.getCtrlY1(), c.getCtrlX2(), c.getCtrlY2(), c.getX2(), c.getY2()); } /** * Returns the square of the flatness of the cubic curve specified * by the indicated control points. The flatness is the maximum distance * of a control point from the line connecting the end points. * * @param x1 the X coordinate that specifies the start point * of a {@code CubicCurve2D} * @param y1 the Y coordinate that specifies the start point * of a {@code CubicCurve2D} * @param ctrlx1 the X coordinate that specifies the first control point * of a {@code CubicCurve2D} * @param ctrly1 the Y coordinate that specifies the first control point * of a {@code CubicCurve2D} * @param ctrlx2 the X coordinate that specifies the second control point * of a {@code CubicCurve2D} * @param ctrly2 the Y coordinate that specifies the second control point * of a {@code CubicCurve2D} * @param x2 the X coordinate that specifies the end point * of a {@code CubicCurve2D} * @param y2 the Y coordinate that specifies the end point * of a {@code CubicCurve2D} * @return the square of the flatness of the {@code CubicCurve2D} * represented by the specified coordinates. * @since 1.2 */ public static double getFlatnessSq(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2) { return Math.max(Line2D.ptSegDistSq(x1, y1, x2, y2, ctrlx1, ctrly1), Line2D.ptSegDistSq(x1, y1, x2, y2, ctrlx2, ctrly2)); } /** * Returns the flatness of the cubic curve specified * by the indicated control points. The flatness is the maximum distance * of a control point from the line connecting the end points. * * @param x1 the X coordinate that specifies the start point * of a {@code CubicCurve2D} * @param y1 the Y coordinate that specifies the start point * of a {@code CubicCurve2D} * @param ctrlx1 the X coordinate that specifies the first control point * of a {@code CubicCurve2D} * @param ctrly1 the Y coordinate that specifies the first control point * of a {@code CubicCurve2D} * @param ctrlx2 the X coordinate that specifies the second control point * of a {@code CubicCurve2D} * @param ctrly2 the Y coordinate that specifies the second control point * of a {@code CubicCurve2D} * @param x2 the X coordinate that specifies the end point * of a {@code CubicCurve2D} * @param y2 the Y coordinate that specifies the end point * of a {@code CubicCurve2D} * @return the flatness of the {@code CubicCurve2D} * represented by the specified coordinates. * @since 1.2 */ public static double getFlatness(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2) { return Math.sqrt(getFlatnessSq(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2)); } /** * Returns the square of the flatness of the cubic curve specified * by the control points stored in the indicated array at the * indicated index. The flatness is the maximum distance * of a control point from the line connecting the end points. * @param coords an array containing coordinates * @param offset the index of <code>coords from which to begin * getting the end points and control points of the curve * @return the square of the flatness of the <code>CubicCurve2D * specified by the coordinates in <code>coords at * the specified offset. * @since 1.2 */ public static double getFlatnessSq(double coords[], int offset) { return getFlatnessSq(coords[offset + 0], coords[offset + 1], coords[offset + 2], coords[offset + 3], coords[offset + 4], coords[offset + 5], coords[offset + 6], coords[offset + 7]); } /** * Returns the flatness of the cubic curve specified * by the control points stored in the indicated array at the * indicated index. The flatness is the maximum distance * of a control point from the line connecting the end points. * @param coords an array containing coordinates * @param offset the index of <code>coords from which to begin * getting the end points and control points of the curve * @return the flatness of the <code>CubicCurve2D * specified by the coordinates in <code>coords at * the specified offset. * @since 1.2 */ public static double getFlatness(double coords[], int offset) { return getFlatness(coords[offset + 0], coords[offset + 1], coords[offset + 2], coords[offset + 3], coords[offset + 4], coords[offset + 5], coords[offset + 6], coords[offset + 7]); } /** * Returns the square of the flatness of this curve. The flatness is the * maximum distance of a control point from the line connecting the * end points. * @return the square of the flatness of this curve. * @since 1.2 */ public double getFlatnessSq() { return getFlatnessSq(getX1(), getY1(), getCtrlX1(), getCtrlY1(), getCtrlX2(), getCtrlY2(), getX2(), getY2()); } /** * Returns the flatness of this curve. The flatness is the * maximum distance of a control point from the line connecting the * end points. * @return the flatness of this curve. * @since 1.2 */ public double getFlatness() { return getFlatness(getX1(), getY1(), getCtrlX1(), getCtrlY1(), getCtrlX2(), getCtrlY2(), getX2(), getY2()); } /** * Subdivides this cubic curve and stores the resulting two * subdivided curves into the left and right curve parameters. * Either or both of the left and right objects may be the same * as this object or null. * @param left the cubic curve object for storing for the left or * first half of the subdivided curve * @param right the cubic curve object for storing for the right or * second half of the subdivided curve * @since 1.2 */ public void subdivide(CubicCurve2D left, CubicCurve2D right) { subdivide(this, left, right); } /** * Subdivides the cubic curve specified by the <code>src parameter * and stores the resulting two subdivided curves into the * <code>left and Other Java examples (source code examples)Here is a short list of links related to this Java CubicCurve2D.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.