java.awt.geom

Class Line2D

Implemented Interfaces:
Cloneable, Shape
Known Direct Subclasses:
Line2D.Double, Line2D.Float

public abstract class Line2D
extends Object
implements Shape, Cloneable

Represents a directed line bewteen two points in (x,y) Cartesian space. Remember, on-screen graphics have increasing x from left-to-right, and increasing y from top-to-bottom. The storage is left to subclasses.
Since:
1.2

Nested Class Summary

static class
Line2D.Double
This class defines a point in double precision.
static class
Line2D.Float
This class defines a point in float precision.

Constructor Summary

Line2D()
The default constructor.

Method Summary

Object
clone()
Create a new line of the same run-time type with the same contents as this one.
boolean
contains(double x, double y)
Test if a point is contained inside the line.
boolean
contains(double x, double y, double w, double h)
Tests if the line contains a rectangle.
boolean
contains(Point2D p)
Test if a point is contained inside the line.
boolean
contains(Rectangle2D r)
Tests if the line contains a rectangle.
Rectangle
getBounds()
Gets a bounding box (not necessarily minimal) for this line.
abstract Point2D
getP1()
Return the first point.
abstract Point2D
getP2()
Return the second point.
PathIterator
getPathIterator(AffineTransform at)
Return a path iterator, possibly applying a transform on the result.
PathIterator
getPathIterator(AffineTransform at, double flatness)
Return a flat path iterator, possibly applying a transform on the result.
abstract double
getX1()
Return the x coordinate of the first point.
abstract double
getX2()
Return the x coordinate of the second point.
abstract double
getY1()
Return the y coordinate of the first point.
abstract double
getY2()
Return the y coordinate of the second point.
boolean
intersects(double x, double y, double w, double h)
Tests if this line intersects the interior of the specified rectangle.
boolean
intersects(Rectangle2D r)
Tests if this line intersects the interior of the specified rectangle.
boolean
intersectsLine(double x1, double y1, double x2, double y2)
Test if this line intersects the line given by (x1,y1)->(x2,y2).
boolean
intersectsLine(Line2D l)
Test if this line intersects the given line.
static boolean
linesIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
Test if the line segment (x1,y1)->(x2,y2) intersects the line segment (x3,y3)->(x4,y4).
double
ptLineDist(double px, double py)
Measures the shortest distance from the reference point to a point on the infinite line extended from this segment.
static double
ptLineDist(double x1, double y1, double x2, double y2, double px, double py)
Measures the shortest distance from the reference point to a point on the infinite line extended from the segment.
double
ptLineDist(Point2D p)
Measures the shortest distance from the reference point to a point on the infinite line extended from this segment.
double
ptLineDistSq(double px, double py)
Measures the square of the shortest distance from the reference point to a point on the infinite line extended from this segment.
static double
ptLineDistSq(double x1, double y1, double x2, double y2, double px, double py)
Measures the square of the shortest distance from the reference point to a point on the infinite line extended from the segment.
double
ptLineDistSq(Point2D p)
Measures the square of the shortest distance from the reference point to a point on the infinite line extended from this segment.
double
ptSegDist(double px, double py)
Measures the shortest distance from the reference point to a point on this line segment.
static double
ptSegDist(double x1, double y1, double x2, double y2, double px, double py)
Measures the shortest distance from the reference point to a point on the line segment.
double
ptSegDist(Point2D p)
Measures the shortest distance from the reference point to a point on this line segment.
double
ptSegDistSq(double px, double py)
Measures the square of the shortest distance from the reference point to a point on this line segment.
static double
ptSegDistSq(double x1, double y1, double x2, double y2, double px, double py)
Measures the square of the shortest distance from the reference point to a point on the line segment.
double
ptSegDistSq(Point2D p)
Measures the square of the shortest distance from the reference point to a point on this line segment.
int
relativeCCW(double px, double py)
Computes the relative rotation direction needed to pivot this line about the first point in order to have the second point colinear with point p.
static int
relativeCCW(double x1, double y1, double x2, double y2, double px, double py)
Computes the relative rotation direction needed to pivot the line about the first point in order to have the second point colinear with point p.
int
relativeCCW(Point2D p)
Computes the relative rotation direction needed to pivot this line about the first point in order to have the second point colinear with point p.
abstract void
setLine(double x1, double y1, double x2, double y2)
Set the coordinates of the line to the given coordinates.
void
setLine(Line2D l)
Set the coordinates to those of the given line.
void
setLine(Point2D p1, Point2D p2)
Set the coordinates to the given points.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

