jdk/src/share/classes/java/awt/geom/Line2D.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 20455 f6f9a0c2796b
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This <code>Line2D</code> represents a line segment in {@code (x,y)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * coordinate space.  This class, like all of the Java 2D API, uses a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * default coordinate system called <i>user space</i> in which the y-axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * values increase downward and x-axis values increase to the right.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * more information on the user space coordinate system, see the
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20451
diff changeset
    38
 * <a href="http://docs.oracle.com/javase/1.3/docs/guide/2d/spec/j2d-intro.fm2.html#61857">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Coordinate Systems</a> section of the Java 2D Programmer's Guide.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class is only the abstract superclass for all objects that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * store a 2D line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The actual storage representation of the coordinates is left to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author      Jim Graham
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public abstract class Line2D implements Shape, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * A line segment specified with float coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static class Float extends Line2D implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         * The X coordinate of the start point of the line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public float x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * The Y coordinate of the start point of the line 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 end point of the line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public float x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         * The Y coordinate of the end point of the line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public float y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        /**
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 10419
diff changeset
    85
         * Constructs and initializes a Line with coordinates (0, 0) &rarr; (0, 0).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public Float() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         * Constructs and initializes a Line from the specified coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public Float(float x1, float y1, float x2, float y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            setLine(x1, y1, x2, 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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * Constructs and initializes a <code>Line2D</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         * specified <code>Point2D</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * @param p1 the start <code>Point2D</code> of this line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * @param p2 the end <code>Point2D</code> of this line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        public Float(Point2D p1, Point2D p2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            setLine(p1, p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        public double getX1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return (double) x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        public double getY1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return (double) y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public Point2D getP1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return new Point2D.Float(x1, y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        public double getX2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return (double) x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        public double getY2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return (double) y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        public Point2D getP2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return new Point2D.Float(x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        public void setLine(double x1, double y1, double x2, double y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            this.x1 = (float) x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            this.y1 = (float) y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            this.x2 = (float) x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            this.y2 = (float) y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * Sets the location of the end points of this <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * to the specified float coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public void setLine(float x1, float y1, float x2, float y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            this.x1 = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            this.y1 = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            this.x2 = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            this.y2 = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            float x, y, w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (x1 < x2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                x = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                w = x2 - x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                x = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                w = x1 - x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (y1 < y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                y = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                h = y2 - y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                y = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                h = y1 - y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return new Rectangle2D.Float(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * JDK 1.6 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        private static final long serialVersionUID = 6161772511649436349L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * A line segment specified with double coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public static class Double extends Line2D implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * The X coordinate of the start point of the line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        public double x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         * The Y coordinate of the start point of the line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public double y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * The X coordinate of the end point of the line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        public double x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * The Y coordinate of the end point of the line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        public double y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        /**
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 10419
diff changeset
   252
         * Constructs and initializes a Line with coordinates (0, 0) &rarr; (0, 0).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        public Double() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * Constructs and initializes a <code>Line2D</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * specified coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        public Double(double x1, double y1, double x2, double y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            setLine(x1, y1, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * Constructs and initializes a <code>Line2D</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         * specified <code>Point2D</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * @param p1 the start <code>Point2D</code> of this line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         * @param p2 the end <code>Point2D</code> of this line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        public Double(Point2D p1, Point2D p2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            setLine(p1, p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public double getX1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        public double getY1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        public Point2D getP1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return new Point2D.Double(x1, y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        public double getX2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        public double getY2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        public Point2D getP2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return new Point2D.Double(x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        public void setLine(double x1, double y1, double x2, double y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            this.x1 = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            this.y1 = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            this.x2 = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            this.y2 = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        public Rectangle2D getBounds2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            double x, y, w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (x1 < x2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                x = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                w = x2 - x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                x = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                w = x1 - x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (y1 < y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                y = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                h = y2 - y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                y = y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                h = y1 - y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return new Rectangle2D.Double(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * JDK 1.6 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        private static final long serialVersionUID = 7979627399746467499L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * This is an abstract class that cannot be instantiated directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Type-specific implementation subclasses are available for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * instantiation and provide a number of formats for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * the information necessary to satisfy the various accessory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * methods below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @see java.awt.geom.Line2D.Float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @see java.awt.geom.Line2D.Double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    protected Line2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Returns the X coordinate of the start point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @return the X coordinate of the start point of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *         {@code Line2D} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public abstract double getX1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Returns the Y coordinate of the start point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return the Y coordinate of the start point of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *         {@code Line2D} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    public abstract double getY1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Returns the start <code>Point2D</code> of this <code>Line2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @return the start <code>Point2D</code> of this <code>Line2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public abstract Point2D getP1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Returns the X coordinate of the end point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @return the X coordinate of the end point of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *         {@code Line2D} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public abstract double getX2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Returns the Y coordinate of the end point in double precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return the Y coordinate of the end point of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         {@code Line2D} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public abstract double getY2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Returns the end <code>Point2D</code> of this <code>Line2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @return the end <code>Point2D</code> of this <code>Line2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public abstract Point2D getP2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Sets the location of the end points of this <code>Line2D</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * the specified double coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param x1 the X coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param y1 the Y coordinate of the start point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @param x2 the X coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @param y2 the Y coordinate of the end point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public abstract void setLine(double x1, double y1, double x2, double y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Sets the location of the end points of this <code>Line2D</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * the specified <code>Point2D</code> coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param p1 the start <code>Point2D</code> of the line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param p2 the end <code>Point2D</code> of the line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public void setLine(Point2D p1, Point2D p2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        setLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Sets the location of the end points of this <code>Line2D</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * the same as those end points of the specified <code>Line2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param l the specified <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public void setLine(Line2D l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        setLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Returns an indicator of where the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * {@code (px,py)} lies with respect to the line segment from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * {@code (x1,y1)} to {@code (x2,y2)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * The return value can be either 1, -1, or 0 and indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * in which direction the specified line must pivot around its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * first end point, {@code (x1,y1)}, in order to point at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * specified point {@code (px,py)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * <p>A return value of 1 indicates that the line segment must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * turn in the direction that takes the positive X axis towards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * the negative Y axis.  In the default coordinate system used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Java 2D, this direction is counterclockwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>A return value of -1 indicates that the line segment must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * turn in the direction that takes the positive X axis towards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * the positive Y axis.  In the default coordinate system, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * direction is clockwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <p>A return value of 0 indicates that the point lies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * exactly on the line segment.  Note that an indicator value
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20455
diff changeset
   480
     * of 0 is rare and not useful for determining collinearity
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * because of floating point rounding issues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * <p>If the point is colinear with the line segment, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * not between the end points, then the value will be -1 if the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * lies "beyond {@code (x1,y1)}" or 1 if the point lies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * "beyond {@code (x2,y2)}".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param x1 the X coordinate of the start point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @param y1 the Y coordinate of the start point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param x2 the X coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param y2 the Y coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param px the X coordinate of the specified point to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *           compared with the specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @param py the Y coordinate of the specified point to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *           compared with the specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @return an integer that indicates the position of the third specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *                  coordinates with respect to the line segment formed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *                  by the first two specified coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public static int relativeCCW(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                  double x2, double y2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                  double px, double py)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        x2 -= x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        y2 -= y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        px -= x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        py -= y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        double ccw = px * y2 - py * x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (ccw == 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            // The point is colinear, classify based on which side of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            // the segment the point falls on.  We can calculate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            // relative value using the projection of px,py onto the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            // segment - a negative value indicates the point projects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            // outside of the segment in the direction of the particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            // endpoint used as the origin for the projection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            ccw = px * x2 + py * y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if (ccw > 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                // Reverse the projection to be relative to the original x2,y2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                // x2 and y2 are simply negated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                // px and py need to have (x2 - x1) or (y2 - y1) subtracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                //    from them (based on the original values)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                // Since we really want to get a positive answer when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                //    point is "beyond (x2,y2)", then we want to calculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                //    the inverse anyway - thus we leave x2 & y2 negated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                px -= x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                py -= y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                ccw = px * x2 + py * y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if (ccw < 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    ccw = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return (ccw < 0.0) ? -1 : ((ccw > 0.0) ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Returns an indicator of where the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * {@code (px,py)} lies with respect to this line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * See the method comments of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * {@link #relativeCCW(double, double, double, double, double, double)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * to interpret the return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param px the X coordinate of the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *           to be compared with this <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @param py the Y coordinate of the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *           to be compared with this <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @return an integer that indicates the position of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *         coordinates with respect to this <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @see #relativeCCW(double, double, double, double, double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public int relativeCCW(double px, double py) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return relativeCCW(getX1(), getY1(), getX2(), getY2(), px, py);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Returns an indicator of where the specified <code>Point2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * lies with respect to this line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * See the method comments of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * {@link #relativeCCW(double, double, double, double, double, double)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * to interpret the return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param p the specified <code>Point2D</code> to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *          with this <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @return an integer that indicates the position of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *         <code>Point2D</code> with respect to this <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @see #relativeCCW(double, double, double, double, double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    public int relativeCCW(Point2D p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return relativeCCW(getX1(), getY1(), getX2(), getY2(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                           p.getX(), p.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Tests if the line segment from {@code (x1,y1)} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * {@code (x2,y2)} intersects the line segment from {@code (x3,y3)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * to {@code (x4,y4)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param x1 the X coordinate of the start point of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @param y1 the Y coordinate of the start point of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param x2 the X coordinate of the end point of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param y2 the Y coordinate of the end point of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param x3 the X coordinate of the start point of the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param y3 the Y coordinate of the start point of the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @param x4 the X coordinate of the end point of the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @param y4 the Y coordinate of the end point of the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @return <code>true</code> if the first specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *                  and the second specified line segment intersect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *                  each other; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public static boolean linesIntersect(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                         double x2, double y2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                         double x3, double y3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                                         double x4, double y4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return ((relativeCCW(x1, y1, x2, y2, x3, y3) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                 relativeCCW(x1, y1, x2, y2, x4, y4) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                && (relativeCCW(x3, y3, x4, y4, x1, y1) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    relativeCCW(x3, y3, x4, y4, x2, y2) <= 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * Tests if the line segment from {@code (x1,y1)} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * {@code (x2,y2)} intersects this line segment.
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 of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param y1 the Y coordinate of the start point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @param x2 the X coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @param y2 the Y coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *           specified line segment
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 10419
diff changeset
   626
     * @return {@code <true>} if this line segment and the specified line segment
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *                  intersect each other; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public boolean intersectsLine(double x1, double y1, double x2, double y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return linesIntersect(x1, y1, x2, y2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                              getX1(), getY1(), getX2(), getY2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Tests if the specified line segment intersects this line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param l the specified <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @return <code>true</code> if this line segment and the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *                  segment intersect each other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *                  <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public boolean intersectsLine(Line2D l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return linesIntersect(l.getX1(), l.getY1(), l.getX2(), l.getY2(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                              getX1(), getY1(), getX2(), getY2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Returns the square of the distance from a point to a line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * point and the closest point between the specified end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * If the specified point intersects the line segment in between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * end points, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @param x1 the X coordinate of the start point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @param y1 the Y coordinate of the start point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @param x2 the X coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param y2 the Y coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @param px the X coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *           measured against the specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @param py the Y coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *           measured against the specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @return a double value that is the square of the distance from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *                  specified point to the specified line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @see #ptLineDistSq(double, double, double, double, double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    public static double ptSegDistSq(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                     double x2, double y2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                                     double px, double py)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        // Adjust vectors relative to x1,y1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        // x2,y2 becomes relative vector from x1,y1 to end of segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        x2 -= x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        y2 -= y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        // px,py becomes relative vector from x1,y1 to test point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        px -= x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        py -= y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        double dotprod = px * x2 + py * y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        double projlenSq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (dotprod <= 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            // px,py is on the side of x1,y1 away from x2,y2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            // distance to segment is length of px,py vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            // "length of its (clipped) projection" is now 0.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            projlenSq = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            // switch to backwards vectors relative to x2,y2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            // x2,y2 are already the negative of x1,y1=>x2,y2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            // to get px,py to be the negative of px,py=>x2,y2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            // the dot product of two negated vectors is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            // as the dot product of the two normal vectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            px = x2 - px;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            py = y2 - py;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            dotprod = px * x2 + py * y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            if (dotprod <= 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                // px,py is on the side of x2,y2 away from x1,y1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                // distance to segment is length of (backwards) px,py vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                // "length of its (clipped) projection" is now 0.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                projlenSq = 0.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                // px,py is between x1,y1 and x2,y2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                // dotprod is the length of the px,py vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                // projected on the x2,y2=>x1,y1 vector times the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                // length of the x2,y2=>x1,y1 vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                projlenSq = dotprod * dotprod / (x2 * x2 + y2 * y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        // Distance to line is now the length of the relative point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        // vector minus the length of its projection onto the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        // (which is zero if the projection falls outside the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        //  of the line segment).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        double lenSq = px * px + py * py - projlenSq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (lenSq < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            lenSq = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        return lenSq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * Returns the distance from a point to a line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * point and the closest point between the specified end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * If the specified point intersects the line segment in between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * end points, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @param x1 the X coordinate of the start point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @param y1 the Y coordinate of the start point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @param x2 the X coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @param y2 the Y coordinate of the end point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *           specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @param px the X coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *           measured against the specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @param py the Y coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *           measured against the specified line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @return a double value that is the distance from the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *                          to the specified line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @see #ptLineDist(double, double, double, double, double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    public static double ptSegDist(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                                   double x2, double y2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                                   double px, double py)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        return Math.sqrt(ptSegDistSq(x1, y1, x2, y2, px, py));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * Returns the square of the distance from a point to this line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * point and the closest point between the current line's end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * If the specified point intersects the line segment in between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * end points, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @param px the X coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *           measured against this line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @param py the Y coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *           measured against this line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return a double value that is the square of the distance from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *                  specified point to the current line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @see #ptLineDistSq(double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    public double ptSegDistSq(double px, double py) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), px, py);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Returns the square of the distance from a <code>Point2D</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * this line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * point and the closest point between the current line's end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * If the specified point intersects the line segment in between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * end points, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @param pt the specified <code>Point2D</code> being measured against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *           this line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @return a double value that is the square of the distance from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *                  specified <code>Point2D</code> to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *                  line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @see #ptLineDistSq(Point2D)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    public double ptSegDistSq(Point2D pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        return ptSegDistSq(getX1(), getY1(), getX2(), getY2(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                           pt.getX(), pt.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * Returns the distance from a point to this line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * point and the closest point between the current line's end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * If the specified point intersects the line segment in between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * end points, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @param px the X coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *           measured against this line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @param py the Y coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *           measured against this line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @return a double value that is the distance from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *                  point to the current line segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @see #ptLineDist(double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    public double ptSegDist(double px, double py) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        return ptSegDist(getX1(), getY1(), getX2(), getY2(), px, py);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * Returns the distance from a <code>Point2D</code> to this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * point and the closest point between the current line's end points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * If the specified point intersects the line segment in between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * end points, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param pt the specified <code>Point2D</code> being measured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *          against this line segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @return a double value that is the distance from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *                          <code>Point2D</code> to the current line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *                          segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @see #ptLineDist(Point2D)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    public double ptSegDist(Point2D pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        return ptSegDist(getX1(), getY1(), getX2(), getY2(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                         pt.getX(), pt.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * Returns the square of the distance from a point to a line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * point and the closest point on the infinitely-extended line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * defined by the specified coordinates.  If the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * intersects the line, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @param x1 the X coordinate of the start point of the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @param y1 the Y coordinate of the start point of the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param x2 the X coordinate of the end point of the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @param y2 the Y coordinate of the end point of the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @param px the X coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *           measured against the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @param py the Y coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *           measured against the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @return a double value that is the square of the distance from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *                  specified point to the specified line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @see #ptSegDistSq(double, double, double, double, double, double)
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 double ptLineDistSq(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                                      double x2, double y2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                      double px, double py)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        // Adjust vectors relative to x1,y1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        // x2,y2 becomes relative vector from x1,y1 to end of segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        x2 -= x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        y2 -= y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        // px,py becomes relative vector from x1,y1 to test point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        px -= x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        py -= y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        double dotprod = px * x2 + py * y2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        // dotprod is the length of the px,py vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        // projected on the x1,y1=>x2,y2 vector times the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        // length of the x1,y1=>x2,y2 vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        double projlenSq = dotprod * dotprod / (x2 * x2 + y2 * y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        // Distance to line is now the length of the relative point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        // vector minus the length of its projection onto the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        double lenSq = px * px + py * py - projlenSq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        if (lenSq < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            lenSq = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        return lenSq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Returns the distance from a point to a line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * point and the closest point on the infinitely-extended line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * defined by the specified coordinates.  If the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * intersects the line, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @param x1 the X coordinate of the start point of the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @param y1 the Y coordinate of the start point of the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param x2 the X coordinate of the end point of the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @param y2 the Y coordinate of the end point of the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @param px the X coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *           measured against the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @param py the Y coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *           measured against the specified line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @return a double value that is the distance from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *                   point to the specified line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @see #ptSegDist(double, double, double, double, double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public static double ptLineDist(double x1, double y1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                                    double x2, double y2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                                    double px, double py)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return Math.sqrt(ptLineDistSq(x1, y1, x2, y2, px, py));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Returns the square of the distance from a point to this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * point and the closest point on the infinitely-extended line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * defined by this <code>Line2D</code>.  If the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * intersects the line, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @param px the X coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     *           measured against this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @param py the Y coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *           measured against this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @return a double value that is the square of the distance from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *                  specified point to the current line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @see #ptSegDistSq(double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    public double ptLineDistSq(double px, double py) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), px, py);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * Returns the square of the distance from a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * <code>Point2D</code> to this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * point and the closest point on the infinitely-extended line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * defined by this <code>Line2D</code>.  If the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * intersects the line, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @param pt the specified <code>Point2D</code> being measured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     *           against this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * @return a double value that is the square of the distance from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *                  specified <code>Point2D</code> to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *                  line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @see #ptSegDistSq(Point2D)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    public double ptLineDistSq(Point2D pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        return ptLineDistSq(getX1(), getY1(), getX2(), getY2(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                            pt.getX(), pt.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * Returns the distance from a point to this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * point and the closest point on the infinitely-extended line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * defined by this <code>Line2D</code>.  If the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * intersects the line, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @param px the X coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *           measured against this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @param py the Y coordinate of the specified point being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *           measured against this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @return a double value that is the distance from a specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *                  to the current line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * @see #ptSegDist(double, double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public double ptLineDist(double px, double py) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        return ptLineDist(getX1(), getY1(), getX2(), getY2(), px, py);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * Returns the distance from a <code>Point2D</code> to this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * The distance measured is the distance between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * point and the closest point on the infinitely-extended line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * defined by this <code>Line2D</code>.  If the specified point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * intersects the line, this method returns 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @param pt the specified <code>Point2D</code> being measured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * @return a double value that is the distance from a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *                  <code>Point2D</code> to the current line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @see #ptSegDist(Point2D)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    public double ptLineDist(Point2D pt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        return ptLineDist(getX1(), getY1(), getX2(), getY2(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                         pt.getX(), pt.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * Tests if a specified coordinate is inside the boundary of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * <code>Line2D</code>.  This method is required to implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * {@link Shape} interface, but in the case of <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * objects it always returns <code>false</code> since a line contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * no area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @param x the X coordinate of the specified point to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @param y the Y coordinate of the specified point to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @return <code>false</code> because a <code>Line2D</code> contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * no area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    public boolean contains(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * Tests if a given <code>Point2D</code> is inside the boundary of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * this <code>Line2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * This method is required to implement the {@link Shape} interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * but in the case of <code>Line2D</code> objects it always returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * <code>false</code> since a line contains no area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * @param p the specified <code>Point2D</code> to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @return <code>false</code> because a <code>Line2D</code> contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * no area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    public boolean contains(Point2D p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    public boolean intersects(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        return intersects(new Rectangle2D.Double(x, y, w, h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    public boolean intersects(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        return r.intersectsLine(getX1(), getY1(), getX2(), getY2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * Tests if the interior of this <code>Line2D</code> entirely contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * the specified set of rectangular coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * This method is required to implement the <code>Shape</code> interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * but in the case of <code>Line2D</code> objects it always returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * false since a line contains no area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @param x the X coordinate of the upper-left corner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *          specified rectangular area
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @param y the Y coordinate of the upper-left corner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *          specified rectangular area
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @param w the width of the specified rectangular area
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * @param h the height of the specified rectangular area
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @return <code>false</code> because a <code>Line2D</code> contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * no area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    public boolean contains(double x, double y, double w, double h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * Tests if the interior of this <code>Line2D</code> entirely contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * the specified <code>Rectangle2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * This method is required to implement the <code>Shape</code> interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * but in the case of <code>Line2D</code> objects it always returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * <code>false</code> since a line contains no area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * @param r the specified <code>Rectangle2D</code> to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @return <code>false</code> because a <code>Line2D</code> contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * no area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    public boolean contains(Rectangle2D r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    public Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        return getBounds2D().getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * Returns an iteration object that defines the boundary of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * <code>Line2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * The iterator for this class is not multi-threaded safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * which means that this <code>Line2D</code> class does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * guarantee that modifications to the geometry of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * <code>Line2D</code> object do not affect any iterations of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * geometry that are already in process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @param at the specified {@link AffineTransform}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @return a {@link PathIterator} that defines the boundary of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *          <code>Line2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    public PathIterator getPathIterator(AffineTransform at) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        return new LineIterator(this, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * Returns an iteration object that defines the boundary of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * flattened <code>Line2D</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * The iterator for this class is not multi-threaded safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * which means that this <code>Line2D</code> class does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * guarantee that modifications to the geometry of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * <code>Line2D</code> object do not affect any iterations of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * geometry that are already in process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @param at the specified <code>AffineTransform</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * @param flatness the maximum amount that the control points for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *          given curve can vary from colinear before a subdivided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *          curve is replaced by a straight line connecting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     *          end points.  Since a <code>Line2D</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *          always flat, this parameter is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * @return a <code>PathIterator</code> that defines the boundary of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *                  flattened <code>Line2D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        return new LineIterator(this, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * Creates a new object of the same class as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @return     a clone of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * @exception  OutOfMemoryError            if there is not enough memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * @see        java.lang.Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * @since      1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            // this shouldn't happen, since we are Cloneable
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
  1125
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
}