0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.geom;
0N/A
0N/Aimport java.awt.Shape;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.io.Serializable;
0N/A
0N/A/**
0N/A * This <code>Line2D</code> represents a line segment in {@code (x,y)}
0N/A * coordinate space. This class, like all of the Java 2D API, uses a
0N/A * default coordinate system called <i>user space</i> in which the y-axis
0N/A * values increase downward and x-axis values increase to the right. For
0N/A * more information on the user space coordinate system, see the
0N/A * <a href="http://java.sun.com/j2se/1.3/docs/guide/2d/spec/j2d-intro.fm2.html#61857">
0N/A * Coordinate Systems</a> section of the Java 2D Programmer's Guide.
0N/A * <p>
0N/A * This class is only the abstract superclass for all objects that
0N/A * store a 2D line segment.
0N/A * The actual storage representation of the coordinates is left to
0N/A * the subclass.
0N/A *
0N/A * @author Jim Graham
0N/A * @since 1.2
0N/A */
0N/Apublic abstract class Line2D implements Shape, Cloneable {
0N/A
0N/A /**
0N/A * A line segment specified with float coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static class Float extends Line2D implements Serializable {
0N/A /**
0N/A * The X coordinate of the start point of the line segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float x1;
0N/A
0N/A /**
0N/A * The Y coordinate of the start point of the line segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float y1;
0N/A
0N/A /**
0N/A * The X coordinate of the end point of the line segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float x2;
0N/A
0N/A /**
0N/A * The Y coordinate of the end point of the line segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public float y2;
0N/A
0N/A /**
0N/A * Constructs and initializes a Line with coordinates (0, 0) -> (0, 0).
0N/A * @since 1.2
0N/A */
0N/A public Float() {
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes a Line from the specified coordinates.
0N/A * @param x1 the X coordinate of the start point
0N/A * @param y1 the Y coordinate of the start point
0N/A * @param x2 the X coordinate of the end point
0N/A * @param y2 the Y coordinate of the end point
0N/A * @since 1.2
0N/A */
0N/A public Float(float x1, float y1, float x2, float y2) {
0N/A setLine(x1, y1, x2, y2);
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes a <code>Line2D</code> from the
0N/A * specified <code>Point2D</code> objects.
0N/A * @param p1 the start <code>Point2D</code> of this line segment
0N/A * @param p2 the end <code>Point2D</code> of this line segment
0N/A * @since 1.2
0N/A */
0N/A public Float(Point2D p1, Point2D p2) {
0N/A setLine(p1, p2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX1() {
0N/A return (double) x1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY1() {
0N/A return (double) y1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getP1() {
0N/A return new Point2D.Float(x1, y1);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX2() {
0N/A return (double) x2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY2() {
0N/A return (double) y2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getP2() {
0N/A return new Point2D.Float(x2, y2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setLine(double x1, double y1, double x2, double y2) {
0N/A this.x1 = (float) x1;
0N/A this.y1 = (float) y1;
0N/A this.x2 = (float) x2;
0N/A this.y2 = (float) y2;
0N/A }
0N/A
0N/A /**
0N/A * Sets the location of the end points of this <code>Line2D</code>
0N/A * to the specified float coordinates.
0N/A * @param x1 the X coordinate of the start point
0N/A * @param y1 the Y coordinate of the start point
0N/A * @param x2 the X coordinate of the end point
0N/A * @param y2 the Y coordinate of the end point
0N/A * @since 1.2
0N/A */
0N/A public void setLine(float x1, float y1, float x2, float y2) {
0N/A this.x1 = x1;
0N/A this.y1 = y1;
0N/A this.x2 = x2;
0N/A this.y2 = y2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D getBounds2D() {
0N/A float x, y, w, h;
0N/A if (x1 < x2) {
0N/A x = x1;
0N/A w = x2 - x1;
0N/A } else {
0N/A x = x2;
0N/A w = x1 - x2;
0N/A }
0N/A if (y1 < y2) {
0N/A y = y1;
0N/A h = y2 - y1;
0N/A } else {
0N/A y = y2;
0N/A h = y1 - y2;
0N/A }
0N/A return new Rectangle2D.Float(x, y, w, h);
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 6161772511649436349L;
0N/A }
0N/A
0N/A /**
0N/A * A line segment specified with double coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static class Double extends Line2D implements Serializable {
0N/A /**
0N/A * The X coordinate of the start point of the line segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double x1;
0N/A
0N/A /**
0N/A * The Y coordinate of the start point of the line segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double y1;
0N/A
0N/A /**
0N/A * The X coordinate of the end point of the line segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double x2;
0N/A
0N/A /**
0N/A * The Y coordinate of the end point of the line segment.
0N/A * @since 1.2
0N/A * @serial
0N/A */
0N/A public double y2;
0N/A
0N/A /**
0N/A * Constructs and initializes a Line with coordinates (0, 0) -> (0, 0).
0N/A * @since 1.2
0N/A */
0N/A public Double() {
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes a <code>Line2D</code> from the
0N/A * specified coordinates.
0N/A * @param x1 the X coordinate of the start point
0N/A * @param y1 the Y coordinate of the start point
0N/A * @param x2 the X coordinate of the end point
0N/A * @param y2 the Y coordinate of the end point
0N/A * @since 1.2
0N/A */
0N/A public Double(double x1, double y1, double x2, double y2) {
0N/A setLine(x1, y1, x2, y2);
0N/A }
0N/A
0N/A /**
0N/A * Constructs and initializes a <code>Line2D</code> from the
0N/A * specified <code>Point2D</code> objects.
0N/A * @param p1 the start <code>Point2D</code> of this line segment
0N/A * @param p2 the end <code>Point2D</code> of this line segment
0N/A * @since 1.2
0N/A */
0N/A public Double(Point2D p1, Point2D p2) {
0N/A setLine(p1, p2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX1() {
0N/A return x1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY1() {
0N/A return y1;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getP1() {
0N/A return new Point2D.Double(x1, y1);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getX2() {
0N/A return x2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public double getY2() {
0N/A return y2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Point2D getP2() {
0N/A return new Point2D.Double(x2, y2);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public void setLine(double x1, double y1, double x2, double y2) {
0N/A this.x1 = x1;
0N/A this.y1 = y1;
0N/A this.x2 = x2;
0N/A this.y2 = y2;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle2D getBounds2D() {
0N/A double x, y, w, h;
0N/A if (x1 < x2) {
0N/A x = x1;
0N/A w = x2 - x1;
0N/A } else {
0N/A x = x2;
0N/A w = x1 - x2;
0N/A }
0N/A if (y1 < y2) {
0N/A y = y1;
0N/A h = y2 - y1;
0N/A } else {
0N/A y = y2;
0N/A h = y1 - y2;
0N/A }
0N/A return new Rectangle2D.Double(x, y, w, h);
0N/A }
0N/A
0N/A /*
0N/A * JDK 1.6 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 7979627399746467499L;
0N/A }
0N/A
0N/A /**
0N/A * This is an abstract class that cannot be instantiated directly.
0N/A * Type-specific implementation subclasses are available for
0N/A * instantiation and provide a number of formats for storing
0N/A * the information necessary to satisfy the various accessory
0N/A * methods below.
0N/A *
0N/A * @see java.awt.geom.Line2D.Float
0N/A * @see java.awt.geom.Line2D.Double
0N/A * @since 1.2
0N/A */
0N/A protected Line2D() {
0N/A }
0N/A
0N/A /**
0N/A * Returns the X coordinate of the start point in double precision.
0N/A * @return the X coordinate of the start point of this
0N/A * {@code Line2D} object.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getX1();
0N/A
0N/A /**
0N/A * Returns the Y coordinate of the start point in double precision.
0N/A * @return the Y coordinate of the start point of this
0N/A * {@code Line2D} object.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getY1();
0N/A
0N/A /**
0N/A * Returns the start <code>Point2D</code> of this <code>Line2D</code>.
0N/A * @return the start <code>Point2D</code> of this <code>Line2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public abstract Point2D getP1();
0N/A
0N/A /**
0N/A * Returns the X coordinate of the end point in double precision.
0N/A * @return the X coordinate of the end point of this
0N/A * {@code Line2D} object.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getX2();
0N/A
0N/A /**
0N/A * Returns the Y coordinate of the end point in double precision.
0N/A * @return the Y coordinate of the end point of this
0N/A * {@code Line2D} object.
0N/A * @since 1.2
0N/A */
0N/A public abstract double getY2();
0N/A
0N/A /**
0N/A * Returns the end <code>Point2D</code> of this <code>Line2D</code>.
0N/A * @return the end <code>Point2D</code> of this <code>Line2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public abstract Point2D getP2();
0N/A
0N/A /**
0N/A * Sets the location of the end points of this <code>Line2D</code> to
0N/A * the specified double coordinates.
0N/A * @param x1 the X coordinate of the start point
0N/A * @param y1 the Y coordinate of the start point
0N/A * @param x2 the X coordinate of the end point
0N/A * @param y2 the Y coordinate of the end point
0N/A * @since 1.2
0N/A */
0N/A public abstract void setLine(double x1, double y1, double x2, double y2);
0N/A
0N/A /**
0N/A * Sets the location of the end points of this <code>Line2D</code> to
0N/A * the specified <code>Point2D</code> coordinates.
0N/A * @param p1 the start <code>Point2D</code> of the line segment
0N/A * @param p2 the end <code>Point2D</code> of the line segment
0N/A * @since 1.2
0N/A */
0N/A public void setLine(Point2D p1, Point2D p2) {
0N/A setLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
0N/A }
0N/A
0N/A /**
0N/A * Sets the location of the end points of this <code>Line2D</code> to
0N/A * the same as those end points of the specified <code>Line2D</code>.
0N/A * @param l the specified <code>Line2D</code>
0N/A * @since 1.2
0N/A */
0N/A public void setLine(Line2D l) {
0N/A setLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
0N/A }
0N/A
0N/A /**
0N/A * Returns an indicator of where the specified point
0N/A * {@code (px,py)} lies with respect to the line segment from
0N/A * {@code (x1,y1)} to {@code (x2,y2)}.
0N/A * The return value can be either 1, -1, or 0 and indicates
0N/A * in which direction the specified line must pivot around its
0N/A * first end point, {@code (x1,y1)}, in order to point at the
0N/A * specified point {@code (px,py)}.
0N/A * <p>A return value of 1 indicates that the line segment must
0N/A * turn in the direction that takes the positive X axis towards
0N/A * the negative Y axis. In the default coordinate system used by
0N/A * Java 2D, this direction is counterclockwise.
0N/A * <p>A return value of -1 indicates that the line segment must
0N/A * turn in the direction that takes the positive X axis towards
0N/A * the positive Y axis. In the default coordinate system, this
0N/A * direction is clockwise.
0N/A * <p>A return value of 0 indicates that the point lies
0N/A * exactly on the line segment. Note that an indicator value
0N/A * of 0 is rare and not useful for determining colinearity
0N/A * because of floating point rounding issues.
0N/A * <p>If the point is colinear with the line segment, but
0N/A * not between the end points, then the value will be -1 if the point
0N/A * lies "beyond {@code (x1,y1)}" or 1 if the point lies
0N/A * "beyond {@code (x2,y2)}".
0N/A *
0N/A * @param x1 the X coordinate of the start point of the
0N/A * specified line segment
0N/A * @param y1 the Y coordinate of the start point of the
0N/A * specified line segment
0N/A * @param x2 the X coordinate of the end point of the
0N/A * specified line segment
0N/A * @param y2 the Y coordinate of the end point of the
0N/A * specified line segment
0N/A * @param px the X coordinate of the specified point to be
0N/A * compared with the specified line segment
0N/A * @param py the Y coordinate of the specified point to be
0N/A * compared with the specified line segment
0N/A * @return an integer that indicates the position of the third specified
0N/A * coordinates with respect to the line segment formed
0N/A * by the first two specified coordinates.
0N/A * @since 1.2
0N/A */
0N/A public static int relativeCCW(double x1, double y1,
0N/A double x2, double y2,
0N/A double px, double py)
0N/A {
0N/A x2 -= x1;
0N/A y2 -= y1;
0N/A px -= x1;
0N/A py -= y1;
0N/A double ccw = px * y2 - py * x2;
0N/A if (ccw == 0.0) {
0N/A // The point is colinear, classify based on which side of
0N/A // the segment the point falls on. We can calculate a
0N/A // relative value using the projection of px,py onto the
0N/A // segment - a negative value indicates the point projects
0N/A // outside of the segment in the direction of the particular
0N/A // endpoint used as the origin for the projection.
0N/A ccw = px * x2 + py * y2;
0N/A if (ccw > 0.0) {
0N/A // Reverse the projection to be relative to the original x2,y2
0N/A // x2 and y2 are simply negated.
0N/A // px and py need to have (x2 - x1) or (y2 - y1) subtracted
0N/A // from them (based on the original values)
0N/A // Since we really want to get a positive answer when the
0N/A // point is "beyond (x2,y2)", then we want to calculate
0N/A // the inverse anyway - thus we leave x2 & y2 negated.
0N/A px -= x2;
0N/A py -= y2;
0N/A ccw = px * x2 + py * y2;
0N/A if (ccw < 0.0) {
0N/A ccw = 0.0;
0N/A }
0N/A }
0N/A }
0N/A return (ccw < 0.0) ? -1 : ((ccw > 0.0) ? 1 : 0);
0N/A }
0N/A
0N/A /**
0N/A * Returns an indicator of where the specified point
0N/A * {@code (px,py)} lies with respect to this line segment.
0N/A * See the method comments of
0N/A * {@link #relativeCCW(double, double, double, double, double, double)}
0N/A * to interpret the return value.
0N/A * @param px the X coordinate of the specified point
0N/A * to be compared with this <code>Line2D</code>
0N/A * @param py the Y coordinate of the specified point
0N/A * to be compared with this <code>Line2D</code>
0N/A * @return an integer that indicates the position of the specified
0N/A * coordinates with respect to this <code>Line2D</code>
0N/A * @see #relativeCCW(double, double, double, double, double, double)
0N/A * @since 1.2
0N/A */
0N/A public int relativeCCW(double px, double py) {
0N/A return relativeCCW(getX1(), getY1(), getX2(), getY2(), px, py);
0N/A }
0N/A
0N/A /**
0N/A * Returns an indicator of where the specified <code>Point2D</code>
0N/A * lies with respect to this line segment.
0N/A * See the method comments of
0N/A * {@link #relativeCCW(double, double, double, double, double, double)}
0N/A * to interpret the return value.
0N/A * @param p the specified <code>Point2D</code> to be compared
0N/A * with this <code>Line2D</code>
0N/A * @return an integer that indicates the position of the specified
0N/A * <code>Point2D</code> with respect to this <code>Line2D</code>
0N/A * @see #relativeCCW(double, double, double, double, double, double)
0N/A * @since 1.2
0N/A */
0N/A public int relativeCCW(Point2D p) {
0N/A return relativeCCW(getX1(), getY1(), getX2(), getY2(),
0N/A p.getX(), p.getY());
0N/A }
0N/A
0N/A /**
0N/A * Tests if the line segment from {@code (x1,y1)} to
0N/A * {@code (x2,y2)} intersects the line segment from {@code (x3,y3)}
0N/A * to {@code (x4,y4)}.
0N/A *
0N/A * @param x1 the X coordinate of the start point of the first
0N/A * specified line segment
0N/A * @param y1 the Y coordinate of the start point of the first
0N/A * specified line segment
0N/A * @param x2 the X coordinate of the end point of the first
0N/A * specified line segment
0N/A * @param y2 the Y coordinate of the end point of the first
0N/A * specified line segment
0N/A * @param x3 the X coordinate of the start point of the second
0N/A * specified line segment
0N/A * @param y3 the Y coordinate of the start point of the second
0N/A * specified line segment
0N/A * @param x4 the X coordinate of the end point of the second
0N/A * specified line segment
0N/A * @param y4 the Y coordinate of the end point of the second
0N/A * specified line segment
0N/A * @return <code>true</code> if the first specified line segment
0N/A * and the second specified line segment intersect
0N/A * each other; <code>false</code> otherwise.
0N/A * @since 1.2
0N/A */
0N/A public static boolean linesIntersect(double x1, double y1,
0N/A double x2, double y2,
0N/A double x3, double y3,
0N/A double x4, double y4)
0N/A {
0N/A return ((relativeCCW(x1, y1, x2, y2, x3, y3) *
0N/A relativeCCW(x1, y1, x2, y2, x4, y4) <= 0)
0N/A && (relativeCCW(x3, y3, x4, y4, x1, y1) *
0N/A relativeCCW(x3, y3, x4, y4, x2, y2) <= 0));
0N/A }
0N/A
0N/A /**
0N/A * Tests if the line segment from {@code (x1,y1)} to
0N/A * {@code (x2,y2)} intersects this line segment.
0N/A *
0N/A * @param x1 the X coordinate of the start point of the
0N/A * specified line segment
0N/A * @param y1 the Y coordinate of the start point of the
0N/A * specified line segment
0N/A * @param x2 the X coordinate of the end point of the
0N/A * specified line segment
0N/A * @param y2 the Y coordinate of the end point of the
0N/A * specified line segment
0N/A * @return <true> if this line segment and the specified line segment
0N/A * intersect each other; <code>false</code> otherwise.
0N/A * @since 1.2
0N/A */
0N/A public boolean intersectsLine(double x1, double y1, double x2, double y2) {
0N/A return linesIntersect(x1, y1, x2, y2,
0N/A getX1(), getY1(), getX2(), getY2());
0N/A }
0N/A
0N/A /**
0N/A * Tests if the specified line segment intersects this line segment.
0N/A * @param l the specified <code>Line2D</code>
0N/A * @return <code>true</code> if this line segment and the specified line
0N/A * segment intersect each other;
0N/A * <code>false</code> otherwise.
0N/A * @since 1.2
0N/A */
0N/A public boolean intersectsLine(Line2D l) {
0N/A return linesIntersect(l.getX1(), l.getY1(), l.getX2(), l.getY2(),
0N/A getX1(), getY1(), getX2(), getY2());
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the distance from a point to a line segment.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point between the specified end points.
0N/A * If the specified point intersects the line segment in between the
0N/A * end points, this method returns 0.0.
0N/A *
0N/A * @param x1 the X coordinate of the start point of the
0N/A * specified line segment
0N/A * @param y1 the Y coordinate of the start point of the
0N/A * specified line segment
0N/A * @param x2 the X coordinate of the end point of the
0N/A * specified line segment
0N/A * @param y2 the Y coordinate of the end point of the
0N/A * specified line segment
0N/A * @param px the X coordinate of the specified point being
0N/A * measured against the specified line segment
0N/A * @param py the Y coordinate of the specified point being
0N/A * measured against the specified line segment
0N/A * @return a double value that is the square of the distance from the
0N/A * specified point to the specified line segment.
0N/A * @see #ptLineDistSq(double, double, double, double, double, double)
0N/A * @since 1.2
0N/A */
0N/A public static double ptSegDistSq(double x1, double y1,
0N/A double x2, double y2,
0N/A double px, double py)
0N/A {
0N/A // Adjust vectors relative to x1,y1
0N/A // x2,y2 becomes relative vector from x1,y1 to end of segment
0N/A x2 -= x1;
0N/A y2 -= y1;
0N/A // px,py becomes relative vector from x1,y1 to test point
0N/A px -= x1;
0N/A py -= y1;
0N/A double dotprod = px * x2 + py * y2;
0N/A double projlenSq;
0N/A if (dotprod <= 0.0) {
0N/A // px,py is on the side of x1,y1 away from x2,y2
0N/A // distance to segment is length of px,py vector
0N/A // "length of its (clipped) projection" is now 0.0
0N/A projlenSq = 0.0;
0N/A } else {
0N/A // switch to backwards vectors relative to x2,y2
0N/A // x2,y2 are already the negative of x1,y1=>x2,y2
0N/A // to get px,py to be the negative of px,py=>x2,y2
0N/A // the dot product of two negated vectors is the same
0N/A // as the dot product of the two normal vectors
0N/A px = x2 - px;
0N/A py = y2 - py;
0N/A dotprod = px * x2 + py * y2;
0N/A if (dotprod <= 0.0) {
0N/A // px,py is on the side of x2,y2 away from x1,y1
0N/A // distance to segment is length of (backwards) px,py vector
0N/A // "length of its (clipped) projection" is now 0.0
0N/A projlenSq = 0.0;
0N/A } else {
0N/A // px,py is between x1,y1 and x2,y2
0N/A // dotprod is the length of the px,py vector
0N/A // projected on the x2,y2=>x1,y1 vector times the
0N/A // length of the x2,y2=>x1,y1 vector
0N/A projlenSq = dotprod * dotprod / (x2 * x2 + y2 * y2);
0N/A }
0N/A }
0N/A // Distance to line is now the length of the relative point
0N/A // vector minus the length of its projection onto the line
0N/A // (which is zero if the projection falls outside the range
0N/A // of the line segment).
0N/A double lenSq = px * px + py * py - projlenSq;
0N/A if (lenSq < 0) {
0N/A lenSq = 0;
0N/A }
0N/A return lenSq;
0N/A }
0N/A
0N/A /**
0N/A * Returns the distance from a point to a line segment.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point between the specified end points.
0N/A * If the specified point intersects the line segment in between the
0N/A * end points, this method returns 0.0.
0N/A *
0N/A * @param x1 the X coordinate of the start point of the
0N/A * specified line segment
0N/A * @param y1 the Y coordinate of the start point of the
0N/A * specified line segment
0N/A * @param x2 the X coordinate of the end point of the
0N/A * specified line segment
0N/A * @param y2 the Y coordinate of the end point of the
0N/A * specified line segment
0N/A * @param px the X coordinate of the specified point being
0N/A * measured against the specified line segment
0N/A * @param py the Y coordinate of the specified point being
0N/A * measured against the specified line segment
0N/A * @return a double value that is the distance from the specified point
0N/A * to the specified line segment.
0N/A * @see #ptLineDist(double, double, double, double, double, double)
0N/A * @since 1.2
0N/A */
0N/A public static double ptSegDist(double x1, double y1,
0N/A double x2, double y2,
0N/A double px, double py)
0N/A {
0N/A return Math.sqrt(ptSegDistSq(x1, y1, x2, y2, px, py));
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the distance from a point to this line segment.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point between the current line's end points.
0N/A * If the specified point intersects the line segment in between the
0N/A * end points, this method returns 0.0.
0N/A *
0N/A * @param px the X coordinate of the specified point being
0N/A * measured against this line segment
0N/A * @param py the Y coordinate of the specified point being
0N/A * measured against this line segment
0N/A * @return a double value that is the square of the distance from the
0N/A * specified point to the current line segment.
0N/A * @see #ptLineDistSq(double, double)
0N/A * @since 1.2
0N/A */
0N/A public double ptSegDistSq(double px, double py) {
0N/A return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), px, py);
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the distance from a <code>Point2D</code> to
0N/A * this line segment.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point between the current line's end points.
0N/A * If the specified point intersects the line segment in between the
0N/A * end points, this method returns 0.0.
0N/A * @param pt the specified <code>Point2D</code> being measured against
0N/A * this line segment.
0N/A * @return a double value that is the square of the distance from the
0N/A * specified <code>Point2D</code> to the current
0N/A * line segment.
0N/A * @see #ptLineDistSq(Point2D)
0N/A * @since 1.2
0N/A */
0N/A public double ptSegDistSq(Point2D pt) {
0N/A return ptSegDistSq(getX1(), getY1(), getX2(), getY2(),
0N/A pt.getX(), pt.getY());
0N/A }
0N/A
0N/A /**
0N/A * Returns the distance from a point to this line segment.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point between the current line's end points.
0N/A * If the specified point intersects the line segment in between the
0N/A * end points, this method returns 0.0.
0N/A *
0N/A * @param px the X coordinate of the specified point being
0N/A * measured against this line segment
0N/A * @param py the Y coordinate of the specified point being
0N/A * measured against this line segment
0N/A * @return a double value that is the distance from the specified
0N/A * point to the current line segment.
0N/A * @see #ptLineDist(double, double)
0N/A * @since 1.2
0N/A */
0N/A public double ptSegDist(double px, double py) {
0N/A return ptSegDist(getX1(), getY1(), getX2(), getY2(), px, py);
0N/A }
0N/A
0N/A /**
0N/A * Returns the distance from a <code>Point2D</code> to this line
0N/A * segment.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point between the current line's end points.
0N/A * If the specified point intersects the line segment in between the
0N/A * end points, this method returns 0.0.
0N/A * @param pt the specified <code>Point2D</code> being measured
0N/A * against this line segment
0N/A * @return a double value that is the distance from the specified
0N/A * <code>Point2D</code> to the current line
0N/A * segment.
0N/A * @see #ptLineDist(Point2D)
0N/A * @since 1.2
0N/A */
0N/A public double ptSegDist(Point2D pt) {
0N/A return ptSegDist(getX1(), getY1(), getX2(), getY2(),
0N/A pt.getX(), pt.getY());
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the distance from a point to a line.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point on the infinitely-extended line
0N/A * defined by the specified coordinates. If the specified point
0N/A * intersects the line, this method returns 0.0.
0N/A *
0N/A * @param x1 the X coordinate of the start point of the specified line
0N/A * @param y1 the Y coordinate of the start point of the specified line
0N/A * @param x2 the X coordinate of the end point of the specified line
0N/A * @param y2 the Y coordinate of the end point of the specified line
0N/A * @param px the X coordinate of the specified point being
0N/A * measured against the specified line
0N/A * @param py the Y coordinate of the specified point being
0N/A * measured against the specified line
0N/A * @return a double value that is the square of the distance from the
0N/A * specified point to the specified line.
0N/A * @see #ptSegDistSq(double, double, double, double, double, double)
0N/A * @since 1.2
0N/A */
0N/A public static double ptLineDistSq(double x1, double y1,
0N/A double x2, double y2,
0N/A double px, double py)
0N/A {
0N/A // Adjust vectors relative to x1,y1
0N/A // x2,y2 becomes relative vector from x1,y1 to end of segment
0N/A x2 -= x1;
0N/A y2 -= y1;
0N/A // px,py becomes relative vector from x1,y1 to test point
0N/A px -= x1;
0N/A py -= y1;
0N/A double dotprod = px * x2 + py * y2;
0N/A // dotprod is the length of the px,py vector
0N/A // projected on the x1,y1=>x2,y2 vector times the
0N/A // length of the x1,y1=>x2,y2 vector
0N/A double projlenSq = dotprod * dotprod / (x2 * x2 + y2 * y2);
0N/A // Distance to line is now the length of the relative point
0N/A // vector minus the length of its projection onto the line
0N/A double lenSq = px * px + py * py - projlenSq;
0N/A if (lenSq < 0) {
0N/A lenSq = 0;
0N/A }
0N/A return lenSq;
0N/A }
0N/A
0N/A /**
0N/A * Returns the distance from a point to a line.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point on the infinitely-extended line
0N/A * defined by the specified coordinates. If the specified point
0N/A * intersects the line, this method returns 0.0.
0N/A *
0N/A * @param x1 the X coordinate of the start point of the specified line
0N/A * @param y1 the Y coordinate of the start point of the specified line
0N/A * @param x2 the X coordinate of the end point of the specified line
0N/A * @param y2 the Y coordinate of the end point of the specified line
0N/A * @param px the X coordinate of the specified point being
0N/A * measured against the specified line
0N/A * @param py the Y coordinate of the specified point being
0N/A * measured against the specified line
0N/A * @return a double value that is the distance from the specified
0N/A * point to the specified line.
0N/A * @see #ptSegDist(double, double, double, double, double, double)
0N/A * @since 1.2
0N/A */
0N/A public static double ptLineDist(double x1, double y1,
0N/A double x2, double y2,
0N/A double px, double py)
0N/A {
0N/A return Math.sqrt(ptLineDistSq(x1, y1, x2, y2, px, py));
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the distance from a point to this line.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point on the infinitely-extended line
0N/A * defined by this <code>Line2D</code>. If the specified point
0N/A * intersects the line, this method returns 0.0.
0N/A *
0N/A * @param px the X coordinate of the specified point being
0N/A * measured against this line
0N/A * @param py the Y coordinate of the specified point being
0N/A * measured against this line
0N/A * @return a double value that is the square of the distance from a
0N/A * specified point to the current line.
0N/A * @see #ptSegDistSq(double, double)
0N/A * @since 1.2
0N/A */
0N/A public double ptLineDistSq(double px, double py) {
0N/A return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), px, py);
0N/A }
0N/A
0N/A /**
0N/A * Returns the square of the distance from a specified
0N/A * <code>Point2D</code> to this line.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point on the infinitely-extended line
0N/A * defined by this <code>Line2D</code>. If the specified point
0N/A * intersects the line, this method returns 0.0.
0N/A * @param pt the specified <code>Point2D</code> being measured
0N/A * against this line
0N/A * @return a double value that is the square of the distance from a
0N/A * specified <code>Point2D</code> to the current
0N/A * line.
0N/A * @see #ptSegDistSq(Point2D)
0N/A * @since 1.2
0N/A */
0N/A public double ptLineDistSq(Point2D pt) {
0N/A return ptLineDistSq(getX1(), getY1(), getX2(), getY2(),
0N/A pt.getX(), pt.getY());
0N/A }
0N/A
0N/A /**
0N/A * Returns the distance from a point to this line.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point on the infinitely-extended line
0N/A * defined by this <code>Line2D</code>. If the specified point
0N/A * intersects the line, this method returns 0.0.
0N/A *
0N/A * @param px the X coordinate of the specified point being
0N/A * measured against this line
0N/A * @param py the Y coordinate of the specified point being
0N/A * measured against this line
0N/A * @return a double value that is the distance from a specified point
0N/A * to the current line.
0N/A * @see #ptSegDist(double, double)
0N/A * @since 1.2
0N/A */
0N/A public double ptLineDist(double px, double py) {
0N/A return ptLineDist(getX1(), getY1(), getX2(), getY2(), px, py);
0N/A }
0N/A
0N/A /**
0N/A * Returns the distance from a <code>Point2D</code> to this line.
0N/A * The distance measured is the distance between the specified
0N/A * point and the closest point on the infinitely-extended line
0N/A * defined by this <code>Line2D</code>. If the specified point
0N/A * intersects the line, this method returns 0.0.
0N/A * @param pt the specified <code>Point2D</code> being measured
0N/A * @return a double value that is the distance from a specified
0N/A * <code>Point2D</code> to the current line.
0N/A * @see #ptSegDist(Point2D)
0N/A * @since 1.2
0N/A */
0N/A public double ptLineDist(Point2D pt) {
0N/A return ptLineDist(getX1(), getY1(), getX2(), getY2(),
0N/A pt.getX(), pt.getY());
0N/A }
0N/A
0N/A /**
0N/A * Tests if a specified coordinate is inside the boundary of this
0N/A * <code>Line2D</code>. This method is required to implement the
0N/A * {@link Shape} interface, but in the case of <code>Line2D</code>
0N/A * objects it always returns <code>false</code> since a line contains
0N/A * no area.
0N/A * @param x the X coordinate of the specified point to be tested
0N/A * @param y the Y coordinate of the specified point to be tested
0N/A * @return <code>false</code> because a <code>Line2D</code> contains
0N/A * no area.
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(double x, double y) {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Tests if a given <code>Point2D</code> is inside the boundary of
0N/A * this <code>Line2D</code>.
0N/A * This method is required to implement the {@link Shape} interface,
0N/A * but in the case of <code>Line2D</code> objects it always returns
0N/A * <code>false</code> since a line contains no area.
0N/A * @param p the specified <code>Point2D</code> to be tested
0N/A * @return <code>false</code> because a <code>Line2D</code> contains
0N/A * no area.
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(Point2D p) {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean intersects(double x, double y, double w, double h) {
0N/A return intersects(new Rectangle2D.Double(x, y, w, h));
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public boolean intersects(Rectangle2D r) {
0N/A return r.intersectsLine(getX1(), getY1(), getX2(), getY2());
0N/A }
0N/A
0N/A /**
0N/A * Tests if the interior of this <code>Line2D</code> entirely contains
0N/A * the specified set of rectangular coordinates.
0N/A * This method is required to implement the <code>Shape</code> interface,
0N/A * but in the case of <code>Line2D</code> objects it always returns
0N/A * false since a line contains no area.
0N/A * @param x the X coordinate of the upper-left corner of the
0N/A * specified rectangular area
0N/A * @param y the Y coordinate of the upper-left corner of the
0N/A * specified rectangular area
0N/A * @param w the width of the specified rectangular area
0N/A * @param h the height of the specified rectangular area
0N/A * @return <code>false</code> because a <code>Line2D</code> contains
0N/A * no area.
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(double x, double y, double w, double h) {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Tests if the interior of this <code>Line2D</code> entirely contains
0N/A * the specified <code>Rectangle2D</code>.
0N/A * This method is required to implement the <code>Shape</code> interface,
0N/A * but in the case of <code>Line2D</code> objects it always returns
0N/A * <code>false</code> since a line contains no area.
0N/A * @param r the specified <code>Rectangle2D</code> to be tested
0N/A * @return <code>false</code> because a <code>Line2D</code> contains
0N/A * no area.
0N/A * @since 1.2
0N/A */
0N/A public boolean contains(Rectangle2D r) {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.2
0N/A */
0N/A public Rectangle getBounds() {
0N/A return getBounds2D().getBounds();
0N/A }
0N/A
0N/A /**
0N/A * Returns an iteration object that defines the boundary of this
0N/A * <code>Line2D</code>.
0N/A * The iterator for this class is not multi-threaded safe,
0N/A * which means that this <code>Line2D</code> class does not
0N/A * guarantee that modifications to the geometry of this
0N/A * <code>Line2D</code> object do not affect any iterations of that
0N/A * geometry that are already in process.
0N/A * @param at the specified {@link AffineTransform}
0N/A * @return a {@link PathIterator} that defines the boundary of this
0N/A * <code>Line2D</code>.
0N/A * @since 1.2
0N/A */
0N/A public PathIterator getPathIterator(AffineTransform at) {
0N/A return new LineIterator(this, at);
0N/A }
0N/A
0N/A /**
0N/A * Returns an iteration object that defines the boundary of this
0N/A * flattened <code>Line2D</code>.
0N/A * The iterator for this class is not multi-threaded safe,
0N/A * which means that this <code>Line2D</code> class does not
0N/A * guarantee that modifications to the geometry of this
0N/A * <code>Line2D</code> object do not affect any iterations of that
0N/A * geometry that are already in process.
0N/A * @param at the specified <code>AffineTransform</code>
0N/A * @param flatness the maximum amount that the control points for a
0N/A * given curve can vary from colinear before a subdivided
0N/A * curve is replaced by a straight line connecting the
0N/A * end points. Since a <code>Line2D</code> object is
0N/A * always flat, this parameter is ignored.
0N/A * @return a <code>PathIterator</code> that defines the boundary of the
0N/A * flattened <code>Line2D</code>
0N/A * @since 1.2
0N/A */
0N/A public PathIterator getPathIterator(AffineTransform at, double flatness) {
0N/A return new LineIterator(this, at);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new object of the same class as this object.
0N/A *
0N/A * @return a clone of this instance.
0N/A * @exception OutOfMemoryError if there is not enough memory.
0N/A * @see java.lang.Cloneable
0N/A * @since 1.2
0N/A */
0N/A public Object clone() {
0N/A try {
0N/A return super.clone();
0N/A } catch (CloneNotSupportedException e) {
0N/A // this shouldn't happen, since we are Cloneable
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A}