Line2D

protected Line2D()
The default constructor.

Method Details

clone

public Object clone()
Create a new line of the same run-time type with the same contents as this one.
Overrides:
clone in interface Object
Returns:
the clone
Since:
1.2

contains

public boolean contains(double x,
                        double y)
Test if a point is contained inside the line. Since a line has no area, this returns false.
Specified by:
contains in interface Shape
Parameters:
x - the x coordinate
y - the y coordinate
Returns:
false; the line does not contain points

contains

public boolean contains(double x,
                        double y,
                        double w,
                        double h)
Tests if the line contains a rectangle. Since lines have no area, this always returns false.
Specified by:
contains in interface Shape
Parameters:
x - the x coordinate of the rectangle
y - the y coordinate of the rectangle
w - the width of the rectangle
h - the height of the rectangle
Returns:
false; the line does not contain points

contains

public boolean contains(Point2D p)
Test if a point is contained inside the line. Since a line has no area, this returns false.
Specified by:
contains in interface Shape
Parameters:
p - the point
Returns:
false; the line does not contain points

contains

public boolean contains(Rectangle2D r)
Tests if the line contains a rectangle. Since lines have no area, this always returns false.
Specified by:
contains in interface Shape
Parameters:
r - the rectangle
Returns:
false; the line does not contain points

getBounds

public Rectangle getBounds()
Gets a bounding box (not necessarily minimal) for this line.
Specified by:
getBounds in interface Shape
Returns:
the integer bounding box
See Also:
getBounds2D()

getP1

public abstract Point2D getP1()
Return the first point.
Returns:
the starting point

getP2

public abstract Point2D getP2()
Return the second point.
Returns:
the ending point

getPathIterator

public PathIterator getPathIterator(AffineTransform at)
Return a path iterator, possibly applying a transform on the result. This iterator is not threadsafe.
Specified by:
getPathIterator in interface Shape
Parameters:
at - the transform, or null
Returns:
a new path iterator

getPathIterator

public PathIterator getPathIterator(AffineTransform at,
                                    double flatness)
Return a flat path iterator, possibly applying a transform on the result. This iterator is not threadsafe.
Specified by:
getPathIterator in interface Shape
Parameters:
at - the transform, or null
flatness - ignored, since lines are already flat
Returns:
a new path iterator

getX1

public abstract double getX1()
Return the x coordinate of the first point.
Returns:
the starting x coordinate

getX2

public abstract double getX2()
Return the x coordinate of the second point.
Returns:
the ending x coordinate

getY1

public abstract double getY1()
Return the y coordinate of the first point.
Returns:
the starting y coordinate

getY2

public abstract double getY2()
Return the y coordinate of the second point.
Returns:
the ending y coordinate

intersects

public boolean intersects(double x,
                          double y,
                          double w,
                          double h)
Tests if this line intersects the interior of the specified rectangle.
Specified by:
intersects in interface Shape
Parameters:
x - the x coordinate of the rectangle
y - the y coordinate of the rectangle
w - the width of the rectangle
h - the height of the rectangle
Returns:
true if the line intersects the rectangle

intersects

public boolean intersects(Rectangle2D r)
Tests if this line intersects the interior of the specified rectangle.
Specified by:
intersects in interface Shape
Parameters:
r - the rectangle
Returns:
true if the line intersects the rectangle
Throws:
NullPointerException - if r is null

intersectsLine

public boolean intersectsLine(double x1,
                              double y1,
                              double x2,
                              double y2)
Test if this line intersects the line given by (x1,y1)->(x2,y2).
Parameters:
x1 - the first x coordinate of the other segment
y1 - the first y coordinate of the other segment
x2 - the second x coordinate of the other segment
y2 - the second y coordinate of the other segment
Returns:
true if the segments intersect

intersectsLine

public boolean intersectsLine(Line2D l)
Test if this line intersects the given line.
Parameters:
l - the other segment
Returns:
true if the segments intersect
Throws:
NullPointerException - if l is null

linesIntersect

public static boolean linesIntersect(double x1,
                                     double y1,
                                     double x2,
                                     double y2,
                                     double x3,
                                     double y3,
                                     double x4,
                                     double y4)
