public class GeneralPath extends Object implements ICloneable, Cloneable
This class uses double precision coordinates to model a virtual pen that can be moved across the world coordinate system and which can be used to draw lines and Bézier curves. This class provides many geometric utility methods, such as calculating the bounds, hit tests, containment tests, curve flattening, path transformation, path modification, and path iteration.
Modifier and Type | Class and Description |
---|---|
static class |
GeneralPath.PathCursor
A class that is used by
GeneralPath to iterate over the elements the path is made up of. |
Constructor and Description |
---|
GeneralPath()
Creates a new instance with given initial capacity.
|
GeneralPath(int capacity)
Creates a new instance with given initial capacity.
|
GeneralPath(Path fxPath)
Creates a new instance from a JavaFX
Path . |
Modifier and Type | Method and Description |
---|---|
void |
append(GeneralPath other,
boolean connect)
Appends the contents of the given path to this path.
|
void |
append(GeneralPath other,
int beginIndex,
int endIndex,
boolean connect)
Appends parts of the contents of the given path to this path.
|
void |
appendEllipse(IRectangle bounds,
boolean connect)
Appends an ellipse using the given bounding box to this instance.
|
void |
appendOrientedRectangle(IOrientedRectangle rectangle,
boolean connect)
Appends an
IOrientedRectangle to this path instance. |
void |
appendRectangle(IRectangle rectangle,
boolean connect)
Appends a rectangle to this path instance.
|
boolean |
areaContains(IPoint point)
Checks whether the point lies inside the area that is described by this path instance.
|
boolean |
areaContains(PointD point)
Checks whether the point lies inside the area that is described by this path instance.
|
boolean |
areaContains(PointD point,
double eps)
Checks whether the point lies inside the area that is described by this path instance.
|
void |
clear()
Resets this path to be an empty path.
|
GeneralPath |
clone()
Creates a clone of this instance, copying the path information to the new instance.
|
void |
close()
Appends a
PathType.CLOSE operation to the path elements, creating a line to the last PathType.MOVE_TO
position. |
void |
compress()
Compresses the internal data structures so that the amount of memory used by this instance is minimal with respect to
the information stored in it.
|
GeneralPath.PathCursor |
createCursor()
Creates a cursor for iterating over the elements of this path.
|
GeneralPath |
createGeneralPath(Matrix2D transform)
Creates a new
GeneralPath that is a copy of this path with all geometry transformed using the supplied matrix. |
Path |
createPath(Paint brush,
Pen pen,
Matrix2D transform)
Creates a
Path instance using the provided parameters. |
GeneralPath |
createSmoothedPath(double smoothingLength)
Creates a new path based on this one where corners are smoothed with a quadratic Bézier arc starting at
smoothingLength
away from the corner. |
void |
cropAfter(GeneralPath.PathCursor cursor)
Crops the path after the position indicated by the cursor.
|
void |
cropBefore(GeneralPath.PathCursor cursor)
Crops the path before the position indicated by the cursor.
|
void |
cubicTo(double c1x,
double c1y,
double c2x,
double c2y,
double x,
double y)
Appends a
cubic Bézier curve to the path elements. |
void |
cubicTo(IPoint c1,
IPoint c2,
IPoint end)
Appends a
cubic Bézier curve to the path elements. |
void |
cubicTo(PointD c1,
PointD c2,
PointD end)
Appends a
cubic Bézier curve to the path elements. |
protected void |
ensureCapacity(int count)
Ensures that this path can contain
count more coordinates and an additional type. |
boolean |
equals(Object o) |
double |
findLineIntersection(PointD start,
PointD end)
Finds an intersection point between a line and this
GeneralPath . |
double |
findLineIntersection(PointD start,
PointD end,
double eps)
Finds an intersection point between a line and this
GeneralPath . |
double |
findRayIntersection(PointD anchor,
PointD direction)
Finds the first intersection of a ray with this
GeneralPath using a given epsilon for flattening Bézier curves
contained in the path. |
double |
findRayIntersection(PointD anchor,
PointD direction,
double eps)
Finds the first intersection of a ray with this
GeneralPath using a given epsilon for flattening Bézier curves
contained in the path. |
GeneralPath |
flatten(double eps)
Creates a new path from this instance flattening all Bézier curves using the given epsilon.
|
RectD |
getBounds()
Calculates the bounds of this path and returns it.
|
RectD |
getBounds(double eps)
Gets the bounds of a flattened version the path.
|
int |
getCount()
Gets the number of
PathType operations stored in this instance. |
PointD |
getLastCoordinate()
Gets the last coordinates of the last path element.
|
double |
getLastX()
Gets the last x coordinate of the last path element.
|
double |
getLastY()
Gets the last y coordinate of the last path element.
|
double |
getLength()
Returns the geometric length of this path.
|
PointD |
getPoint(double ratio)
Gets a point on this path instance at the given ratio.
|
int |
getSegmentCount()
Gets the number of segments in this path, that is the number of
PathType.LINE_TO and PathType.CLOSE
operations. |
Tangent |
getTangent(double ratio)
Gets the tangent on this path instance at the given ratio.
|
Tangent |
getTangent(int segmentIndex,
double ratio)
Gets the tangent on this path instance at the segment and segment ratio.
|
boolean |
intersects(RectD rectangle,
double eps)
Checks whether this path intersects the given rectangle.
|
boolean |
isEmpty()
Gets a value indicating whether this path contains elements other than an initial
PathType.MOVE_TO .. |
boolean |
isVisible()
Gets a value indicating whether the path contains visible parts.
|
void |
lineTo(double x,
double y)
Appends a
PathType.LINE_TO operation to the path elements. |
void |
lineTo(IPoint point)
Appends a
PathType.LINE_TO operation to the path elements. |
void |
lineTo(PointD point)
Appends a
PathType.LINE_TO operation to the path elements. |
boolean |
mayIntersectClip(RectD clip,
double width)
Performs a quick test to decide whether this path might intersect the clipping rectangle, provided the path would be
rendered using stroke of the given width.
|
void |
moveTo(double x,
double y)
Appends a
PathType.MOVE_TO operation to the path elements. |
void |
moveTo(IPoint point)
Appends a
PathType.MOVE_TO operation to the path elements. |
void |
moveTo(PointD point)
Appends a
PathType.MOVE_TO operation to the path elements. |
boolean |
pathContains(PointD point,
double eps)
Tests whether the line of the path is hit at the given point using an epsilon for fuzzy hit testing.
|
void |
prepend(GeneralPath other,
boolean connect)
Adds the contents of the given path to the beginning of this path.
|
void |
quadTo(double cx,
double cy,
double x,
double y)
Appends a
quadratic Bézier curve to the path elements. |
void |
quadTo(IPoint center,
IPoint point)
Appends a
quadratic Bézier curve to the path elements. |
void |
quadTo(PointD center,
PointD point)
Appends a
quadratic Bézier curve to the path elements. |
void |
transform(Matrix2D transform)
Transforms this
GeneralPath in place using the given transform. |
void |
updatePath(Path path,
Paint brush,
Pen pen,
Matrix2D transform)
Updates a path instance that has been created using
createPath(Paint, Pen, Matrix2D) . |
public GeneralPath()
public GeneralPath(int capacity)
capacity
- The number of elements for which storage will be allocated initially.public final void append(GeneralPath other, boolean connect)
If connect
is true
, an initial PathType.MOVE_TO
in other
will be transformed into a
PathType.LINE_TO
.
other
- The path to append to this path.connect
- Whether to connect the path to the end of this path.public final void append(GeneralPath other, int beginIndex, int endIndex, boolean connect)
If connect
is true
, an initial PathType.MOVE_TO
in other
will be transformed into a
PathType.LINE_TO
.
other
- The path to append to this path.connect
- Whether to connect the path to the end of this path.beginIndex
- The first element to be copied from other
, inclusively.endIndex
- The last element to be copied from other
, exclusively.public final void appendEllipse(IRectangle bounds, boolean connect)
bounds
- The bounds of the ellipse.connect
- Whether to initially lineTo(IPoint)
the ellipses lowest point.public final void appendOrientedRectangle(IOrientedRectangle rectangle, boolean connect)
IOrientedRectangle
to this path instance.rectangle
- The IOrientedRectangle
to get the coordinates from.connect
- Whether to use a lineTo(IPoint)
to the anchor corner.public final void appendRectangle(IRectangle rectangle, boolean connect)
rectangle
- The rectangle to get the coordinates from.connect
- Whether to use a lineTo(IPoint)
to the upper left corner.public final boolean areaContains(IPoint point)
Note that this will only work as expected for closed paths.
point
- The coordinates of the point to test.public final boolean areaContains(PointD point)
Note that this will only work as expected for closed paths.
point
- The coordinates of the point to test.public final boolean areaContains(PointD point, double eps)
Note that this will only work as expected for closed paths. The epsilon is used for internally flattening Bézier curves.
point
- The coordinates of the point to test.eps
- The value to use for flattening Bézier curves during the test.public final void clear()
public GeneralPath clone()
clone
in interface ICloneable
clone
in class Object
Object.clone()
public final void close()
PathType.CLOSE
operation to the path elements, creating a line to the last PathType.MOVE_TO
position.
This draws a line from the current end point to the last moveTo(double, double)
position, closing the current
sub path.
public final void compress()
public final GeneralPath.PathCursor createCursor()
The cursor is fail-fast, i.e. if the path's structure is modified after the construction of the cursor any cursor operation will fail.
public final GeneralPath createGeneralPath(Matrix2D transform)
GeneralPath
that is a copy of this path with all geometry transformed using the supplied matrix.transform
- The matrix to multiply the geometry with.public Path createPath(Paint brush, Pen pen, Matrix2D transform)
Path
instance using the provided parameters.brush
- The brush to use for filling the path.pen
- The pen to use for stroking.transform
- The matrix to apply to the path data.updatePath(Path, Paint, Pen, Matrix2D)
public final GeneralPath createSmoothedPath(double smoothingLength)
smoothingLength
away from the corner.smoothingLength
- Distance from the corner where the arc begins.public final void cropAfter(GeneralPath.PathCursor cursor)
This will remove all path operations that follow the position indicated by the cursor.
cursor
- The cursor that depicts a position in this path.public final void cropBefore(GeneralPath.PathCursor cursor)
This will remove all path operations that precede the position indicated by the cursor.
cursor
- The cursor that depicts a position in this path.public final void cubicTo(double c1x, double c1y, double c2x, double c2y, double x, double y)
cubic Bézier curve
to the path elements.
This draws a cubic Bézier curve from the current end point to the given position using the two given control points.
c1x
- The x coordinate of the first Bézier control point.c1y
- The y coordinate of the first Bézier control point.c2x
- The x coordinate of the second Bézier control point.c2y
- The y coordinate of the second Bézier control point.x
- The x coordinate of the curve's end point.y
- The y coordinate of the curve's end point.public final void cubicTo(IPoint c1, IPoint c2, IPoint end)
cubic Bézier curve
to the path elements.
This draws a cubic Bézier curve from the current end point to the given position using the two given control points.
c1
- The coordinates of the first Bézier control point.c2
- The coordinates of the second Bézier control point.end
- The coordinates of the curve's end point.public final void cubicTo(PointD c1, PointD c2, PointD end)
cubic Bézier curve
to the path elements.
This draws a cubic Bézier curve from the current end point to the given position using the two given control points.
c1
- The coordinates of the first Bézier control point.c2
- The coordinates of the second Bézier control point.end
- The coordinates of the curve's end point.protected final void ensureCapacity(int count)
count
more coordinates and an additional type.
This enlarges the internal data structures appropriately if needed.
count
- The number of coordinates to hold for the next type to be added.public final double findLineIntersection(PointD start, PointD end)
GeneralPath
.
The value returned will be the smallest positive value smaller than 1 such that the point depicted by start + result * (end - start)
is an intersection point between the line and the path.
start
- The coordinates of the first point of the line.end
- The coordinates of the second point of the line.double.PositiveInfinity
.public final double findLineIntersection(PointD start, PointD end, double eps)
GeneralPath
.
The value returned will be the smallest positive value smaller than 1 such that the point depicted by start + result * (end - start)
is an intersection point between the line and the path.
start
- The coordinates of the first point of the line.end
- The coordinates of the second point of the line.eps
- The value to use for interpolating Bézier curves.double.PositiveInfinity
.public final double findRayIntersection(PointD anchor, PointD direction)
GeneralPath
using a given epsilon for flattening Bézier curves
contained in the path.
The ray is treated like an infinite line starting at the given anchor point and continuing in the direction of rayX,
rayY. The ray vector does not need to be normalized. The value returned will be the smallest positive value such that
the point depicted by (anchorX + result * rayX, anchorY + result * rayY)
is an intersection point between the
ray and the path.
anchor
- The coordinates of the anchor point of the ray.direction
- The coordinates of the direction vector of the ray.double.PositiveInfinity
.public final double findRayIntersection(PointD anchor, PointD direction, double eps)
GeneralPath
using a given epsilon for flattening Bézier curves
contained in the path.
The ray is treated like an infinite line starting at the given anchor point and continuing in the direction of rayX,
rayY. The ray vector does not need to be normalized. The value returned will be the smallest positive value such that
the point depicted by (anchorX + result * rayX, anchorY + result * rayY)
is an intersection point between the
ray and the path.
anchor
- The coordinates of the anchor point of the ray.direction
- The coordinates of the direction vector of the ray.eps
- The epsilon value that is used for the accuracy with which the implementation should test Bézier curves within the path
for intersections.double.PositiveInfinity
.public final GeneralPath flatten(double eps)
This will create a path that consists of PathType.MOVE_TO
, PathType.LINE_TO
, and PathType.CLOSE
operations only.
eps
- The maximum value the flattened path may diverge from the original path for the Bézier curve line interpolations.public final RectD getBounds()
This will not yield tight bounds if the path contains Bézier curves. The bounds are calculated using the union of all points and control points contained in this path.
getBounds(double)
public final RectD getBounds(double eps)
eps
- The epsilon to use for the flattening operation.public final int getCount()
PathType
operations stored in this instance.public final PointD getLastCoordinate()
public final double getLastX()
public final double getLastY()
public final double getLength()
Note that Bézier curves are still treated as line segments.
public final PointD getPoint(double ratio)
Note that this implementation still treats Bézier curves as linear segments.
ratio
- A value between 0 and 1 inclusively that indicates a ratio from the beginning to the end of this path.public final int getSegmentCount()
PathType.LINE_TO
and PathType.CLOSE
operations.public final Tangent getTangent(double ratio)
Note that this implementation still treats Bézier curves as linear segments.
ratio
- A value between 0 and 1 inclusively that indicates a ratio from the beginning to the end of this path.public final Tangent getTangent(int segmentIndex, double ratio)
Note that this implementation still treats Bézier curves as linear segments. Also note that the tangent vector may not be normalized.
ratio
- A value between 0 and 1 inclusively that indicates a ratio from the beginning to the end of the segment at segmentIndex
for this path.segmentIndex
- The segment index to determine a point at.public final boolean intersects(RectD rectangle, double eps)
This will yield false if the rectangle is fully contained within the area of the path.
rectangle
- The rectangle to test.eps
- The epsilon to use for fuzzy testing.public final boolean isEmpty()
PathType.MOVE_TO
..public final boolean isVisible()
public final void lineTo(double x, double y)
PathType.LINE_TO
operation to the path elements.
This draws a line from the current end point to the given position.
x
- The x coordinate of the line's end point.y
- The y coordinate of the line's end point.public final void lineTo(IPoint point)
PathType.LINE_TO
operation to the path elements.
This draws a line from the current end point to the given position.
point
- The coordinates of the line's end point.public final void lineTo(PointD point)
PathType.LINE_TO
operation to the path elements.
This draws a line from the current end point to the given position.
point
- The coordinates of the line's end point.public final boolean mayIntersectClip(RectD clip, double width)
This method will return false if the clip is entirely inside the area of this path.
clip
- The clip to check for intersection.width
- The width of the stroke.false
if it is guaranteed that this instance would not intersect the given clip.public final void moveTo(double x, double y)
PathType.MOVE_TO
operation to the path elements.
This moves the pen to a new position without drawing a line.
x
- The next x coordinate.y
- The next y coordinate.public final void moveTo(IPoint point)
PathType.MOVE_TO
operation to the path elements.
This moves the pen to a new position without drawing a line.
point
- The next coordinate.public final void moveTo(PointD point)
PathType.MOVE_TO
operation to the path elements.
This moves the pen to a new position without drawing a line.
point
- The next coordinate.public final boolean pathContains(PointD point, double eps)
point
- The p coordinates of the point to test.eps
- The allowed distance from the point to the path that is considered a hit.public final void prepend(GeneralPath other, boolean connect)
If connect
is true
, the initial PathType.MOVE_TO
in this
will be transformed into a
PathType.LINE_TO
.
other
- The path to append to this path.connect
- Whether to connect the path to the end of this path.public final void quadTo(double cx, double cy, double x, double y)
quadratic Bézier curve
to the path elements.
This draws a quadratic Bézier curve from the current end point to the given position using the given control point.
cx
- The x coordinate of the Bézier control point.cy
- The y coordinate of the Bézier control point.x
- The x coordinate of the curve's end point.y
- The y coordinate of the curve's end point.public final void quadTo(IPoint center, IPoint point)
quadratic Bézier curve
to the path elements.
This draws a quadratic Bézier curve from the current end point to the given position using the given control point.
center
- The coordinates of the Bézier control point.point
- The coordinates of the curve's end point.public final void quadTo(PointD center, PointD point)
quadratic Bézier curve
to the path elements.
This draws a quadratic Bézier curve from the current end point to the given position using the given control point.
center
- The coordinates of the Bézier control point.point
- The coordinates of the curve's end point.public final void transform(Matrix2D transform)
GeneralPath
in place using the given transform.transform
- The matrix to multiply the geometry with.public void updatePath(Path path, Paint brush, Pen pen, Matrix2D transform)
createPath(Paint, Pen, Matrix2D)
.path
- The path instance to update.brush
- The brush to use for filling the path.pen
- The pen to use for stroking.transform
- The matrix to apply to the path data.createPath(Paint, Pen, Matrix2D)