Package org.opensourcephysics.display
Class GraphicsLib
java.lang.Object
org.opensourcephysics.display.GraphicsLib
Library of useful computer graphics routines such as geometry routines
for computing the intersection of different shapes and rendering methods
for computing bounds and performing optimized drawing.
- Author:
- jeffrey heer
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Indicates intersection between shapesstatic final int
Indicates no intersection between shapesstatic final int
Indicates two lines are parallel -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic GeneralPath
cardinalSpline
(float[] pts, float slack, boolean closed) Compute a cardinal spline, a series of cubic Bezier splines smoothly connecting a set of points.static GeneralPath
cardinalSpline
(float[] pts, int start, int npoints, float slack, boolean closed) Compute a cardinal spline, a series of cubic Bezier splines smoothly connecting a set of points.static GeneralPath
cardinalSpline
(GeneralPath p, float[] pts, float slack, boolean closed, float tx, float ty) Compute a cardinal spline, a series of cubic Bezier splines smoothly connecting a set of points.static GeneralPath
cardinalSpline
(GeneralPath p, float[] pts, int start, int npoints, float slack, boolean closed, float tx, float ty) Compute a cardinal spline, a series of cubic Bezier splines smoothly connecting a set of points.static float[]
centroid
(float[] pts, int len) Computes the mean, or centroid, of a set of pointsstatic double[]
convexHull
(double[] pts, int len) Computes the 2D convex hull of a set of points using Graham's scanning algorithm.static double[]
convexHull
(double[] pts, int len, float[] angles, int[] idx, int[] stack) Computes the 2D convex hull of a set of points using Graham's scanning algorithm.static void
expand
(Rectangle2D r, double amount) Expand a rectangle by the given amount.static void
growPolygon
(float[] pts, int len, float amt) Expand a polygon by adding the given distance along the line from the centroid of the polyong.static int
intersectLineLine
(double a1x, double a1y, double a2x, double a2y, double b1x, double b1y, double b2x, double b2y, Point2D intersect) Compute the intersection of two line segments.static int
intersectLineLine
(Line2D a, Line2D b, Point2D intersect) Compute the intersection of two line segments.static int
intersectLineRectangle
(Line2D l, Rectangle2D r, Point2D[] pts) Compute the intersection of a line and a rectangle.static int
intersectLineRectangle
(Point2D a1, Point2D a2, Rectangle2D r, Point2D[] pts) Compute the intersection of a line and a rectangle.static GeneralPath
stackSpline
(GeneralPath p, float[] pts, float epsilon, float slack, boolean closed, float tx, float ty) Computes a set of curves using the cardinal spline approach, but using straight lines for completely horizontal or vertical segments.static GeneralPath
stackSpline
(GeneralPath p, float[] pts, int start, int npoints, float epsilon, float slack, boolean closed, float tx, float ty) Computes a set of curves using the cardinal spline approach, but using straight lines for completely horizontal or vertical segments.
-
Field Details
-
NO_INTERSECTION
public static final int NO_INTERSECTIONIndicates no intersection between shapes- See Also:
-
COINCIDENT
public static final int COINCIDENTIndicates intersection between shapes- See Also:
-
PARALLEL
public static final int PARALLELIndicates two lines are parallel- See Also:
-
-
Constructor Details
-
GraphicsLib
public GraphicsLib()
-
-
Method Details
-
intersectLineLine
Compute the intersection of two line segments.- Parameters:
a
- the first line segmentb
- the second line segmentintersect
- a Point in which to store the intersection point- Returns:
- the intersection code. One of
NO_INTERSECTION
,COINCIDENT
, orPARALLEL
.
-
intersectLineLine
public static int intersectLineLine(double a1x, double a1y, double a2x, double a2y, double b1x, double b1y, double b2x, double b2y, Point2D intersect) Compute the intersection of two line segments.- Parameters:
a1x
- the x-coordinate of the first endpoint of the first linea1y
- the y-coordinate of the first endpoint of the first linea2x
- the x-coordinate of the second endpoint of the first linea2y
- the y-coordinate of the second endpoint of the first lineb1x
- the x-coordinate of the first endpoint of the second lineb1y
- the y-coordinate of the first endpoint of the second lineb2x
- the x-coordinate of the second endpoint of the second lineb2y
- the y-coordinate of the second endpoint of the second lineintersect
- a Point in which to store the intersection point- Returns:
- the intersection code. One of
NO_INTERSECTION
,COINCIDENT
, orPARALLEL
.
-
intersectLineRectangle
Compute the intersection of a line and a rectangle.- Parameters:
a1
- the first endpoint of the linea2
- the second endpoint of the liner
- the rectanglepts
- a length 2 or greater array of points in which to store the results- Returns:
- the intersection code. One of
NO_INTERSECTION
,COINCIDENT
, orPARALLEL
.
-
intersectLineRectangle
Compute the intersection of a line and a rectangle.- Parameters:
l
- the liner
- the rectanglepts
- a length 2 or greater array of points in which to store the results- Returns:
- the intersection code. One of
NO_INTERSECTION
,COINCIDENT
, orPARALLEL
.
-
convexHull
public static double[] convexHull(double[] pts, int len) Computes the 2D convex hull of a set of points using Graham's scanning algorithm. The algorithm has been implemented as described in Cormen, Leiserson, and Rivest's Introduction to Algorithms. The running time of this algorithm is O(n log n), where n is the number of input points.- Parameters:
pts
- the input points in [x0,y0,x1,y1,...] orderlen
- the length of the pts array to consider (2 * #points)- Returns:
- the convex hull of the input points
-
convexHull
public static double[] convexHull(double[] pts, int len, float[] angles, int[] idx, int[] stack) Computes the 2D convex hull of a set of points using Graham's scanning algorithm. The algorithm has been implemented as described in Cormen, Leiserson, and Rivest's Introduction to Algorithms. The running time of this algorithm is O(n log n), where n is the number of input points.- Parameters:
pts
-- Returns:
- the convex hull of the input points
-
centroid
public static float[] centroid(float[] pts, int len) Computes the mean, or centroid, of a set of points- Parameters:
pts
- the points array, in x1, y1, x2, y2, ... arrangement.len
- the length of the array to consider- Returns:
- the centroid as a length-2 float array
-
growPolygon
public static void growPolygon(float[] pts, int len, float amt) Expand a polygon by adding the given distance along the line from the centroid of the polyong.- Parameters:
pts
- the polygon to expand, a set of points in a float arraylen
- the length of the range of the array to consideramt
- the amount by which to expand the polygon, each point will be moved this distance along the line from the centroid of the polygon to the given point.
-
cardinalSpline
Compute a cardinal spline, a series of cubic Bezier splines smoothly connecting a set of points. Cardinal splines maintain C(1) continuity, ensuring the connected spline segments form a differentiable curve, ensuring at least a minimum level of smoothness.- Parameters:
pts
- the points to interpolate with a cardinal splineslack
- a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable valueclosed
- true if the cardinal spline should be closed (i.e. return to the starting point), false for an open curve- Returns:
- the cardinal spline as a Java2D
GeneralPath
instance.
-
cardinalSpline
public static GeneralPath cardinalSpline(float[] pts, int start, int npoints, float slack, boolean closed) Compute a cardinal spline, a series of cubic Bezier splines smoothly connecting a set of points. Cardinal splines maintain C(1) continuity, ensuring the connected spline segments form a differentiable curve, ensuring at least a minimum level of smoothness.- Parameters:
pts
- the points to interpolate with a cardinal splinestart
- the starting index from which to read pointsnpoints
- the number of points to considerslack
- a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable valueclosed
- true if the cardinal spline should be closed (i.e. return to the starting point), false for an open curve- Returns:
- the cardinal spline as a Java2D
GeneralPath
instance.
-
cardinalSpline
public static GeneralPath cardinalSpline(GeneralPath p, float[] pts, float slack, boolean closed, float tx, float ty) Compute a cardinal spline, a series of cubic Bezier splines smoothly connecting a set of points. Cardinal splines maintain C(1) continuity, ensuring the connected spline segments form a differentiable curve, ensuring at least a minimum level of smoothness.- Parameters:
p
- the GeneralPath instance to use to store the resultpts
- the points to interpolate with a cardinal splineslack
- a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable valueclosed
- true if the cardinal spline should be closed (i.e. return to the starting point), false for an open curvetx
- a value by which to translate the curve along the x-dimensionty
- a value by which to translate the curve along the y-dimension- Returns:
- the cardinal spline as a Java2D
GeneralPath
instance.
-
cardinalSpline
public static GeneralPath cardinalSpline(GeneralPath p, float[] pts, int start, int npoints, float slack, boolean closed, float tx, float ty) Compute a cardinal spline, a series of cubic Bezier splines smoothly connecting a set of points. Cardinal splines maintain C(1) continuity, ensuring the connected spline segments form a differentiable curve, ensuring at least a minimum level of smoothness.- Parameters:
p
- the GeneralPath instance to use to store the resultpts
- the points to interpolate with a cardinal splinestart
- the starting index from which to read pointsnpoints
- the number of points to considerslack
- a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable valueclosed
- true if the cardinal spline should be closed (i.e. return to the starting point), false for an open curvetx
- a value by which to translate the curve along the x-dimensionty
- a value by which to translate the curve along the y-dimension- Returns:
- the cardinal spline as a Java2D
GeneralPath
instance.
-
stackSpline
public static GeneralPath stackSpline(GeneralPath p, float[] pts, float epsilon, float slack, boolean closed, float tx, float ty) Computes a set of curves using the cardinal spline approach, but using straight lines for completely horizontal or vertical segments.- Parameters:
p
- the GeneralPath instance to use to store the resultpts
- the points to interpolate with the splineepsilon
- threshold value under which to treat the difference between two values to be zero. Used to determine which segments to treat as lines rather than curves.slack
- a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable valueclosed
- true if the spline should be closed (i.e. return to the starting point), false for an open curvetx
- a value by which to translate the curve along the x-dimensionty
- a value by which to translate the curve along the y-dimension- Returns:
- the stack spline as a Java2D
GeneralPath
instance.
-
stackSpline
public static GeneralPath stackSpline(GeneralPath p, float[] pts, int start, int npoints, float epsilon, float slack, boolean closed, float tx, float ty) Computes a set of curves using the cardinal spline approach, but using straight lines for completely horizontal or vertical segments.- Parameters:
p
- the GeneralPath instance to use to store the resultpts
- the points to interpolate with the splinestart
- the starting index from which to read pointsnpoints
- the number of points to considerepsilon
- threshold value under which to treat the difference between two values to be zero. Used to determine which segments to treat as lines rather than curves.slack
- a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable valueclosed
- true if the spline should be closed (i.e. return to the starting point), false for an open curvetx
- a value by which to translate the curve along the x-dimensionty
- a value by which to translate the curve along the y-dimension- Returns:
- the stack spline as a Java2D
GeneralPath
instance.
-
expand
Expand a rectangle by the given amount.- Parameters:
r
- the rectangle to expandamount
- the amount by which to expand the rectangle
-