public class Line2D extends Shape
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.| Modifier and Type | Field and Description |
|---|---|
float |
x1
The X coordinate of the start point of the line segment.
|
float |
x2
The X coordinate of the end point of the line segment.
|
float |
y1
The Y coordinate of the start point of the line segment.
|
float |
y2
The Y coordinate of the end point of the line segment.
|
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP, RECT_INTERSECTS| Constructor and Description |
|---|
Line2D()
Constructs and initializes a Line with coordinates (0, 0) -> (0, 0).
|
Line2D(float x1,
float y1,
float x2,
float y2)
Constructs and initializes a Line from the specified coordinates.
|
Line2D(Point2D p1,
Point2D p2)
Constructs and initializes a
Line2D from the
specified Point2D objects. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(float x,
float y)
Tests if the specified coordinates are inside the boundary of the
Shape. |
boolean |
contains(float x,
float y,
float w,
float h)
Tests if the interior of the
Shape entirely contains
the specified rectangular area. |
boolean |
contains(Point2D p)
Tests if a specified
Point2D is inside the boundary
of the Shape. |
Line2D |
copy()
Returns a new copy of this
Shape instance. |
boolean |
equals(java.lang.Object obj) |
RectBounds |
getBounds()
Note that there is no guarantee that the returned
RectBounds is the smallest bounding box that encloses
the Shape, only that the Shape lies
entirely within the indicated RectBounds. |
PathIterator |
getPathIterator(BaseTransform tx)
Returns an iteration object that defines the boundary of this
Line2D. |
PathIterator |
getPathIterator(BaseTransform tx,
float flatness)
Returns an iteration object that defines the boundary of this
flattened
Line2D. |
int |
hashCode() |
boolean |
intersects(float x,
float y,
float w,
float h)
Tests if the interior of the
Shape intersects the
interior of a specified rectangular area. |
boolean |
intersectsLine(float x1,
float y1,
float x2,
float y2)
Tests if the line segment from
(x1,y1) to
(x2,y2) intersects this line segment. |
boolean |
intersectsLine(Line2D l)
Tests if the specified line segment intersects this line segment.
|
static boolean |
linesIntersect(float x1,
float y1,
float x2,
float y2,
float x3,
float y3,
float x4,
float y4)
Tests if the line segment from
(x1,y1) to
(x2,y2) intersects the line segment from (x3,y3)
to (x4,y4). |
float |
ptLineDist(float px,
float py)
Returns the distance from a point to this line.
|
static float |
ptLineDist(float x1,
float y1,
float x2,
float y2,
float px,
float py)
Returns the distance from a point to a line.
|
float |
ptLineDist(Point2D pt)
Returns the distance from a
Point2D to this line. |
float |
ptLineDistSq(float px,
float py)
Returns the square of the distance from a point to this line.
|
static float |
ptLineDistSq(float x1,
float y1,
float x2,
float y2,
float px,
float py)
Returns the square of the distance from a point to a line.
|
float |
ptLineDistSq(Point2D pt)
Returns the square of the distance from a specified
Point2D to this line. |
double |
ptSegDist(float px,
float py)
Returns the distance from a point to this line segment.
|
static float |
ptSegDist(float x1,
float y1,
float x2,
float y2,
float px,
float py)
Returns the distance from a point to a line segment.
|
float |
ptSegDist(Point2D pt)
Returns the distance from a
Point2D to this line
segment. |
float |
ptSegDistSq(float px,
float py)
Returns the square of the distance from a point to this line segment.
|
static float |
ptSegDistSq(float x1,
float y1,
float x2,
float y2,
float px,
float py)
Returns the square of the distance from a point to a line segment.
|
float |
ptSegDistSq(Point2D pt)
Returns the square of the distance from a
Point2D to
this line segment. |
int |
relativeCCW(float px,
float py)
Returns an indicator of where the specified point
(px,py) lies with respect to this line segment. |
static int |
relativeCCW(float x1,
float y1,
float x2,
float y2,
float px,
float py)
Returns an indicator of where the specified point
(px,py) lies with respect to the line segment from
(x1,y1) to (x2,y2). |
int |
relativeCCW(Point2D p)
Returns an indicator of where the specified
Point2D
lies with respect to this line segment. |
void |
setLine(float x1,
float y1,
float x2,
float y2)
Sets the location of the end points of this
Line2D
to the specified float coordinates. |
void |
setLine(Line2D l)
Sets the location of the end points of this
Line2D to
the same as those end points of the specified Line2D. |
void |
setLine(Point2D p1,
Point2D p2)
Sets the location of the end points of this
Line2D to
the specified Point2D coordinates. |
accumulate, accumulateCubic, accumulateCubic, accumulateQuad, contains, intersects, intersectsLine, outcode, pointCrossingsForCubic, pointCrossingsForLine, pointCrossingsForPath, pointCrossingsForQuad, rectCrossingsForCubic, rectCrossingsForLine, rectCrossingsForPath, rectCrossingsForQuadpublic float x1
public float y1
public float x2
public float y2
public Line2D()
public Line2D(float x1,
float y1,
float x2,
float y2)
x1 - the X coordinate of the start pointy1 - the Y coordinate of the start pointx2 - the X coordinate of the end pointy2 - the Y coordinate of the end pointpublic void setLine(float x1,
float y1,
float x2,
float y2)
Line2D
to the specified float coordinates.x1 - the X coordinate of the start pointy1 - the Y coordinate of the start pointx2 - the X coordinate of the end pointy2 - the Y coordinate of the end pointpublic void setLine(Point2D p1, Point2D p2)
Line2D to
the specified Point2D coordinates.p1 - the start Point2D of the line segmentp2 - the end Point2D of the line segmentpublic void setLine(Line2D l)
Line2D to
the same as those end points of the specified Line2D.l - the specified Line2Dpublic RectBounds getBounds()
RectBounds is the smallest bounding box that encloses
the Shape, only that the Shape lies
entirely within the indicated RectBounds.public boolean contains(float x,
float y)
ShapeShape.public boolean contains(float x,
float y,
float w,
float h)
ShapeShape entirely contains
the specified rectangular area. All coordinates that lie inside
the rectangular area must lie within the Shape for the
entire rectanglar area to be considered contained within the
Shape.
The Shape.contains() method allows a Shape
implementation to conservatively return false when:
intersect method returns true and
Shape entirely contains the rectangular area are
prohibitively expensive.
Shapes this method might
return false even though the Shape contains
the rectangular area.
The Area class performs
more accurate geometric computations than most
Shape objects and therefore can be used if a more precise
answer is required.contains in class Shapex - the X coordinate of the upper-left corner
of the specified rectangular areay - the Y coordinate of the upper-left corner
of the specified rectangular areaw - the width of the specified rectangular areah - the height of the specified rectangular areatrue if the interior of the Shape
entirely contains the specified rectangular area;
false otherwise or, if the Shape
contains the rectangular area and the
intersects method returns true
and the containment calculations would be too expensive to
perform.Shape.intersects(float, float, float, float)public boolean contains(Point2D p)
ShapePoint2D is inside the boundary
of the Shape.public boolean intersects(float x,
float y,
float w,
float h)
ShapeShape intersects the
interior of a specified rectangular area.
The rectangular area is considered to intersect the Shape
if any point is contained in both the interior of the
Shape and the specified rectangular area.
The Shape.intersects() method allows a Shape
implementation to conservatively return true when:
Shape intersect, but
Shapes this method might
return true even though the rectangular area does not
intersect the Shape.
The Area class performs
more accurate computations of geometric intersection than most
Shape objects and therefore can be used if a more precise
answer is required.intersects in class Shapex - the X coordinate of the upper-left corner
of the specified rectangular areay - the Y coordinate of the upper-left corner
of the specified rectangular areaw - the width of the specified rectangular areah - the height of the specified rectangular areatrue if the interior of the Shape and
the interior of the rectangular area intersect, or are
both highly likely to intersect and intersection calculations
would be too expensive to perform; false otherwise.public static int relativeCCW(float x1,
float y1,
float x2,
float y2,
float px,
float py)
(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 end point, (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 end points, then the value will be -1 if the point
lies "beyond (x1,y1)" or 1 if the point lies
"beyond (x2,y2)".
x1 - the X coordinate of the start point of the
specified line segmenty1 - the Y coordinate of the start point of the
specified line segmentx2 - the X coordinate of the end point of the
specified line segmenty2 - the Y coordinate of the end point of the
specified line segmentpx - the X coordinate of the specified point to be
compared with the specified line segmentpy - the Y coordinate of the specified point to be
compared with the specified line segmentpublic int relativeCCW(float px,
float py)
(px,py) lies with respect to this line segment.
See the method comments of
#relativeCCW(double, double, double, double, double, double)
to interpret the return value.px - the X coordinate of the specified point
to be compared with this Line2Dpy - the Y coordinate of the specified point
to be compared with this Line2DLine2D#relativeCCW(double, double, double, double, double, double)public int relativeCCW(Point2D p)
Point2D
lies with respect to this line segment.
See the method comments of
#relativeCCW(double, double, double, double, double, double)
to interpret the return value.p - the specified Point2D to be compared
with this Line2DPoint2D with respect to this Line2D#relativeCCW(double, double, double, double, double, double)public static boolean linesIntersect(float x1,
float y1,
float x2,
float y2,
float x3,
float y3,
float x4,
float y4)
(x1,y1) to
(x2,y2) intersects the line segment from (x3,y3)
to (x4,y4).x1 - the X coordinate of the start point of the first
specified line segmenty1 - the Y coordinate of the start point of the first
specified line segmentx2 - the X coordinate of the end point of the first
specified line segmenty2 - the Y coordinate of the end point of the first
specified line segmentx3 - the X coordinate of the start point of the second
specified line segmenty3 - the Y coordinate of the start point of the second
specified line segmentx4 - the X coordinate of the end point of the second
specified line segmenty4 - the Y coordinate of the end point of the second
specified line segmenttrue if the first specified line segment
and the second specified line segment intersect
each other; false otherwise.public boolean intersectsLine(float x1,
float y1,
float x2,
float y2)
(x1,y1) to
(x2,y2) intersects this line segment.x1 - the X coordinate of the start point of the
specified line segmenty1 - the Y coordinate of the start point of the
specified line segmentx2 - the X coordinate of the end point of the
specified line segmenty2 - the Y coordinate of the end point of the
specified line segmentfalse otherwise.public boolean intersectsLine(Line2D l)
l - the specified Line2Dtrue if this line segment and the specified line
segment intersect each other;
false otherwise.public static float ptSegDistSq(float x1,
float y1,
float x2,
float y2,
float px,
float py)
x1 - the X coordinate of the start point of the
specified line segmenty1 - the Y coordinate of the start point of the
specified line segmentx2 - the X coordinate of the end point of the
specified line segmenty2 - the Y coordinate of the end point of the
specified line segmentpx - the X coordinate of the specified point being
measured against the specified line segmentpy - the Y coordinate of the specified point being
measured against the specified line segment#ptLineDistSq(double, double, double, double, double, double)public static float ptSegDist(float x1,
float y1,
float x2,
float y2,
float px,
float py)
x1 - the X coordinate of the start point of the
specified line segmenty1 - the Y coordinate of the start point of the
specified line segmentx2 - the X coordinate of the end point of the
specified line segmenty2 - the Y coordinate of the end point of the
specified line segmentpx - the X coordinate of the specified point being
measured against the specified line segmentpy - the Y coordinate of the specified point being
measured against the specified line segment#ptLineDist(double, double, double, double, double, double)public float ptSegDistSq(float px,
float py)
px - the X coordinate of the specified point being
measured against this line segmentpy - the Y coordinate of the specified point being
measured against this line segment#ptLineDistSq(double, double)public float ptSegDistSq(Point2D pt)
Point2D to
this line segment.
The distance measured is the distance between the specified
point and the closest point between the current line's end points.
If the specified point intersects the line segment in between the
end points, this method returns 0.0.pt - the specified Point2D being measured against
this line segment.Point2D to the current
line segment.ptLineDistSq(Point2D)public double ptSegDist(float px,
float py)
px - the X coordinate of the specified point being
measured against this line segmentpy - the Y coordinate of the specified point being
measured against this line segment#ptLineDist(double, double)public float ptSegDist(Point2D pt)
Point2D to this line
segment.
The distance measured is the distance between the specified
point and the closest point between the current line's end points.
If the specified point intersects the line segment in between the
end points, this method returns 0.0.pt - the specified Point2D being measured
against this line segmentPoint2D to the current line
segment.ptLineDist(Point2D)public static float ptLineDistSq(float x1,
float y1,
float x2,
float y2,
float px,
float py)
x1 - the X coordinate of the start point of the specified liney1 - the Y coordinate of the start point of the specified linex2 - the X coordinate of the end point of the specified liney2 - the Y coordinate of the end point of the specified linepx - the X coordinate of the specified point being
measured against the specified linepy - the Y coordinate of the specified point being
measured against the specified line#ptSegDistSq(double, double, double, double, double, double)public static float ptLineDist(float x1,
float y1,
float x2,
float y2,
float px,
float py)
x1 - the X coordinate of the start point of the specified liney1 - the Y coordinate of the start point of the specified linex2 - the X coordinate of the end point of the specified liney2 - the Y coordinate of the end point of the specified linepx - the X coordinate of the specified point being
measured against the specified linepy - the Y coordinate of the specified point being
measured against the specified line#ptSegDist(double, double, double, double, double, double)public float ptLineDistSq(float px,
float py)
Line2D. If the specified point
intersects the line, this method returns 0.0.px - the X coordinate of the specified point being
measured against this linepy - the Y coordinate of the specified point being
measured against this line#ptSegDistSq(double, double)public float ptLineDistSq(Point2D pt)
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.pt - the specified Point2D being measured
against this linePoint2D to the current
line.ptSegDistSq(Point2D)public float ptLineDist(float px,
float py)
Line2D. If the specified point
intersects the line, this method returns 0.0.px - the X coordinate of the specified point being
measured against this linepy - the Y coordinate of the specified point being
measured against this line#ptSegDist(double, double)public float ptLineDist(Point2D pt)
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.pt - the specified Point2D being measuredPoint2D to the current line.ptSegDist(Point2D)public PathIterator getPathIterator(BaseTransform tx)
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.getPathIterator in class Shapetx - the specified BaseTransformPathIterator that defines the boundary of this
Line2D.public PathIterator getPathIterator(BaseTransform tx, float flatness)
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.getPathIterator in class Shapetx - the specified BaseTransformflatness - the maximum amount that the control points for a
given curve can vary from colinear before a subdivided
curve is replaced by a straight line connecting the
end points. Since a Line2D object is
always flat, this parameter is ignored.PathIterator that defines the boundary of the
flattened Line2Dpublic Line2D copy()
ShapeShape instance.public int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Object