jdk/src/share/classes/java/awt/geom/CubicCurve2D.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 9035 1255eb81cc2f
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8129
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.geom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.awt.geom.Curve;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
    34
import static java.lang.Math.abs;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
    35
import static java.lang.Math.max;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
    36
import static java.lang.Math.ulp;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
    37
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The <code>CubicCurve2D</code> class defines a cubic parametric curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * segment in {@code (x,y)} coordinate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class is only the abstract superclass for all objects which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * store a 2D cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The actual storage representation of the coordinates is left to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public abstract class CubicCurve2D implements Shape, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * A cubic parametric curve segment specified with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * {@code float} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static class Float extends CubicCurve2D implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         * The X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public float x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * The Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        public float y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * The X coordinate of the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        public float ctrlx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
         * The Y coordinate of the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public float ctrly1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         * The X coordinate of the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public float ctrlx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * The Y coordinate of the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        public float ctrly2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * The X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        public float x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * The Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public float y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * Constructs and initializes a CubicCurve with coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * (0, 0, 0, 0, 0, 0, 0, 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public Float() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         * Constructs and initializes a {@code CubicCurve2D} from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * the specified {@code float} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * @param x1 the X coordinate for the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         *           of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * @param y1 the Y coordinate for the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         *           of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         * @param ctrlx1 the X coordinate for the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         *               of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * @param ctrly1 the Y coordinate for the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         *               of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * @param ctrlx2 the X coordinate for the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         *               of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * @param ctrly2 the Y coordinate for the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         *               of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * @param x2 the X coordinate for the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         *           of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         * @param y2 the Y coordinate for the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         *           of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public Float(float x1, float y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                     float ctrlx1, float ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                     float ctrlx2, float ctrly2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                     float x2, float y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public double getX1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return (double) x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        public double getY1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return (double) y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        public Point2D getP1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return new Point2D.Float(x1, y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        public double getCtrlX1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return (double) ctrlx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        public double getCtrlY1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return (double) ctrly1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        public Point2D getCtrlP1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return new Point2D.Float(ctrlx1, ctrly1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        public double getCtrlX2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return (double) ctrlx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        public double getCtrlY2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return (double) ctrly2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        public Point2D getCtrlP2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return new Point2D.Float(ctrlx2, ctrly2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public double getX2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return (double) x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        public double getY2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return (double) y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        public Point2D getP2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return new Point2D.Float(x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        public void setCurve(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                             double ctrlx1, double ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                             double ctrlx2, double ctrly2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                             double x2, double y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            this.x1     = (float) x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            this.y1     = (float) y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            this.ctrlx1 = (float) ctrlx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            this.ctrly1 = (float) ctrly1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            this.ctrlx2 = (float) ctrlx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            this.ctrly2 = (float) ctrly2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            this.x2     = (float) x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            this.y2     = (float) y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         * Sets the location of the end points and control points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * of this curve to the specified {@code float} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * @param x1 the X coordinate used to set the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         *           of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         * @param y1 the Y coordinate used to set the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         *           of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * @param ctrlx1 the X coordinate used to set the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         *               of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * @param ctrly1 the Y coordinate used to set the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         *               of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * @param ctrlx2 the X coordinate used to set the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         *               of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         * @param ctrly2 the Y coordinate used to set the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         *               of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         * @param x2 the X coordinate used to set the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         *           of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * @param y2 the Y coordinate used to set the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         *           of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        public void setCurve(float x1, float y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                             float ctrlx1, float ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                             float ctrlx2, float ctrly2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                             float x2, float y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            this.x1     = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            this.y1     = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            this.ctrlx1 = ctrlx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            this.ctrly1 = ctrly1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            this.ctrlx2 = ctrlx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            this.ctrly2 = ctrly2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            this.x2     = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            this.y2     = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            float left   = Math.min(Math.min(x1, x2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                    Math.min(ctrlx1, ctrlx2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            float top    = Math.min(Math.min(y1, y2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                    Math.min(ctrly1, ctrly2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            float right  = Math.max(Math.max(x1, x2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                    Math.max(ctrlx1, ctrlx2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            float bottom = Math.max(Math.max(y1, y2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                    Math.max(ctrly1, ctrly2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return new Rectangle2D.Float(left, top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                         right - left, bottom - top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * JDK 1.6 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        private static final long serialVersionUID = -1272015596714244385L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * A cubic parametric curve segment specified with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * {@code double} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public static class Double extends CubicCurve2D implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * The X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        public double x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * The Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public double y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * The X coordinate of the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        public double ctrlx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * The Y coordinate of the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        public double ctrly1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         * The X coordinate of the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        public double ctrlx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * The Y coordinate of the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        public double ctrly2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * The X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        public double x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * The Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * of the cubic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        public double y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * Constructs and initializes a CubicCurve with coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * (0, 0, 0, 0, 0, 0, 0, 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        public Double() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * Constructs and initializes a {@code CubicCurve2D} from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         * the specified {@code double} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         * @param x1 the X coordinate for the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         *           of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
         * @param y1 the Y coordinate for the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         *           of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * @param ctrlx1 the X coordinate for the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         *               of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         * @param ctrly1 the Y coordinate for the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         *               of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * @param ctrlx2 the X coordinate for the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         *               of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * @param ctrly2 the Y coordinate for the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         *               of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * @param x2 the X coordinate for the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         *           of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         * @param y2 the Y coordinate for the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         *           of the resulting {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        public Double(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                      double ctrlx1, double ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                      double ctrlx2, double ctrly2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                      double x2, double y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        public double getX1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            return x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        public double getY1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        public Point2D getP1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            return new Point2D.Double(x1, y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        public double getCtrlX1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            return ctrlx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        public double getCtrlY1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return ctrly1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        public Point2D getCtrlP1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return new Point2D.Double(ctrlx1, ctrly1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        public double getCtrlX2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return ctrlx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        public double getCtrlY2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return ctrly2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        public Point2D getCtrlP2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            return new Point2D.Double(ctrlx2, ctrly2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        public double getX2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            return x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        public double getY2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            return y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        public Point2D getP2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return new Point2D.Double(x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        public void setCurve(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                             double ctrlx1, double ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                             double ctrlx2, double ctrly2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                             double x2, double y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            this.x1     = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            this.y1     = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            this.ctrlx1 = ctrlx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            this.ctrly1 = ctrly1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            this.ctrlx2 = ctrlx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            this.ctrly2 = ctrly2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            this.x2     = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            this.y2     = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            double left   = Math.min(Math.min(x1, x2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                     Math.min(ctrlx1, ctrlx2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            double top    = Math.min(Math.min(y1, y2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                                     Math.min(ctrly1, ctrly2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            double right  = Math.max(Math.max(x1, x2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                                     Math.max(ctrlx1, ctrlx2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            double bottom = Math.max(Math.max(y1, y2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                                     Math.max(ctrly1, ctrly2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            return new Rectangle2D.Double(left, top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                          right - left, bottom - top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         * JDK 1.6 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        private static final long serialVersionUID = -4202960122839707295L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * This is an abstract class that cannot be instantiated directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Type-specific implementation subclasses are available for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * instantiation and provide a number of formats for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * the information necessary to satisfy the various accessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * methods below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @see java.awt.geom.CubicCurve2D.Float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @see java.awt.geom.CubicCurve2D.Double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    protected CubicCurve2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Returns the X coordinate of the start point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @return the X coordinate of the start point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *         {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    public abstract double getX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Returns the Y coordinate of the start point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return the Y coordinate of the start point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *         {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public abstract double getY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Returns the start point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @return a {@code Point2D} that is the start point of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *         the {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public abstract Point2D getP1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Returns the X coordinate of the first control point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @return the X coordinate of the first control point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *         {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    public abstract double getCtrlX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Returns the Y coordinate of the first control point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @return the Y coordinate of the first control point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *         {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public abstract double getCtrlY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Returns the first control point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return a {@code Point2D} that is the first control point of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *         the {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    public abstract Point2D getCtrlP1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Returns the X coordinate of the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @return the X coordinate of the second control point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *         {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public abstract double getCtrlX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * Returns the Y coordinate of the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @return the Y coordinate of the second control point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *         {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    public abstract double getCtrlY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * Returns the second control point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return a {@code Point2D} that is the second control point of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *         the {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    public abstract Point2D getCtrlP2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * Returns the X coordinate of the end point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @return the X coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *         {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public abstract double getX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Returns the Y coordinate of the end point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @return the Y coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *         {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public abstract double getY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Returns the end point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @return a {@code Point2D} that is the end point of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *         the {@code CubicCurve2D}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public abstract Point2D getP2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * Sets the location of the end points and control points of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * to the specified double coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @param x1 the X coordinate used to set the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *           of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @param y1 the Y coordinate used to set the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *           of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @param ctrlx1 the X coordinate used to set the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *               of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @param ctrly1 the Y coordinate used to set the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *               of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @param ctrlx2 the X coordinate used to set the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *               of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @param ctrly2 the Y coordinate used to set the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *               of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @param x2 the X coordinate used to set the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *           of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @param y2 the Y coordinate used to set the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *           of this {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public abstract void setCurve(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                                  double ctrlx1, double ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                                  double ctrlx2, double ctrly2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                                  double x2, double y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * Sets the location of the end points and control points of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * to the double coordinates at the specified offset in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @param coords a double array containing coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @param offset the index of <code>coords</code> from which to begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *          setting the end points and control points of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *          to the coordinates contained in <code>coords</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    public void setCurve(double[] coords, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        setCurve(coords[offset + 0], coords[offset + 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                 coords[offset + 2], coords[offset + 3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                 coords[offset + 4], coords[offset + 5],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                 coords[offset + 6], coords[offset + 7]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Sets the location of the end points and control points of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * to the specified <code>Point2D</code> coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @param p1 the first specified <code>Point2D</code> used to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *          start point of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @param cp1 the second specified <code>Point2D</code> used to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *          first control point of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param cp2 the third specified <code>Point2D</code> used to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *          second control point of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @param p2 the fourth specified <code>Point2D</code> used to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *          end point of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    public void setCurve(Point2D p1, Point2D cp1, Point2D cp2, Point2D p2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        setCurve(p1.getX(), p1.getY(), cp1.getX(), cp1.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                 cp2.getX(), cp2.getY(), p2.getX(), p2.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * Sets the location of the end points and control points of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * to the coordinates of the <code>Point2D</code> objects at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * offset in the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @param pts an array of <code>Point2D</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @param offset  the index of <code>pts</code> from which to begin setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *          the end points and control points of this curve to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *          points contained in <code>pts</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    public void setCurve(Point2D[] pts, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        setCurve(pts[offset + 0].getX(), pts[offset + 0].getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                 pts[offset + 1].getX(), pts[offset + 1].getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                 pts[offset + 2].getX(), pts[offset + 2].getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                 pts[offset + 3].getX(), pts[offset + 3].getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Sets the location of the end points and control points of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * to the same as those in the specified <code>CubicCurve2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @param c the specified <code>CubicCurve2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public void setCurve(CubicCurve2D c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        setCurve(c.getX1(), c.getY1(), c.getCtrlX1(), c.getCtrlY1(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                 c.getCtrlX2(), c.getCtrlY2(), c.getX2(), c.getY2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Returns the square of the flatness of the cubic curve specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * by the indicated control points. The flatness is the maximum distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * of a control point from the line connecting the end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @param x1 the X coordinate that specifies the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *           of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @param y1 the Y coordinate that specifies the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *           of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @param ctrlx1 the X coordinate that specifies the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *               of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @param ctrly1 the Y coordinate that specifies the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *               of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @param ctrlx2 the X coordinate that specifies the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *               of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @param ctrly2 the Y coordinate that specifies the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *               of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @param x2 the X coordinate that specifies the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *           of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @param y2 the Y coordinate that specifies the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *           of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @return the square of the flatness of the {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *          represented by the specified coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    public static double getFlatnessSq(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                                       double ctrlx1, double ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                                       double ctrlx2, double ctrly2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                                       double x2, double y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        return Math.max(Line2D.ptSegDistSq(x1, y1, x2, y2, ctrlx1, ctrly1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        Line2D.ptSegDistSq(x1, y1, x2, y2, ctrlx2, ctrly2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Returns the flatness of the cubic curve specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * by the indicated control points. The flatness is the maximum distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * of a control point from the line connecting the end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @param x1 the X coordinate that specifies the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *           of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @param y1 the Y coordinate that specifies the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *           of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @param ctrlx1 the X coordinate that specifies the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *               of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @param ctrly1 the Y coordinate that specifies the first control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *               of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @param ctrlx2 the X coordinate that specifies the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *               of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @param ctrly2 the Y coordinate that specifies the second control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *               of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @param x2 the X coordinate that specifies the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *           of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @param y2 the Y coordinate that specifies the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *           of a {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @return the flatness of the {@code CubicCurve2D}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *          represented by the specified coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    public static double getFlatness(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                                     double ctrlx1, double ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                     double ctrlx2, double ctrly2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                     double x2, double y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        return Math.sqrt(getFlatnessSq(x1, y1, ctrlx1, ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                       ctrlx2, ctrly2, x2, y2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Returns the square of the flatness of the cubic curve specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * by the control points stored in the indicated array at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * indicated index. The flatness is the maximum distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * of a control point from the line connecting the end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @param coords an array containing coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @param offset the index of <code>coords</code> from which to begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *          getting the end points and control points of the curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @return the square of the flatness of the <code>CubicCurve2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *          specified by the coordinates in <code>coords</code> at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *          the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    public static double getFlatnessSq(double coords[], int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        return getFlatnessSq(coords[offset + 0], coords[offset + 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                             coords[offset + 2], coords[offset + 3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                             coords[offset + 4], coords[offset + 5],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                             coords[offset + 6], coords[offset + 7]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Returns the flatness of the cubic curve specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * by the control points stored in the indicated array at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * indicated index.  The flatness is the maximum distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * of a control point from the line connecting the end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param coords an array containing coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @param offset the index of <code>coords</code> from which to begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *          getting the end points and control points of the curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @return the flatness of the <code>CubicCurve2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *          specified by the coordinates in <code>coords</code> at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *          the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    public static double getFlatness(double coords[], int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        return getFlatness(coords[offset + 0], coords[offset + 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                           coords[offset + 2], coords[offset + 3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                           coords[offset + 4], coords[offset + 5],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                           coords[offset + 6], coords[offset + 7]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Returns the square of the flatness of this curve.  The flatness is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * maximum distance of a control point from the line connecting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @return the square of the flatness of this curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    public double getFlatnessSq() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        return getFlatnessSq(getX1(), getY1(), getCtrlX1(), getCtrlY1(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                             getCtrlX2(), getCtrlY2(), getX2(), getY2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * Returns the flatness of this curve.  The flatness is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * maximum distance of a control point from the line connecting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @return the flatness of this curve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    public double getFlatness() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        return getFlatness(getX1(), getY1(), getCtrlX1(), getCtrlY1(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                           getCtrlX2(), getCtrlY2(), getX2(), getY2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * Subdivides this cubic curve and stores the resulting two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * subdivided curves into the left and right curve parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * Either or both of the left and right objects may be the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * as this object or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @param left the cubic curve object for storing for the left or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * first half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @param right the cubic curve object for storing for the right or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * second half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    public void subdivide(CubicCurve2D left, CubicCurve2D right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        subdivide(this, left, right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Subdivides the cubic curve specified by the <code>src</code> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * and stores the resulting two subdivided curves into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * <code>left</code> and <code>right</code> curve parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * Either or both of the <code>left</code> and <code>right</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * may be the same as the <code>src</code> object or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * @param src the cubic curve to be subdivided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @param left the cubic curve object for storing the left or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * first half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @param right the cubic curve object for storing the right or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * second half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    public static void subdivide(CubicCurve2D src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                                 CubicCurve2D left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                                 CubicCurve2D right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        double x1 = src.getX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        double y1 = src.getY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        double ctrlx1 = src.getCtrlX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        double ctrly1 = src.getCtrlY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        double ctrlx2 = src.getCtrlX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        double ctrly2 = src.getCtrlY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        double x2 = src.getX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        double y2 = src.getY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        double centerx = (ctrlx1 + ctrlx2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        double centery = (ctrly1 + ctrly2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        ctrlx1 = (x1 + ctrlx1) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        ctrly1 = (y1 + ctrly1) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        ctrlx2 = (x2 + ctrlx2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        ctrly2 = (y2 + ctrly2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        double ctrlx12 = (ctrlx1 + centerx) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        double ctrly12 = (ctrly1 + centery) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        double ctrlx21 = (ctrlx2 + centerx) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        double ctrly21 = (ctrly2 + centery) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        centerx = (ctrlx12 + ctrlx21) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        centery = (ctrly12 + ctrly21) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        if (left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            left.setCurve(x1, y1, ctrlx1, ctrly1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                          ctrlx12, ctrly12, centerx, centery);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        if (right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            right.setCurve(centerx, centery, ctrlx21, ctrly21,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                           ctrlx2, ctrly2, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * Subdivides the cubic curve specified by the coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * stored in the <code>src</code> array at indices <code>srcoff</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * through (<code>srcoff</code>&nbsp;+&nbsp;7) and stores the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * resulting two subdivided curves into the two result arrays at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * corresponding indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * Either or both of the <code>left</code> and <code>right</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * arrays may be <code>null</code> or a reference to the same array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * as the <code>src</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * Note that the last point in the first subdivided curve is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * same as the first point in the second subdivided curve. Thus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * it is possible to pass the same array for <code>left</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * and <code>right</code> and to use offsets, such as <code>rightoff</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * equals (<code>leftoff</code> + 6), in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * to avoid allocating extra storage for this common point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @param src the array holding the coordinates for the source curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @param srcoff the offset into the array of the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * the 6 source coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @param left the array for storing the coordinates for the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * @param leftoff the offset into the array of the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * the 6 left coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @param right the array for storing the coordinates for the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @param rightoff the offset into the array of the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * the 6 right coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    public static void subdivide(double src[], int srcoff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                                 double left[], int leftoff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                                 double right[], int rightoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        double x1 = src[srcoff + 0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        double y1 = src[srcoff + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        double ctrlx1 = src[srcoff + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        double ctrly1 = src[srcoff + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        double ctrlx2 = src[srcoff + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        double ctrly2 = src[srcoff + 5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        double x2 = src[srcoff + 6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        double y2 = src[srcoff + 7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        if (left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            left[leftoff + 0] = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            left[leftoff + 1] = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        if (right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            right[rightoff + 6] = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            right[rightoff + 7] = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        x1 = (x1 + ctrlx1) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        y1 = (y1 + ctrly1) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        x2 = (x2 + ctrlx2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        y2 = (y2 + ctrly2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        double centerx = (ctrlx1 + ctrlx2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        double centery = (ctrly1 + ctrly2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        ctrlx1 = (x1 + centerx) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        ctrly1 = (y1 + centery) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        ctrlx2 = (x2 + centerx) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        ctrly2 = (y2 + centery) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        centerx = (ctrlx1 + ctrlx2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        centery = (ctrly1 + ctrly2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        if (left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            left[leftoff + 2] = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            left[leftoff + 3] = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            left[leftoff + 4] = ctrlx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            left[leftoff + 5] = ctrly1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            left[leftoff + 6] = centerx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            left[leftoff + 7] = centery;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if (right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            right[rightoff + 0] = centerx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            right[rightoff + 1] = centery;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            right[rightoff + 2] = ctrlx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            right[rightoff + 3] = ctrly2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            right[rightoff + 4] = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            right[rightoff + 5] = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * Solves the cubic whose coefficients are in the <code>eqn</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * array and places the non-complex roots back into the same array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * returning the number of roots.  The solved cubic is represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * by the equation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *     eqn = {c, b, a, d}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *     dx^3 + ax^2 + bx + c = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * A return value of -1 is used to distinguish a constant equation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * that might be always 0 or never 0 from an equation that has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * zeroes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @param eqn an array containing coefficients for a cubic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @return the number of roots, or -1 if the equation is a constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public static int solveCubic(double eqn[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        return solveCubic(eqn, eqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * Solve the cubic whose coefficients are in the <code>eqn</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * array and place the non-complex roots into the <code>res</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * array, returning the number of roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * The cubic solved is represented by the equation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *     eqn = {c, b, a, d}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *     dx^3 + ax^2 + bx + c = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * A return value of -1 is used to distinguish a constant equation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * which may be always 0 or never 0, from an equation which has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * zeroes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @param eqn the specified array of coefficients to use to solve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *        the cubic equation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @param res the array that contains the non-complex roots
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *        resulting from the solution of the cubic equation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * @return the number of roots, or -1 if the equation is a constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    public static int solveCubic(double eqn[], double res[]) {
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1090
        // From Graphics Gems:
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1091
        // http://tog.acm.org/resources/GraphicsGems/gems/Roots3And4.c
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1092
        final double d = eqn[3];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1093
        if (d == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            return QuadCurve2D.solveQuadratic(eqn, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        }
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1096
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1097
        /* normal form: x^3 + Ax^2 + Bx + C = 0 */
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1098
        final double A = eqn[2] / d;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1099
        final double B = eqn[1] / d;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1100
        final double C = eqn[0] / d;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1101
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1102
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1103
        //  substitute x = y - A/3 to eliminate quadratic term:
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1104
        //     x^3 +Px + Q = 0
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1105
        //
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1106
        // Since we actually need P/3 and Q/2 for all of the
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1107
        // calculations that follow, we will calculate
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1108
        // p = P/3
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1109
        // q = Q/2
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1110
        // instead and use those values for simplicity of the code.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1111
        double sq_A = A * A;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1112
        double p = 1.0/3 * (-1.0/3 * sq_A + B);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1113
        double q = 1.0/2 * (2.0/27 * A * sq_A - 1.0/3 * A * B + C);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1114
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1115
        /* use Cardano's formula */
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1116
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1117
        double cb_p = p * p * p;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1118
        double D = q * q + cb_p;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1119
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1120
        final double sub = 1.0/3 * A;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1121
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1122
        int num;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1123
        if (D < 0) { /* Casus irreducibilis: three real solutions */
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1124
            // see: http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1125
            double phi = 1.0/3 * Math.acos(-q / Math.sqrt(-cb_p));
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1126
            double t = 2 * Math.sqrt(-p);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1127
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            if (res == eqn) {
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1129
                eqn = Arrays.copyOf(eqn, 4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            }
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1131
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1132
            res[ 0 ] =  ( t * Math.cos(phi));
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1133
            res[ 1 ] =  (-t * Math.cos(phi + Math.PI / 3));
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1134
            res[ 2 ] =  (-t * Math.cos(phi - Math.PI / 3));
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1135
            num = 3;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1136
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1137
            for (int i = 0; i < num; ++i) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1138
                res[ i ] -= sub;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1139
            }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1140
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        } else {
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1142
            // Please see the comment in fixRoots marked 'XXX' before changing
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1143
            // any of the code in this case.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1144
            double sqrt_D = Math.sqrt(D);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1145
            double u = Math.cbrt(sqrt_D - q);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1146
            double v = - Math.cbrt(sqrt_D + q);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1147
            double uv = u+v;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1148
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1149
            num = 1;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1150
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1151
            double err = 1200000000*ulp(abs(uv) + abs(sub));
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1152
            if (iszero(D, err) || within(u, v, err)) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1153
                if (res == eqn) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1154
                    eqn = Arrays.copyOf(eqn, 4);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1155
                }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1156
                res[1] = -(uv / 2) - sub;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1157
                num = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            }
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1159
            // this must be done after the potential Arrays.copyOf
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1160
            res[ 0 ] =  uv - sub;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1161
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1162
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1163
        if (num > 1) { // num == 3 || num == 2
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1164
            num = fixRoots(eqn, res, num);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1166
        if (num > 2 && (res[2] == res[1] || res[2] == res[0])) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1167
            num--;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1168
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1169
        if (num > 1 && res[1] == res[0]) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1170
            res[1] = res[--num]; // Copies res[2] to res[1] if needed
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1171
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1172
        return num;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1175
    // preconditions: eqn != res && eqn[3] != 0 && num > 1
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1176
    // This method tries to improve the accuracy of the roots of eqn (which
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1177
    // should be in res). It also might eliminate roots in res if it decideds
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1178
    // that they're not real roots. It will not check for roots that the
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1179
    // computation of res might have missed, so this method should only be
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1180
    // used when the roots in res have been computed using an algorithm
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1181
    // that never underestimates the number of roots (such as solveCubic above)
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1182
    private static int fixRoots(double[] eqn, double[] res, int num) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1183
        double[] intervals = {eqn[1], 2*eqn[2], 3*eqn[3]};
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1184
        int critCount = QuadCurve2D.solveQuadratic(intervals, intervals);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1185
        if (critCount == 2 && intervals[0] == intervals[1]) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1186
            critCount--;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1187
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1188
        if (critCount == 2 && intervals[0] > intervals[1]) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1189
            double tmp = intervals[0];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1190
            intervals[0] = intervals[1];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1191
            intervals[1] = tmp;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1192
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1193
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1194
        // below we use critCount to possibly filter out roots that shouldn't
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1195
        // have been computed. We require that eqn[3] != 0, so eqn is a proper
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1196
        // cubic, which means that its limits at -/+inf are -/+inf or +/-inf.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1197
        // Therefore, if critCount==2, the curve is shaped like a sideways S,
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1198
        // and it could have 1-3 roots. If critCount==0 it is monotonic, and
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1199
        // if critCount==1 it is monotonic with a single point where it is
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1200
        // flat. In the last 2 cases there can only be 1 root. So in cases
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1201
        // where num > 1 but critCount < 2, we eliminate all roots in res
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1202
        // except one.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1203
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1204
        if (num == 3) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1205
            double xe = getRootUpperBound(eqn);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1206
            double x0 = -xe;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1207
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1208
            Arrays.sort(res, 0, num);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1209
            if (critCount == 2) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1210
                // this just tries to improve the accuracy of the computed
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1211
                // roots using Newton's method.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1212
                res[0] = refineRootWithHint(eqn, x0, intervals[0], res[0]);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1213
                res[1] = refineRootWithHint(eqn, intervals[0], intervals[1], res[1]);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1214
                res[2] = refineRootWithHint(eqn, intervals[1], xe, res[2]);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1215
                return 3;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1216
            } else if (critCount == 1) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1217
                // we only need fx0 and fxe for the sign of the polynomial
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1218
                // at -inf and +inf respectively, so we don't need to do
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1219
                // fx0 = solveEqn(eqn, 3, x0); fxe = solveEqn(eqn, 3, xe)
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1220
                double fxe = eqn[3];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1221
                double fx0 = -fxe;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1222
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1223
                double x1 = intervals[0];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1224
                double fx1 = solveEqn(eqn, 3, x1);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1225
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1226
                // if critCount == 1 or critCount == 0, but num == 3 then
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1227
                // something has gone wrong. This branch and the one below
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1228
                // would ideally never execute, but if they do we can't know
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1229
                // which of the computed roots is closest to the real root;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1230
                // therefore, we can't use refineRootWithHint. But even if
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1231
                // we did know, being here most likely means that the
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1232
                // curve is very flat close to two of the computed roots
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1233
                // (or maybe even all three). This might make Newton's method
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1234
                // fail altogether, which would be a pain to detect and fix.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1235
                // This is why we use a very stable bisection method.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1236
                if (oppositeSigns(fx0, fx1)) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1237
                    res[0] = bisectRootWithHint(eqn, x0, x1, res[0]);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1238
                } else if (oppositeSigns(fx1, fxe)) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1239
                    res[0] = bisectRootWithHint(eqn, x1, xe, res[2]);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1240
                } else /* fx1 must be 0 */ {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1241
                    res[0] = x1;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1242
                }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1243
                // return 1
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1244
            } else if (critCount == 0) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1245
                res[0] = bisectRootWithHint(eqn, x0, xe, res[1]);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1246
                // return 1
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1247
            }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1248
        } else if (num == 2 && critCount == 2) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1249
            // XXX: here we assume that res[0] has better accuracy than res[1].
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1250
            // This is true because this method is only used from solveCubic
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1251
            // which puts in res[0] the root that it would compute anyway even
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1252
            // if num==1. If this method is ever used from any other method, or
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1253
            // if the solveCubic implementation changes, this assumption should
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1254
            // be reevaluated, and the choice of goodRoot might have to become
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1255
            // goodRoot = (abs(eqn'(res[0])) > abs(eqn'(res[1]))) ? res[0] : res[1]
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1256
            // where eqn' is the derivative of eqn.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1257
            double goodRoot = res[0];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1258
            double badRoot = res[1];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1259
            double x1 = intervals[0];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1260
            double x2 = intervals[1];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1261
            // If a cubic curve really has 2 roots, one of those roots must be
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1262
            // at a critical point. That can't be goodRoot, so we compute x to
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1263
            // be the farthest critical point from goodRoot. If there are two
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1264
            // roots, x must be the second one, so we evaluate eqn at x, and if
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1265
            // it is zero (or close enough) we put x in res[1] (or badRoot, if
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1266
            // |solveEqn(eqn, 3, badRoot)| < |solveEqn(eqn, 3, x)| but this
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1267
            // shouldn't happen often).
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1268
            double x = abs(x1 - goodRoot) > abs(x2 - goodRoot) ? x1 : x2;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1269
            double fx = solveEqn(eqn, 3, x);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1270
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1271
            if (iszero(fx, 10000000*ulp(x))) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1272
                double badRootVal = solveEqn(eqn, 3, badRoot);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1273
                res[1] = abs(badRootVal) < abs(fx) ? badRoot : x;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1274
                return 2;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1275
            }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1276
        } // else there can only be one root - goodRoot, and it is already in res[0]
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1277
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1278
        return 1;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1279
    }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1280
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1281
    // use newton's method.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1282
    private static double refineRootWithHint(double[] eqn, double min, double max, double t) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1283
        if (!inInterval(t, min, max)) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1284
            return t;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1285
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1286
        double[] deriv = {eqn[1], 2*eqn[2], 3*eqn[3]};
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1287
        double origt = t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        for (int i = 0; i < 3; i++) {
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1289
            double slope = solveEqn(deriv, 2, t);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1290
            double y = solveEqn(eqn, 3, t);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1291
            double delta = - (y / slope);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1292
            double newt = t + delta;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1293
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1294
            if (slope == 0 || y == 0 || t == newt) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1295
                break;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1296
            }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1297
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1298
            t = newt;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1299
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1300
        if (within(t, origt, 1000*ulp(origt)) && inInterval(t, min, max)) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1301
            return t;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1302
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1303
        return origt;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1304
    }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1305
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1306
    private static double bisectRootWithHint(double[] eqn, double x0, double xe, double hint) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1307
        double delta1 = Math.min(abs(hint - x0) / 64, 0.0625);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1308
        double delta2 = Math.min(abs(hint - xe) / 64, 0.0625);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1309
        double x02 = hint - delta1;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1310
        double xe2 = hint + delta2;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1311
        double fx02 = solveEqn(eqn, 3, x02);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1312
        double fxe2 = solveEqn(eqn, 3, xe2);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1313
        while (oppositeSigns(fx02, fxe2)) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1314
            if (x02 >= xe2) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1315
                return x02;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            }
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1317
            x0 = x02;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1318
            xe = xe2;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1319
            delta1 /= 64;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1320
            delta2 /= 64;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1321
            x02 = hint - delta1;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1322
            xe2 = hint + delta2;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1323
            fx02 = solveEqn(eqn, 3, x02);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1324
            fxe2 = solveEqn(eqn, 3, xe2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        }
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1326
        if (fx02 == 0) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1327
            return x02;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1328
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1329
        if (fxe2 == 0) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1330
            return xe2;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1331
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1332
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1333
        return bisectRoot(eqn, x0, xe);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1334
    }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1335
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1336
    private static double bisectRoot(double[] eqn, double x0, double xe) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1337
        double fx0 = solveEqn(eqn, 3, x0);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1338
        double m = x0 + (xe - x0) / 2;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1339
        while (m != x0 && m != xe) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1340
            double fm = solveEqn(eqn, 3, m);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1341
            if (fm == 0) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1342
                return m;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1343
            }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1344
            if (oppositeSigns(fx0, fm)) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1345
                xe = m;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1346
            } else {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1347
                fx0 = fm;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1348
                x0 = m;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1349
            }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1350
            m = x0 + (xe-x0)/2;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1351
        }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1352
        return m;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1353
    }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1354
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1355
    private static boolean inInterval(double t, double min, double max) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1356
        return min <= t && t <= max;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1357
    }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1358
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1359
    private static boolean within(double x, double y, double err) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1360
        double d = y - x;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1361
        return (d <= err && d >= -err);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1362
    }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1363
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1364
    private static boolean iszero(double x, double err) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1365
        return within(x, 0, err);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1366
    }
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1367
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1368
    private static boolean oppositeSigns(double x1, double x2) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1369
        return (x1 < 0 && x2 > 0) || (x1 > 0 && x2 < 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    private static double solveEqn(double eqn[], int order, double t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        double v = eqn[order];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        while (--order >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            v = v * t + eqn[order];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1380
    /*
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1381
     * Computes M+1 where M is an upper bound for all the roots in of eqn.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1382
     * See: http://en.wikipedia.org/wiki/Sturm%27s_theorem#Applications.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1383
     * The above link doesn't contain a proof, but I [dlila] proved it myself
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1384
     * so the result is reliable. The proof isn't difficult, but it's a bit
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1385
     * long to include here.
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1386
     * Precondition: eqn must represent a cubic polynomial
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1387
     */
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1388
    private static double getRootUpperBound(double[] eqn) {
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1389
        double d = eqn[3];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1390
        double a = eqn[2];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1391
        double b = eqn[1];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1392
        double c = eqn[0];
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1393
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1394
        double M = 1 + max(max(abs(a), abs(b)), abs(c)) / abs(d);
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1395
        M += ulp(M) + 1;
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1396
        return M;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
8129
54c848224b7b 4645692: solveCubic does not return all solutions.
dlila
parents: 7942
diff changeset
  1399
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    public boolean contains(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        if (!(x * 0.0 + y * 0.0 == 0.0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            /* Either x or y was infinite or NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
             * A NaN always produces a negative response to any test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
             * and Infinity values cannot be "inside" any path so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
             * they should return false as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        // We count the "Y" crossings to determine if the point is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        // inside the curve bounded by its closing line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        double x1 = getX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        double y1 = getY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        double x2 = getX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        double y2 = getY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        int crossings =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            (Curve.pointCrossingsForLine(x, y, x1, y1, x2, y2) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
             Curve.pointCrossingsForCubic(x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                                          x1, y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                                          getCtrlX1(), getCtrlY1(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                                          getCtrlX2(), getCtrlY2(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                                          x2, y2, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        return ((crossings & 1) == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    public boolean contains(Point2D p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        return contains(p.getX(), p.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    public boolean intersects(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        // Trivially reject non-existant rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        if (w <= 0 || h <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
7942
e1e8a8dd8cd8 4493128: CubicCurve2D intersects method fails
dlila
parents: 7941
diff changeset
  1447
        int numCrossings = rectCrossings(x, y, w, h);
e1e8a8dd8cd8 4493128: CubicCurve2D intersects method fails
dlila
parents: 7941
diff changeset
  1448
        // the intended return value is
e1e8a8dd8cd8 4493128: CubicCurve2D intersects method fails
dlila
parents: 7941
diff changeset
  1449
        // numCrossings != 0 || numCrossings == Curve.RECT_INTERSECTS
e1e8a8dd8cd8 4493128: CubicCurve2D intersects method fails
dlila
parents: 7941
diff changeset
  1450
        // but if (numCrossings != 0) numCrossings == INTERSECTS won't matter
e1e8a8dd8cd8 4493128: CubicCurve2D intersects method fails
dlila
parents: 7941
diff changeset
  1451
        // and if !(numCrossings != 0) then numCrossings == 0, so
e1e8a8dd8cd8 4493128: CubicCurve2D intersects method fails
dlila
parents: 7941
diff changeset
  1452
        // numCrossings != RECT_INTERSECT
e1e8a8dd8cd8 4493128: CubicCurve2D intersects method fails
dlila
parents: 7941
diff changeset
  1453
        return numCrossings != 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    public boolean intersects(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    public boolean contains(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        if (w <= 0 || h <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        }
7941
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1472
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1473
        int numCrossings = rectCrossings(x, y, w, h);
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1474
        return !(numCrossings == 0 || numCrossings == Curve.RECT_INTERSECTS);
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1475
    }
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1476
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1477
    private int rectCrossings(double x, double y, double w, double h) {
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1478
        int crossings = 0;
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1479
        if (!(getX1() == getX2() && getY1() == getY2())) {
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1480
            crossings = Curve.rectCrossingsForLine(crossings,
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1481
                                                   x, y,
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1482
                                                   x+w, y+h,
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1483
                                                   getX1(), getY1(),
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1484
                                                   getX2(), getY2());
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1485
            if (crossings == Curve.RECT_INTERSECTS) {
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1486
                return crossings;
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1487
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        }
7941
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1489
        // we call this with the curve's direction reversed, because we wanted
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1490
        // to call rectCrossingsForLine first, because it's cheaper.
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1491
        return Curve.rectCrossingsForCubic(crossings,
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1492
                                           x, y,
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1493
                                           x+w, y+h,
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1494
                                           getX2(), getY2(),
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1495
                                           getCtrlX2(), getCtrlY2(),
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1496
                                           getCtrlX1(), getCtrlY1(),
e71443fb9af6 4724552: CubicCurve2D.contains(Rectangle2D) returns true when only partially contained.
dlila
parents: 5506
diff changeset
  1497
                                           getX1(), getY1(), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    public boolean contains(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    public Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        return getBounds2D().getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * Returns an iteration object that defines the boundary of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * The iterator for this class is not multi-threaded safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * which means that this <code>CubicCurve2D</code> class does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * guarantee that modifications to the geometry of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * <code>CubicCurve2D</code> object do not affect any iterations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * that geometry that are already in process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * @param at an optional <code>AffineTransform</code> to be applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * coordinates as they are returned in the iteration, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * if untransformed coordinates are desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * @return    the <code>PathIterator</code> object that returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     *          geometry of the outline of this <code>CubicCurve2D</code>, one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     *          segment at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    public PathIterator getPathIterator(AffineTransform at) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        return new CubicIterator(this, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * Return an iteration object that defines the boundary of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * flattened shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * The iterator for this class is not multi-threaded safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * which means that this <code>CubicCurve2D</code> class does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * guarantee that modifications to the geometry of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * <code>CubicCurve2D</code> object do not affect any iterations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * that geometry that are already in process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * @param at an optional <code>AffineTransform</code> to be applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * coordinates as they are returned in the iteration, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * if untransformed coordinates are desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * @param flatness the maximum amount that the control points
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * for a given curve can vary from colinear before a subdivided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * curve is replaced by a straight line connecting the end points
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * @return    the <code>PathIterator</code> object that returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * geometry of the outline of this <code>CubicCurve2D</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * one segment at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        return new FlatteningPathIterator(getPathIterator(at), flatness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * Creates a new object of the same class as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * @return     a clone of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * @exception  OutOfMemoryError            if there is not enough memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * @see        java.lang.Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * @since      1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            // this shouldn't happen, since we are Cloneable
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1572
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
}