Test if the line segment (x1,y1)->(x2,y2) intersects the line segment (x3,y3)->(x4,y4).
Parameters:
x1 - the first x coordinate of the first segment
y1 - the first y coordinate of the first segment
x2 - the second x coordinate of the first segment
y2 - the second y coordinate of the first segment
x3 - the first x coordinate of the second segment
y3 - the first y coordinate of the second segment
x4 - the second x coordinate of the second segment
y4 - the second y coordinate of the second segment
Returns:
true if the segments intersect

ptLineDist

public double ptLineDist(double px,
                         double py)
Measures the shortest distance from the reference point to a point on the infinite line extended from this segment. If the point is on the segment, the result will be 0. If the segment is length 0, the distance is to the common endpoint.
Parameters:
px - the x coordinate of the point
py - the y coordinate of the point
Returns:
the distance from the point to the extended line

ptLineDist

public static double ptLineDist(double x1,
                                double y1,
                                double x2,
                                double y2,
                                double px,
                                double py)
Measures the shortest distance from the reference point to a point on the infinite line extended from the segment. If the point is on the segment, the result will be 0. If the segment is length 0, the distance is to the common endpoint.
Parameters:
x1 - the first x coordinate of the segment
y1 - the first y coordinate of the segment
x2 - the second x coordinate of the segment
y2 - the second y coordinate of the segment
px - the x coordinate of the point
py - the y coordinate of the point
Returns:
the distance from the point to the extended line

ptLineDist

public double ptLineDist(Point2D p)
Measures the shortest distance from the reference point to a point on the infinite line extended from this segment. If the point is on the segment, the result will be 0. If the segment is length 0, the distance is to the common endpoint.
Parameters:
p - the point
Returns:
the distance from the point to the extended line
Throws:
NullPointerException - if p is null

ptLineDistSq

public double ptLineDistSq(double px,
                           double py)
Measures the square of the shortest distance from the reference point to a point on the infinite line extended from this segment. If the point is on the segment, the result will be 0. If the segment is length 0, the distance is to the common endpoint.
Parameters:
px - the x coordinate of the point
py - the y coordinate of the point
Returns:
the square of the distance from the point to the extended line

ptLineDistSq

public static double ptLineDistSq(double x1,
                                  double y1,
                                  double x2,
                                  double y2,
                                  double px,
                                  double py)
Measures the square of the shortest distance from the reference point to a point on the infinite line extended from the segment. If the point is on the segment, the result will be 0. If the segment is length 0, the distance is to the common endpoint.
Parameters:
x1 - the first x coordinate of the segment
y1 - the first y coordinate of the segment
x2 - the second x coordinate of the segment
y2 - the second y coordinate of the segment
px - the x coordinate of the point
py - the y coordinate of the point
Returns:
the square of the distance from the point to the extended line

ptLineDistSq

public double ptLineDistSq(Point2D p)
Measures the square of the shortest distance from the reference point to a point on the infinite line extended from this segment. If the point is on the segment, the result will be 0. If the segment is length 0, the distance is to the common endpoint.
Parameters:
p - the point
Returns:
the square of the distance from the point to the extended line
Throws:
NullPointerException - if p is null

ptSegDist

public double ptSegDist(double px,
                        double py)
Measures the shortest distance from the reference point to a point on this line segment. If the point is on the segment, the result will be 0.
Parameters:
px - the x coordinate of the point
py - the y coordinate of the point
Returns:
the distance from the point to the segment

ptSegDist

public static double ptSegDist(double x1,
                               double y1,
                               double x2,
                               double y2,
                               double px,
                               double py)
Measures the shortest distance from the reference point to a point on the line segment. If the point is on the segment, the result will be 0.
Parameters:
x1 - the first x coordinate of the segment
y1 - the first y coordinate of the segment
x2 - the second x coordinate of the segment
y2 - the second y coordinate of the segment
px - the x coordinate of the point
py - the y coordinate of the point
Returns:
the distance from the point to the segment

ptSegDist

public double ptSegDist(Point2D p)
Measures the shortest distance from the reference point to a point on this line segment. If the point is on the segment, the result will be 0.
Parameters:
p - the point
Returns:
the distance from the point to the segment
Throws:
NullPointerException - if p is null

ptSegDistSq

