Line2Dpublic abstract class Line2D extends Object implements Shape, CloneableThis Line2D represents a line segment in (x, y)
coordinate space. This class, like all of the Java 2D API, uses a
default coordinate system called user space in which the y-axis
values increase downward and x-axis values increase to the right. For
more information on the user space coordinate system, see the
Coordinate Systems section of the Java 2D Programmer's Guide.
This class is only the abstract superclass for all objects that
store a 2D line segment.
The actual storage representation of the coordinates is left to
the subclass. |
Constructors Summary |
---|
protected Line2D()This is an abstract class that cannot be instantiated directly.
Type-specific implementation subclasses are available for
instantiation and provide a number of formats for storing
the information necessary to satisfy the various accessory
methods below.
|
Methods Summary |
---|
public java.lang.Object | clone()Creates a new object of the same class as this object.
try {
return super.clone();
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
| public boolean | contains(double x, double y)Tests if a specified coordinate is inside the boundary of this
Line2D . This method is required to implement the
{@link Shape} interface, but in the case of Line2D
objects it always returns false since a line contains
no area.
return false;
| public boolean | contains(java.awt.geom.Point2D p)Tests if a given Point2D is inside the boundary of
this Line2D .
This method is required to implement the Shape interface,
but in the case of Line2D objects it always returns
false since a line contains no area.
return false;
| public boolean | contains(double x, double y, double w, double h)Tests if the interior of this Line2D entirely contains
the specified set of rectangular coordinates.
This method is required to implement the Shape interface,
but in the case of Line2D objects it always returns
false since a line contains no area.
return false;
| public boolean | contains(java.awt.geom.Rectangle2D r)Tests if the interior of this Line2D entirely contains
the specified Rectangle2D .
This method is required to implement the Shape interface,
but in the case of Line2D objects it always returns
false since a line contains no area.
return false;
| public java.awt.Rectangle | getBounds()Returns the bounding box of this Line2D .
return getBounds2D().getBounds();
| public abstract java.awt.geom.Point2D | getP1()Returns the starting Point2D of this
Line2D .
| public abstract java.awt.geom.Point2D | getP2()Returns the end Point2D of this Line2D .
| public java.awt.geom.PathIterator | getPathIterator(java.awt.geom.AffineTransform at)Returns an iteration object that defines the boundary of this
Line2D .
The iterator for this class is not multi-threaded safe,
which means that this Line2D class does not
guarantee that modifications to the geometry of this
Line2D object do not affect any iterations of that
geometry that are already in process.
return new LineIterator(this, at);
| public java.awt.geom.PathIterator | getPathIterator(java.awt.geom.AffineTransform at, double flatness)Returns an iteration object that defines the boundary of this
flattened Line2D .
The iterator for this class is not multi-threaded safe,
which means that this Line2D class does not
guarantee that modifications to the geometry of this
Line2D object do not affect any iterations of that
geometry that are already in process.
return new LineIterator(this, at);
| public abstract double | getX1()Returns the X coordinate of the start point in double precision.
| public abstract double | getX2()Returns the X coordinate of the end point in double precision.
| public abstract double | getY1()Returns the Y coordinate of the start point in double precision.
| public abstract double | getY2()Returns the Y coordinate of the end point in double precision.
| public boolean | intersects(double x, double y, double w, double h)Tests if this Line2D intersects the interior of a
specified set of rectangular coordinates.
return intersects(new Rectangle2D.Double(x, y, w, h));
| public boolean | intersects(java.awt.geom.Rectangle2D r)Tests if this Line2D intersects the interior of a
specified Rectangle2D .
return r.intersectsLine(getX1(), getY1(), getX2(), getY2());
| public boolean | intersectsLine(double X1, double Y1, double X2, double Y2)Tests if the line segment from (X1, Y1) to
(X2, Y2) intersects this line segment.
return linesIntersect(X1, Y1, X2, Y2,
getX1(), getY1(), getX2(), getY2());
| public boolean | intersectsLine(java.awt.geom.Line2D l)Tests if the specified line segment intersects this line segment.
return linesIntersect(l.getX1(), l.getY1(), l.getX2(), l.getY2(),
getX1(), getY1(), getX2(), getY2());
| public static boolean | linesIntersect(double X1, double Y1, double X2, double Y2, double X3, double Y3, double X4, double Y4)Tests if the line segment from (X1, Y1) to
(X2, Y2) intersects the line segment from (X3, Y3)
to (X4, Y4).
return ((relativeCCW(X1, Y1, X2, Y2, X3, Y3) *
relativeCCW(X1, Y1, X2, Y2, X4, Y4) <= 0)
&& (relativeCCW(X3, Y3, X4, Y4, X1, Y1) *
relativeCCW(X3, Y3, X4, Y4, X2, Y2) <= 0));
| public static double | ptLineDist(double X1, double Y1, double X2, double Y2, double PX, double PY)Returns the distance from a point to a line.
The distance measured is the distance between the specified
point and the closest point on the infinitely-extended line
defined by the specified coordinates. If the specified point
intersects the line, this method returns 0.0.
return Math.sqrt(ptLineDistSq(X1, Y1, X2, Y2, PX, PY));
| public double | ptLineDist(double PX, double PY)Returns the distance from a point to this line.
The distance measured is the distance between the specified
point and the closest point on the infinitely-extended line
defined by this Line2D . If the specified point
intersects the line, this method returns 0.0.
return ptLineDist(getX1(), getY1(), getX2(), getY2(), PX, PY);
| public double | ptLineDist(java.awt.geom.Point2D pt)Returns the distance from a Point2D to this line.
The distance measured is the distance between the specified
point and the closest point on the infinitely-extended line
defined by this Line2D . If the specified point
intersects the line, this method returns 0.0.
return ptLineDist(getX1(), getY1(), getX2(), getY2(),
pt.getX(), pt.getY());
| public static double | ptLineDistSq(double X1, double Y1, double X2, double Y2, double PX, double PY)Returns the square of the distance from a point to a line.
The distance measured is the distance between the specified
point and the closest point on the infinitely-extended line
defined by the specified coordinates. If the specified point
intersects the line, this method returns 0.0.
// Adjust vectors relative to X1,Y1
// X2,Y2 becomes relative vector from X1,Y1 to end of segment
X2 -= X1;
Y2 -= Y1;
// PX,PY becomes relative vector from X1,Y1 to test point
PX -= X1;
PY -= Y1;
double dotprod = PX * X2 + PY * Y2;
// dotprod is the length of the PX,PY vector
// projected on the X1,Y1=>X2,Y2 vector times the
// length of the X1,Y1=>X2,Y2 vector
double projlenSq = dotprod * dotprod / (X2 * X2 + Y2 * Y2);
// Distance to line is now the length of the relative point
// vector minus the length of its projection onto the line
double lenSq = PX * PX + PY * PY - projlenSq;
if (lenSq < 0) {
lenSq = 0;
}
return lenSq;
| public double | ptLineDistSq(double PX, double PY)Returns the square of the distance from a point to this line.
The distance measured is the distance between the specified
point and the closest point on the infinitely-extended line
defined by this Line2D . If the specified point
intersects the line, this method returns 0.0.
return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), PX, PY);
| public double | ptLineDistSq(java.awt.geom.Point2D pt)Returns the square of the distance from a specified
Point2D to this line.
The distance measured is the distance between the specified
point and the closest point on the infinitely-extended line
defined by this Line2D . If the specified point
intersects the line, this method returns 0.0.
return ptLineDistSq(getX1(), getY1(), getX2(), getY2(),
pt.getX(), pt.getY());
| public static double | ptSegDist(double X1, double Y1, double X2, double Y2, double PX, double PY)Returns the distance from a point to a line segment.
The distance measured is the distance between the specified
point and the closest point between the specified endpoints.
If the specified point intersects the line segment in between the
endpoints, this method returns 0.0.
return Math.sqrt(ptSegDistSq(X1, Y1, X2, Y2, PX, PY));
| public double | ptSegDist(double PX, double PY)Returns the distance from a point to this line segment.
The distance measured is the distance between the specified
point and the closest point between the current line's endpoints.
If the specified point intersects the line segment in between the
endpoints, this method returns 0.0.
return ptSegDist(getX1(), getY1(), getX2(), getY2(), PX, PY);
| public double | ptSegDist(java.awt.geom.Point2D pt)Returns the distance from a Point2D to this line
segment.
The distance measured is the distance between the specified
point and the closest point between the current line's endpoints.
If the specified point intersects the line segment in between the
endpoints, this method returns 0.0.
return ptSegDist(getX1(), getY1(), getX2(), getY2(),
pt.getX(), pt.getY());
| public static double | ptSegDistSq(double X1, double Y1, double X2, double Y2, double PX, double PY)Returns the square of the distance from a point to a line segment.
The distance measured is the distance between the specified
point and the closest point between the specified endpoints.
If the specified point intersects the line segment in between the
endpoints, this method returns 0.0.
// Adjust vectors relative to X1,Y1
// X2,Y2 becomes relative vector from X1,Y1 to end of segment
X2 -= X1;
Y2 -= Y1;
// PX,PY becomes relative vector from X1,Y1 to test point
PX -= X1;
PY -= Y1;
double dotprod = PX * X2 + PY * Y2;
double projlenSq;
if (dotprod <= 0.0) {
// PX,PY is on the side of X1,Y1 away from X2,Y2
// distance to segment is length of PX,PY vector
// "length of its (clipped) projection" is now 0.0
projlenSq = 0.0;
} else {
// switch to backwards vectors relative to X2,Y2
// X2,Y2 are already the negative of X1,Y1=>X2,Y2
// to get PX,PY to be the negative of PX,PY=>X2,Y2
// the dot product of two negated vectors is the same
// as the dot product of the two normal vectors
PX = X2 - PX;
PY = Y2 - PY;
dotprod = PX * X2 + PY * Y2;
if (dotprod <= 0.0) {
// PX,PY is on the side of X2,Y2 away from X1,Y1
// distance to segment is length of (backwards) PX,PY vector
// "length of its (clipped) projection" is now 0.0
projlenSq = 0.0;
} else {
// PX,PY is between X1,Y1 and X2,Y2
// dotprod is the length of the PX,PY vector
// projected on the X2,Y2=>X1,Y1 vector times the
// length of the X2,Y2=>X1,Y1 vector
projlenSq = dotprod * dotprod / (X2 * X2 + Y2 * Y2);
}
}
// Distance to line is now the length of the relative point
// vector minus the length of its projection onto the line
// (which is zero if the projection falls outside the range
// of the line segment).
double lenSq = PX * PX + PY * PY - projlenSq;
if (lenSq < 0) {
lenSq = 0;
}
return lenSq;
| public double | ptSegDistSq(double PX, double PY)Returns the square of the distance from a point to this line segment.
The distance measured is the distance between the specified
point and the closest point between the current line's endpoints.
If the specified point intersects the line segment in between the
endpoints, this method returns 0.0.
return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), PX, PY);
| public double | ptSegDistSq(java.awt.geom.Point2D pt)Returns the square of the distance from a Point2D to
this line segment.
The distance measured is the distance between the specified
point and the closest point between the current line's endpoints.
If the specified point intersects the line segment in between the
endpoints, this method returns 0.0.
return ptSegDistSq(getX1(), getY1(), getX2(), getY2(),
pt.getX(), pt.getY());
| public static int | relativeCCW(double X1, double Y1, double X2, double Y2, double PX, double PY)Returns an indicator of where the specified point
(PX, PY) lies with respect to the line segment from
(X1, Y1) to (X2, Y2).
The return value can be either 1, -1, or 0 and indicates
in which direction the specified line must pivot around its
first endpoint, (X1, Y1), in order to point at the
specified point (PX, PY).
A return value of 1 indicates that the line segment must
turn in the direction that takes the positive X axis towards
the negative Y axis. In the default coordinate system used by
Java 2D, this direction is counterclockwise.
A return value of -1 indicates that the line segment must
turn in the direction that takes the positive X axis towards
the positive Y axis. In the default coordinate system, this
direction is clockwise.
A return value of 0 indicates that the point lies
exactly on the line segment. Note that an indicator value
of 0 is rare and not useful for determining colinearity
because of floating point rounding issues.
If the point is colinear with the line segment, but
not between the endpoints, then the value will be -1 if the point
lies "beyond (X1, Y1)" or 1 if the point lies
"beyond (X2, Y2)".
X2 -= X1;
Y2 -= Y1;
PX -= X1;
PY -= Y1;
double ccw = PX * Y2 - PY * X2;
if (ccw == 0.0) {
// The point is colinear, classify based on which side of
// the segment the point falls on. We can calculate a
// relative value using the projection of PX,PY onto the
// segment - a negative value indicates the point projects
// outside of the segment in the direction of the particular
// endpoint used as the origin for the projection.
ccw = PX * X2 + PY * Y2;
if (ccw > 0.0) {
// Reverse the projection to be relative to the original X2,Y2
// X2 and Y2 are simply negated.
// PX and PY need to have (X2 - X1) or (Y2 - Y1) subtracted
// from them (based on the original values)
// Since we really want to get a positive answer when the
// point is "beyond (X2,Y2)", then we want to calculate
// the inverse anyway - thus we leave X2 & Y2 negated.
PX -= X2;
PY -= Y2;
ccw = PX * X2 + PY * Y2;
if (ccw < 0.0) {
ccw = 0.0;
}
}
}
return (ccw < 0.0) ? -1 : ((ccw > 0.0) ? 1 : 0);
| public int | relativeCCW(double PX, double PY)Returns an indicator of where the specified point
(PX, PY) lies with respect to this line segment.
See the method comments of
{@link #relativeCCW(double, double, double, double, double, double)}
to interpret the return value.
return relativeCCW(getX1(), getY1(), getX2(), getY2(), PX, PY);
| public int | relativeCCW(java.awt.geom.Point2D p)Returns an indicator of where the specified Point2D
lies with respect to this line segment.
See the method comments of
{@link #relativeCCW(double, double, double, double, double, double)}
to interpret the return value.
return relativeCCW(getX1(), getY1(), getX2(), getY2(),
p.getX(), p.getY());
| public void | setLine(java.awt.geom.Line2D l)Sets the location of the endpoints of this Line2D to
the same as those endpoints of the specified Line2D .
setLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
| public abstract void | setLine(double X1, double Y1, double X2, double Y2)Sets the location of the endpoints of this Line2D to
the specified double coordinates.
| public void | setLine(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)Sets the location of the endpoints of this Line2D to
the specified Point2D coordinates.
setLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
|
|