jdk/src/share/classes/java/awt/geom/QuadCurve2D.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 21278 ef8a3a2a72f2
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
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, 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.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.awt.geom.Curve;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The <code>QuadCurve2D</code> class defines a quadratic parametric curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * segment in {@code (x,y)} coordinate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class is only the abstract superclass for all objects that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * store a 2D quadratic curve segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The actual storage representation of the coordinates is left to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public abstract class QuadCurve2D implements Shape, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * A quadratic parametric curve segment specified with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * {@code float} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static class Float extends QuadCurve2D implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         * The X coordinate of the start point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        public float x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * The Y coordinate of the start point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public float y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         * The X coordinate of the control point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public float ctrlx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         * The Y coordinate of the control point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public float ctrly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * The X coordinate of the end point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public float x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * The Y coordinate of the end point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        public float y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         * Constructs and initializes a <code>QuadCurve2D</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * coordinates (0, 0, 0, 0, 0, 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public Float() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         * Constructs and initializes a <code>QuadCurve2D</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         * specified {@code float} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * @param ctrlx the X coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * @param ctrly the Y coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public Float(float x1, float y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                     float ctrlx, float ctrly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                     float x2, float y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            setCurve(x1, y1, ctrlx, ctrly, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        public double getX1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return (double) x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public double getY1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return (double) y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public Point2D getP1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return new Point2D.Float(x1, y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        public double getCtrlX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return (double) ctrlx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        public double getCtrlY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return (double) ctrly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public Point2D getCtrlPt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return new Point2D.Float(ctrlx, ctrly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        public double getX2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return (double) x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        public double getY2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return (double) y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public Point2D getP2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return new Point2D.Float(x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        public void setCurve(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                             double ctrlx, double ctrly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                             double x2, double y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            this.x1    = (float) x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            this.y1    = (float) y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            this.ctrlx = (float) ctrlx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            this.ctrly = (float) ctrly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            this.x2    = (float) x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            this.y2    = (float) y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * Sets the location of the end points and control point of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * to the specified {@code float} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * @param ctrlx the X coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * @param ctrly the Y coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        public void setCurve(float x1, float y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                             float ctrlx, float ctrly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                             float x2, float y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            this.x1    = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            this.y1    = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            this.ctrlx = ctrlx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            this.ctrly = ctrly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            this.x2    = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            this.y2    = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            float left   = Math.min(Math.min(x1, x2), ctrlx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            float top    = Math.min(Math.min(y1, y2), ctrly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            float right  = Math.max(Math.max(x1, x2), ctrlx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            float bottom = Math.max(Math.max(y1, y2), ctrly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return new Rectangle2D.Float(left, top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                         right - left, bottom - top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         * JDK 1.6 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        private static final long serialVersionUID = -8511188402130719609L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * A quadratic parametric curve segment specified with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * {@code double} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static class Double extends QuadCurve2D implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * The X coordinate of the start point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        public double x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         * The Y coordinate of the start point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        public double y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         * The X coordinate of the control point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        public double ctrlx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * The Y coordinate of the control point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        public double ctrly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * The X coordinate of the end point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        public double x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         * The Y coordinate of the end point of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        public double y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * Constructs and initializes a <code>QuadCurve2D</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * coordinates (0, 0, 0, 0, 0, 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        public Double() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * Constructs and initializes a <code>QuadCurve2D</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * specified {@code double} coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * @param ctrlx the X coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * @param ctrly the Y coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        public Double(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                      double ctrlx, double ctrly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                      double x2, double y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            setCurve(x1, y1, ctrlx, ctrly, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        public double getX1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return 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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        public double getY1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return 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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        public Point2D getP1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return new Point2D.Double(x1, y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        public double getCtrlX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return ctrlx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        public double getCtrlY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return ctrly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        public Point2D getCtrlPt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return new Point2D.Double(ctrlx, ctrly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        public double getX2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            return 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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        public double getY2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return 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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * {@inheritDoc}
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 Point2D getP2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return new Point2D.Double(x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        public void setCurve(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                             double ctrlx, double ctrly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                             double x2, double y2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            this.x1    = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            this.y1    = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            this.ctrlx = ctrlx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            this.ctrly = ctrly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            this.x2    = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            this.y2    = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            double left   = Math.min(Math.min(x1, x2), ctrlx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            double top    = Math.min(Math.min(y1, y2), ctrly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            double right  = Math.max(Math.max(x1, x2), ctrlx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            double bottom = Math.max(Math.max(y1, y2), ctrly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            return new Rectangle2D.Double(left, top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                                          right - left, bottom - top);
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
         * JDK 1.6 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        private static final long serialVersionUID = 4217149928428559721L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * This is an abstract class that cannot be instantiated directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Type-specific implementation subclasses are available for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * instantiation and provide a number of formats for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * the information necessary to satisfy the various accessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * methods below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @see java.awt.geom.QuadCurve2D.Float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @see java.awt.geom.QuadCurve2D.Double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    protected QuadCurve2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Returns the X coordinate of the start point in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * <code>double</code> in precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @return the X coordinate of the start point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public abstract double getX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Returns the Y coordinate of the start point in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * <code>double</code> precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @return the Y coordinate of the start point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public abstract double getY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Returns the start point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @return a <code>Point2D</code> that is the start point of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *          <code>QuadCurve2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public abstract Point2D getP1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Returns the X coordinate of the control point in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <code>double</code> precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @return X coordinate the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public abstract double getCtrlX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Returns the Y coordinate of the control point in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <code>double</code> precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @return the Y coordinate of the control point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public abstract double getCtrlY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Returns the control point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @return a <code>Point2D</code> that is the control point of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *          <code>Point2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    public abstract Point2D getCtrlPt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Returns the X coordinate of the end point in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <code>double</code> precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @return the x coordiante of the end point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public abstract double getX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Returns the Y coordinate of the end point in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <code>double</code> precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @return the Y coordinate of the end point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public abstract double getY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Returns the end point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @return a <code>Point</code> object that is the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *          of this <code>Point2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public abstract Point2D getP2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Sets the location of the end points and control point of this curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * to the specified <code>double</code> coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param ctrlx the X coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @param ctrly the Y coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public abstract void setCurve(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                  double ctrlx, double ctrly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                  double x2, double y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Sets the location of the end points and control points of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * <code>QuadCurve2D</code> to the <code>double</code> coordinates at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * the specified offset in the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @param coords the array containing coordinate values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @param offset the index into the array from which to start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *          getting the coordinate values and assigning them to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *          <code>QuadCurve2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    public void setCurve(double[] coords, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        setCurve(coords[offset + 0], coords[offset + 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                 coords[offset + 2], coords[offset + 3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                 coords[offset + 4], coords[offset + 5]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Sets the location of the end points and control point of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <code>QuadCurve2D</code> to the specified <code>Point2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @param p1 the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @param cp the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param p2 the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    public void setCurve(Point2D p1, Point2D cp, Point2D p2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        setCurve(p1.getX(), p1.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                 cp.getX(), cp.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                 p2.getX(), p2.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Sets the location of the end points and control points of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <code>QuadCurve2D</code> to the coordinates of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <code>Point2D</code> objects at the specified offset in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @param pts an array containing <code>Point2D</code> that define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *          coordinate values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param offset the index into <code>pts</code> from which to start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *          getting the coordinate values and assigning them to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *          <code>QuadCurve2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public void setCurve(Point2D[] pts, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        setCurve(pts[offset + 0].getX(), pts[offset + 0].getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                 pts[offset + 1].getX(), pts[offset + 1].getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                 pts[offset + 2].getX(), pts[offset + 2].getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Sets the location of the end points and control point of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * <code>QuadCurve2D</code> to the same as those in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <code>QuadCurve2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param c the specified <code>QuadCurve2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public void setCurve(QuadCurve2D c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        setCurve(c.getX1(), c.getY1(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                 c.getCtrlX(), c.getCtrlY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                 c.getX2(), c.getY2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Returns the square of the flatness, or maximum distance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * control point from the line connecting the end points, of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * quadratic curve specified by the indicated control points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param ctrlx the X coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @param ctrly the Y coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @return the square of the flatness of the quadratic curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *          defined by the specified coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public static double getFlatnessSq(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                       double ctrlx, double ctrly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                       double x2, double y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return Line2D.ptSegDistSq(x1, y1, x2, y2, ctrlx, ctrly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Returns the flatness, or maximum distance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * control point from the line connecting the end points, of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * quadratic curve specified by the indicated control points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @param ctrlx the X coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param ctrly the Y coordinate of the control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @return the flatness of the quadratic curve defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *          specified coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    public static double getFlatness(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                                     double ctrlx, double ctrly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                     double x2, double y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return Line2D.ptSegDist(x1, y1, x2, y2, ctrlx, ctrly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Returns the square of the flatness, or maximum distance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * control point from the line connecting the end points, of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * quadratic curve specified by the control points stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * indicated array at the indicated index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @param coords an array containing coordinate values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param offset the index into <code>coords</code> from which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *          to start getting the values from the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return the flatness of the quadratic curve that is defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *          values in the specified array at the specified index.
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 static double getFlatnessSq(double coords[], int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        return Line2D.ptSegDistSq(coords[offset + 0], coords[offset + 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                                  coords[offset + 4], coords[offset + 5],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                                  coords[offset + 2], coords[offset + 3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Returns the flatness, or maximum distance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * control point from the line connecting the end points, of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * quadratic curve specified by the control points stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * indicated array at the indicated index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @param coords an array containing coordinate values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @param offset the index into <code>coords</code> from which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *          start getting the coordinate values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @return the flatness of a quadratic curve defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *          specified array at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public static double getFlatness(double coords[], int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        return Line2D.ptSegDist(coords[offset + 0], coords[offset + 1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                                coords[offset + 4], coords[offset + 5],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                                coords[offset + 2], coords[offset + 3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Returns the square of the flatness, or maximum distance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * control point from the line connecting the end points, of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * <code>QuadCurve2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @return the square of the flatness of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *          <code>QuadCurve2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public double getFlatnessSq() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        return Line2D.ptSegDistSq(getX1(), getY1(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                  getX2(), getY2(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                                  getCtrlX(), getCtrlY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * Returns the flatness, or maximum distance of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * control point from the line connecting the end points, of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * <code>QuadCurve2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @return the flatness of this <code>QuadCurve2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    public double getFlatness() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return Line2D.ptSegDist(getX1(), getY1(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                                getX2(), getY2(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                                getCtrlX(), getCtrlY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Subdivides this <code>QuadCurve2D</code> and stores the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * two subdivided curves into the <code>left</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * <code>right</code> curve parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * Either or both of the <code>left</code> and <code>right</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * objects can be the same as this <code>QuadCurve2D</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @param left the <code>QuadCurve2D</code> object for storing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * left or first half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @param right the <code>QuadCurve2D</code> object for storing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * right or second half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    public void subdivide(QuadCurve2D left, QuadCurve2D right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        subdivide(this, left, right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Subdivides the quadratic curve specified by the <code>src</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * parameter and stores the resulting two subdivided curves into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * <code>left</code> and <code>right</code> curve parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Either or both of the <code>left</code> and <code>right</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * objects can be the same as the <code>src</code> object or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @param src the quadratic curve to be subdivided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param left the <code>QuadCurve2D</code> object for storing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *          left or first half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @param right the <code>QuadCurve2D</code> object for storing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *          right or second half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    public static void subdivide(QuadCurve2D src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                                 QuadCurve2D left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                                 QuadCurve2D right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        double x1 = src.getX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        double y1 = src.getY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        double ctrlx = src.getCtrlX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        double ctrly = src.getCtrlY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        double x2 = src.getX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        double y2 = src.getY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        double ctrlx1 = (x1 + ctrlx) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        double ctrly1 = (y1 + ctrly) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        double ctrlx2 = (x2 + ctrlx) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        double ctrly2 = (y2 + ctrly) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        ctrlx = (ctrlx1 + ctrlx2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        ctrly = (ctrly1 + ctrly2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            left.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx, ctrly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if (right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            right.setCurve(ctrlx, ctrly, ctrlx2, ctrly2, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
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
     * Subdivides the quadratic curve specified by the coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * stored in the <code>src</code> array at indices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * <code>srcoff</code> through <code>srcoff</code>&nbsp;+&nbsp;5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * and stores the resulting two subdivided curves into the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * result arrays at the corresponding indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Either or both of the <code>left</code> and <code>right</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * arrays can be <code>null</code> or a reference to the same array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * and offset as the <code>src</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * Note that the last point in the first subdivided curve is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * same as the first point in the second subdivided curve.  Thus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * it is possible to pass the same array for <code>left</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * <code>right</code> and to use offsets such that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * <code>rightoff</code> equals <code>leftoff</code> + 4 in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * to avoid allocating extra storage for this common point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @param src the array holding the coordinates for the source curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @param srcoff the offset into the array of the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * the 6 source coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @param left the array for storing the coordinates for the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @param leftoff the offset into the array of the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * the 6 left coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @param right the array for storing the coordinates for the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * half of the subdivided curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @param rightoff the offset into the array of the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * the 6 right coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    public static void subdivide(double src[], int srcoff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                                 double left[], int leftoff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                 double right[], int rightoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        double x1 = src[srcoff + 0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        double y1 = src[srcoff + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        double ctrlx = src[srcoff + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        double ctrly = src[srcoff + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        double x2 = src[srcoff + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        double y2 = src[srcoff + 5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        if (left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            left[leftoff + 0] = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            left[leftoff + 1] = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if (right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            right[rightoff + 4] = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            right[rightoff + 5] = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        x1 = (x1 + ctrlx) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        y1 = (y1 + ctrly) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        x2 = (x2 + ctrlx) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        y2 = (y2 + ctrly) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        ctrlx = (x1 + x2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        ctrly = (y1 + y2) / 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        if (left != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            left[leftoff + 2] = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            left[leftoff + 3] = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            left[leftoff + 4] = ctrlx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            left[leftoff + 5] = ctrly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (right != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            right[rightoff + 0] = ctrlx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            right[rightoff + 1] = ctrly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            right[rightoff + 2] = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            right[rightoff + 3] = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Solves the quadratic whose coefficients are in the <code>eqn</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * array and places the non-complex roots back into the same array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * returning the number of roots.  The quadratic solved is represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * by the equation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *     eqn = {C, B, A};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *     ax^2 + bx + c = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * A return value of <code>-1</code> is used to distinguish a constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * equation, which might be always 0 or never 0, from an equation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * has no zeroes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @param eqn the array that contains the quadratic coefficients
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @return the number of roots, or <code>-1</code> if the equation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *          a constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    public static int solveQuadratic(double eqn[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        return solveQuadratic(eqn, eqn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * Solves the quadratic whose coefficients are in the <code>eqn</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * array and places the non-complex roots into the <code>res</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * array, returning the number of roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * The quadratic solved is represented by the equation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *     eqn = {C, B, A};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *     ax^2 + bx + c = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * A return value of <code>-1</code> is used to distinguish a constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * equation, which might be always 0 or never 0, from an equation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * has no zeroes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @param eqn the specified array of coefficients to use to solve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *        the quadratic equation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @param res the array that contains the non-complex roots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *        resulting from the solution of the quadratic equation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @return the number of roots, or <code>-1</code> if the equation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *  a constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public static int solveQuadratic(double eqn[], double res[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        double a = eqn[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        double b = eqn[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        double c = eqn[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        int roots = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if (a == 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            // The quadratic parabola has degenerated to a line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            if (b == 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                // The line has degenerated to a constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            res[roots++] = -c / b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            // From Numerical Recipes, 5.6, Quadratic and Cubic Equations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            double d = b * b - 4.0 * a * c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            if (d < 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                // If d < 0.0, then there are no roots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            d = Math.sqrt(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            // For accuracy, calculate one root using:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            //     (-b +/- d) / 2a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            // and the other using:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            //     2c / (-b +/- d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            // Choose the sign of the +/- so that b+d gets larger in magnitude
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            if (b < 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                d = -d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            double q = (b + d) / -2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            // We already tested a for being 0 above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            res[roots++] = q / a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            if (q != 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                res[roots++] = c / q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        return roots;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    public boolean contains(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        double x1 = getX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        double y1 = getY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        double xc = getCtrlX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        double yc = getCtrlY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        double x2 = getX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        double y2 = getY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
         * We have a convex shape bounded by quad curve Pc(t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
         * and ine Pl(t).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         *     P1 = (x1, y1) - start point of curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         *     P2 = (x2, y2) - end point of curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         *     Pc = (xc, yc) - control point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
         *     Pq(t) = P1*(1 - t)^2 + 2*Pc*t*(1 - t) + P2*t^2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
         *           = (P1 - 2*Pc + P2)*t^2 + 2*(Pc - P1)*t + P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
         *     Pl(t) = P1*(1 - t) + P2*t
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         *     t = [0:1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
         *     P = (x, y) - point of interest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * Let's look at second derivative of quad curve equation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
         *     Pq''(t) = 2 * (P1 - 2 * Pc + P2) = Pq''
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
         *     It's constant vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
         * Let's draw a line through P to be parallel to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
         * vector and find the intersection of the quad curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
         * and the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
         * Pq(t) is point of intersection if system of equations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
         * below has the solution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         *     L(s) = P + Pq''*s == Pq(t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
         *     Pq''*s + (P - Pq(t)) == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
         *     | xq''*s + (x - xq(t)) == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
         *     | yq''*s + (y - yq(t)) == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
         * This system has the solution if rank of its matrix equals to 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * That is, determinant of the matrix should be zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
         *     (y - yq(t))*xq'' == (x - xq(t))*yq''
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * Let's solve this equation with 't' variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         * Also let kx = x1 - 2*xc + x2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         *          ky = y1 - 2*yc + y2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
         *     t0q = (1/2)*((x - x1)*ky - (y - y1)*kx) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
         *                 ((xc - x1)*ky - (yc - y1)*kx)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
         * Let's do the same for our line Pl(t):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
         *     t0l = ((x - x1)*ky - (y - y1)*kx) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         *           ((x2 - x1)*ky - (y2 - y1)*kx)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
         * It's easy to check that t0q == t0l. This fact means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
         * we can compute t0 only one time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         * In case t0 < 0 or t0 > 1, we have an intersections outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
         * of shape bounds. So, P is definitely out of shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
         * In case t0 is inside [0:1], we should calculate Pq(t0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
         * and Pl(t0). We have three points for now, and all of them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         * lie on one line. So, we just need to detect, is our point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         * of interest between points of intersections or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         * If the denominator in the t0q and t0l equations is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         * zero, then the points must be collinear and so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         * curve is degenerate and encloses no area.  Thus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         * result is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        double kx = x1 - 2 * xc + x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        double ky = y1 - 2 * yc + y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        double dx = x - x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        double dy = y - y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        double dxl = x2 - x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        double dyl = y2 - y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        double t0 = (dx * ky - dy * kx) / (dxl * ky - dyl * kx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (t0 < 0 || t0 > 1 || t0 != t0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        double xb = kx * t0 * t0 + 2 * (xc - x1) * t0 + x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        double yb = ky * t0 * t0 + 2 * (yc - y1) * t0 + y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        double xl = dxl * t0 + x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        double yl = dyl * t0 + y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        return (x >= xb && x < xl) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
               (x >= xl && x < xb) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
               (y >= yb && y < yl) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
               (y >= yl && y < yb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    public boolean contains(Point2D p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        return contains(p.getX(), p.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Fill an array with the coefficients of the parametric equation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * in t, ready for solving against val with solveQuadratic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * We currently have:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *     val = Py(t) = C1*(1-t)^2 + 2*CP*t*(1-t) + C2*t^2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *                 = C1 - 2*C1*t + C1*t^2 + 2*CP*t - 2*CP*t^2 + C2*t^2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *                 = C1 + (2*CP - 2*C1)*t + (C1 - 2*CP + C2)*t^2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *               0 = (C1 - val) + (2*CP - 2*C1)*t + (C1 - 2*CP + C2)*t^2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *               0 = C + Bt + At^2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *     C = C1 - val
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *     B = 2*CP - 2*C1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *     A = C1 - 2*CP + C2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    private static void fillEqn(double eqn[], double val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                                double c1, double cp, double c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        eqn[0] = c1 - val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        eqn[1] = cp + cp - c1 - c1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        eqn[2] = c1 - cp - cp + c2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * Evaluate the t values in the first num slots of the vals[] array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * and place the evaluated values back into the same array.  Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * evaluate t values that are within the range <0, 1>, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * the 0 and 1 ends of the range iff the include0 or include1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * booleans are true.  If an "inflection" equation is handed in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * then any points which represent a point of inflection for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * quadratic equation are also ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    private static int evalQuadratic(double vals[], int num,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                                     boolean include0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                                     boolean include1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                                     double inflect[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                                     double c1, double ctrl, double c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        for (int i = 0; i < num; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            double t = vals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            if ((include0 ? t >= 0 : t > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                (include1 ? t <= 1 : t < 1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                (inflect == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                 inflect[1] + 2*inflect[2]*t != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                double u = 1 - t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                vals[j++] = c1*u*u + 2*ctrl*t*u + c2*t*t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        return j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    private static final int BELOW = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    private static final int LOWEDGE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    private static final int INSIDE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    private static final int HIGHEDGE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    private static final int ABOVE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * Determine where coord lies with respect to the range from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * low to high.  It is assumed that low <= high.  The return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * value is one of the 5 values BELOW, LOWEDGE, INSIDE, HIGHEDGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * or ABOVE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    private static int getTag(double coord, double low, double high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        if (coord <= low) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            return (coord < low ? BELOW : LOWEDGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        if (coord >= high) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            return (coord > high ? ABOVE : HIGHEDGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return INSIDE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Determine if the pttag represents a coordinate that is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * in its test range, or is on the border with either of the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * opttags representing another coordinate that is "towards the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * inside" of that test range.  In other words, are either of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * two "opt" points "drawing the pt inward"?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    private static boolean inwards(int pttag, int opt1tag, int opt2tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        switch (pttag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        case BELOW:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        case ABOVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        case LOWEDGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            return (opt1tag >= INSIDE || opt2tag >= INSIDE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        case INSIDE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        case HIGHEDGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            return (opt1tag <= INSIDE || opt2tag <= INSIDE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    public boolean intersects(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        // Trivially reject non-existant rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        if (w <= 0 || h <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        // Trivially accept if either endpoint is inside the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        // (not on its border since it may end there and not go inside)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        // Record where they lie with respect to the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        //     -1 => left, 0 => inside, 1 => right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        double x1 = getX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        double y1 = getY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        int x1tag = getTag(x1, x, x+w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        int y1tag = getTag(y1, y, y+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        if (x1tag == INSIDE && y1tag == INSIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        double x2 = getX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        double y2 = getY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        int x2tag = getTag(x2, x, x+w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        int y2tag = getTag(y2, y, y+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        if (x2tag == INSIDE && y2tag == INSIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        double ctrlx = getCtrlX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        double ctrly = getCtrlY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        int ctrlxtag = getTag(ctrlx, x, x+w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        int ctrlytag = getTag(ctrly, y, y+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        // Trivially reject if all points are entirely to one side of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        // the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        if (x1tag < INSIDE && x2tag < INSIDE && ctrlxtag < INSIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            return false;       // All points left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        if (y1tag < INSIDE && y2tag < INSIDE && ctrlytag < INSIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            return false;       // All points above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        if (x1tag > INSIDE && x2tag > INSIDE && ctrlxtag > INSIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            return false;       // All points right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        if (y1tag > INSIDE && y2tag > INSIDE && ctrlytag > INSIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            return false;       // All points below
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        // Test for endpoints on the edge where either the segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        // or the curve is headed "inwards" from them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        // Note: These tests are a superset of the fast endpoint tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        //       above and thus repeat those tests, but take more time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        //       and cover more cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        if (inwards(x1tag, x2tag, ctrlxtag) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            inwards(y1tag, y2tag, ctrlytag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            // First endpoint on border with either edge moving inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (inwards(x2tag, x1tag, ctrlxtag) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            inwards(y2tag, y1tag, ctrlytag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            // Second endpoint on border with either edge moving inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        // Trivially accept if endpoints span directly across the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        boolean xoverlap = (x1tag * x2tag <= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        boolean yoverlap = (y1tag * y2tag <= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (x1tag == INSIDE && x2tag == INSIDE && yoverlap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        if (y1tag == INSIDE && y2tag == INSIDE && xoverlap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        // We now know that both endpoints are outside the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        // but the 3 points are not all on one side of the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        // Therefore the curve cannot be contained inside the rectangle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        // but the rectangle might be contained inside the curve, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        // the curve might intersect the boundary of the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        double[] eqn = new double[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        double[] res = new double[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        if (!yoverlap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            // Both Y coordinates for the closing segment are above or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            // below the rectangle which means that we can only intersect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            // if the curve crosses the top (or bottom) of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            // in more than one place and if those crossing locations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            // span the horizontal range of the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            fillEqn(eqn, (y1tag < INSIDE ? y : y+h), y1, ctrly, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            return (solveQuadratic(eqn, res) == 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    evalQuadratic(res, 2, true, true, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                                  x1, ctrlx, x2) == 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    getTag(res[0], x, x+w) * getTag(res[1], x, x+w) <= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        // Y ranges overlap.  Now we examine the X ranges
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        if (!xoverlap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            // Both X coordinates for the closing segment are left of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            // or right of the rectangle which means that we can only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            // intersect if the curve crosses the left (or right) edge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            // of the rectangle in more than one place and if those
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            // crossing locations span the vertical range of the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            fillEqn(eqn, (x1tag < INSIDE ? x : x+w), x1, ctrlx, x2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            return (solveQuadratic(eqn, res) == 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    evalQuadratic(res, 2, true, true, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                                  y1, ctrly, y2) == 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    getTag(res[0], y, y+h) * getTag(res[1], y, y+h) <= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        // The X and Y ranges of the endpoints overlap the X and Y
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        // ranges of the rectangle, now find out how the endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        // line segment intersects the Y range of the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        double dx = x2 - x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        double dy = y2 - y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        double k = y2 * x1 - x2 * y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        int c1tag, c2tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        if (y1tag == INSIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            c1tag = x1tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            c1tag = getTag((k + dx * (y1tag < INSIDE ? y : y+h)) / dy, x, x+w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        if (y2tag == INSIDE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            c2tag = x2tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            c2tag = getTag((k + dx * (y2tag < INSIDE ? y : y+h)) / dy, x, x+w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        // If the part of the line segment that intersects the Y range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        // of the rectangle crosses it horizontally - trivially accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        if (c1tag * c2tag <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        // Now we know that both the X and Y ranges intersect and that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        // the endpoint line segment does not directly cross the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        // We can almost treat this case like one of the cases above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        // where both endpoints are to one side, except that we will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        // only get one intersection of the curve with the vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        // side of the rectangle.  This is because the endpoint segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        // accounts for the other intersection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        // (Remember there is overlap in both the X and Y ranges which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        //  means that the segment must cross at least one vertical edge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        //  of the rectangle - in particular, the "near vertical side" -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        //  leaving only one intersection for the curve.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        // Now we calculate the y tags of the two intersections on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        // "near vertical side" of the rectangle.  We will have one with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        // the endpoint segment, and one with the curve.  If those two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        // vertical intersections overlap the Y range of the rectangle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        // we have an intersection.  Otherwise, we don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        // c1tag = vertical intersection class of the endpoint segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        // Choose the y tag of the endpoint that was not on the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        // side of the rectangle as the subsegment calculated above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        // Note that we can "steal" the existing Y tag of that endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        // since it will be provably the same as the vertical intersection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        c1tag = ((c1tag * x1tag <= 0) ? y1tag : y2tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        // c2tag = vertical intersection class of the curve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        // We have to calculate this one the straightforward way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        // Note that the c2tag can still tell us which vertical edge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        // to test against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        fillEqn(eqn, (c2tag < INSIDE ? x : x+w), x1, ctrlx, x2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        int num = solveQuadratic(eqn, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        // Note: We should be able to assert(num == 2); since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        // X range "crosses" (not touches) the vertical boundary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        // but we pass num to evalQuadratic for completeness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        evalQuadratic(res, num, true, true, null, y1, ctrly, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        // Note: We can assert(num evals == 1); since one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        // 2 crossings will be out of the [0,1] range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        c2tag = getTag(res[0], y, y+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        // Finally, we have an intersection if the two crossings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        // overlap the Y range of the rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        return (c1tag * c2tag <= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    public boolean intersects(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    public boolean contains(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        if (w <= 0 || h <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        // Assertion: Quadratic curves closed by connecting their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        // endpoints are always convex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        return (contains(x, y) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                contains(x + w, y) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                contains(x + w, y + h) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                contains(x, y + h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    public boolean contains(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    public Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        return getBounds2D().getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * Returns an iteration object that defines the boundary of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * shape of this <code>QuadCurve2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * The iterator for this class is not multi-threaded safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * which means that this <code>QuadCurve2D</code> class does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * guarantee that modifications to the geometry of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * <code>QuadCurve2D</code> object do not affect any iterations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * that geometry that are already in process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * @param at an optional {@link AffineTransform} to apply to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     *          shape boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * @return a {@link PathIterator} object that defines the boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     *          of the shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    public PathIterator getPathIterator(AffineTransform at) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        return new QuadIterator(this, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * Returns an iteration object that defines the boundary of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * flattened shape of this <code>QuadCurve2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * The iterator for this class is not multi-threaded safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * which means that this <code>QuadCurve2D</code> class does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * guarantee that modifications to the geometry of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * <code>QuadCurve2D</code> object do not affect any iterations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * that geometry that are already in process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * @param at an optional <code>AffineTransform</code> to apply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     *          to the boundary of the shape
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * @param flatness the maximum distance that the control points for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     *          subdivided curve can be with respect to a line connecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     *          the end points of this curve before this curve is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     *          replaced by a straight line connecting the end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * @return a <code>PathIterator</code> object that defines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     *          flattened boundary of the shape.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        return new FlatteningPathIterator(getPathIterator(at), flatness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * Creates a new object of the same class and with the same contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * @return     a clone of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * @exception  OutOfMemoryError            if there is not enough memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * @see        java.lang.Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * @since      1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            // this shouldn't happen, since we are Cloneable
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
  1398
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
}