public double ptSegDistSq(double px,
                          double py)
Measures the square of the shortest distance from the reference point to a point on this line segment. If the point is on the segment, the result will be 0.
Parameters:
px - the x coordinate of the point
py - the y coordinate of the point
Returns:
the square of the distance from the point to the segment

ptSegDistSq

public static double ptSegDistSq(double x1,
                                 double y1,
                                 double x2,
                                 double y2,
                                 double px,
                                 double py)
Measures the square of the shortest distance from the reference point to a point on the line segment. If the point is on the segment, the result will be 0.
Parameters:
x1 - the first x coordinate of the segment
y1 - the first y coordinate of the segment
x2 - the second x coordinate of the segment
y2 - the second y coordinate of the segment
px - the x coordinate of the point
py - the y coordinate of the point
Returns:
the square of the distance from the point to the segment

ptSegDistSq

public double ptSegDistSq(Point2D p)
Measures the square of the shortest distance from the reference point to a point on this line segment. If the point is on the segment, the result will be 0.
Parameters:
p - the point
Returns:
the square of the distance from the point to the segment
Throws:
NullPointerException - if p is null

relativeCCW

public int relativeCCW(double px,
                       double py)
Computes the relative rotation direction needed to pivot this line about the first point in order to have the second point colinear with point p. Because of floating point rounding, don't expect this to be a perfect measure of colinearity. The answer is 1 if the line has a shorter rotation in the direction of the positive X axis to the negative Y axis (counter-clockwise in the default Java coordinate system), or -1 if the shortest rotation is in the opposite direction (clockwise). If p is already colinear, the return value is -1 if it lies beyond the first point, 0 if it lies in the segment, or 1 if it lies beyond the second point. If the first and second point are coincident, this returns 0.
Parameters:
px - the reference x coordinate
py - the reference y coordinate
Returns:
the relative rotation direction

relativeCCW

public static int relativeCCW(double x1,
                              double y1,
                              double x2,
                              double y2,
                              double px,
                              double py)
Computes the relative rotation direction needed to pivot the line about the first point in order to have the second point colinear with point p. Because of floating point rounding, don't expect this to be a perfect measure of colinearity. The answer is 1 if the line has a shorter rotation in the direction of the positive X axis to the negative Y axis (counter-clockwise in the default Java coordinate system), or -1 if the shortest rotation is in the opposite direction (clockwise). If p is already colinear, the return value is -1 if it lies beyond the first point, 0 if it lies in the segment, or 1 if it lies beyond the second point. If the first and second point are coincident, this returns 0.
Parameters:
x1 - the first x coordinate
y1 - the first y coordinate
x2 - the second x coordinate
y2 - the second y coordinate
px - the reference x coordinate
py - the reference y coordinate
Returns:
the relative rotation direction

relativeCCW

public int relativeCCW(Point2D p)
Computes the relative rotation direction needed to pivot this line about the first point in order to have the second point colinear with point p. Because of floating point rounding, don't expect this to be a perfect measure of colinearity. The answer is 1 if the line has a shorter rotation in the direction of the positive X axis to the negative Y axis (counter-clockwise in the default Java coordinate system), or -1 if the shortest rotation is in the opposite direction (clockwise). If p is already colinear, the return value is -1 if it lies beyond the first point, 0 if it lies in the segment, or 1 if it lies beyond the second point. If the first and second point are coincident, this returns 0.
Parameters:
p - the reference point
Returns:
the relative rotation direction
Throws:
NullPointerException - if p is null

setLine

public abstract void setLine(double x1,
                             double y1,
                             double x2,
                             double y2)
Set the coordinates of the line to the given coordinates. Loss of precision may occur due to rounding issues.
Parameters:
x1 - the first x coordinate
y1 - the first y coordinate
x2 - the second x coordinate
y2 - the second y coordinate

setLine

public void setLine(Line2D l)
Set the coordinates to those of the given line.
Parameters:
l - the line to copy
Throws:
NullPointerException - if l is null

setLine

public void setLine(Point2D p1,
                    Point2D p2)
Set the coordinates to the given points.
Parameters:
p1 - the first point
p2 - the second point
Throws:
NullPointerException - if either point is null

Line2D.java -- represents a line in 2-D space, plus operations on a line Copyright (C) 2000, 2001, 2002 Free Software Foundation This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.