org.opensourcephysics.display
Class GraphicsLib

java.lang.Object
  extended by org.opensourcephysics.display.GraphicsLib

public class GraphicsLib
extends java.lang.Object

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
static int COINCIDENT
          Indicates intersection between shapes
static int NO_INTERSECTION
          Indicates no intersection between shapes
static int PARALLEL
          Indicates two lines are parallel
 
Constructor Summary
GraphicsLib()
           
 
Method Summary
static java.awt.geom.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 java.awt.geom.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 java.awt.geom.GeneralPath cardinalSpline(java.awt.geom.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 java.awt.geom.GeneralPath cardinalSpline(java.awt.geom.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 points
static 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(java.awt.geom.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, java.awt.geom.Point2D intersect)
          Compute the intersection of two line segments.
static int intersectLineLine(java.awt.geom.Line2D a, java.awt.geom.Line2D b, java.awt.geom.Point2D intersect)
          Compute the intersection of two line segments.
static int intersectLineRectangle(java.awt.geom.Line2D l, java.awt.geom.Rectangle2D r, java.awt.geom.Point2D[] pts)
          Compute the intersection of a line and a rectangle.
static int intersectLineRectangle(java.awt.geom.Point2D a1, java.awt.geom.Point2D a2, java.awt.geom.Rectangle2D r, java.awt.geom.Point2D[] pts)
          Compute the intersection of a line and a rectangle.
static java.awt.geom.GeneralPath stackSpline(java.awt.geom.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 java.awt.geom.GeneralPath stackSpline(java.awt.geom.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_INTERSECTION

public static final int NO_INTERSECTION
Indicates no intersection between shapes

See Also:
Constant Field Values

COINCIDENT

public static final int COINCIDENT
Indicates intersection between shapes

See Also:
Constant Field Values

PARALLEL

public static final int PARALLEL
Indicates two lines are parallel

See Also:
Constant Field Values
Constructor Detail

GraphicsLib

public GraphicsLib()
Method Detail

intersectLineLine

public static int intersectLineLine(java.awt.geom.Line2D a,
                                    java.awt.geom.Line2D b,
                                    java.awt.geom.Point2D intersect)
Compute the intersection of two line segments.

Parameters:
a - the first line segment
b - the second line segment
intersect - a Point in which to store the intersection point
Returns:
the intersection code. One of NO_INTERSECTION, COINCIDENT, or PARALLEL.

intersectLineLine

public static int intersectLineLine(double a1x,
                                    double a1y,
                                    double a2x,
                                    double a2y,
                                    double b1x,
                                    double b1y,
                                    double b2x,
                                    double b2y,
                                    java.awt.geom.Point2D intersect)
Compute the intersection of two line segments.

Parameters:
a1x - the x-coordinate of the first endpoint of the first line
a1y - the y-coordinate of the first endpoint of the first line
a2x - the x-coordinate of the second endpoint of the first line
a2y - the y-coordinate of the second endpoint of the first line
b1x - the x-coordinate of the first endpoint of the second line
b1y - the y-coordinate of the first endpoint of the second line
b2x - the x-coordinate of the second endpoint of the second line
b2y - the y-coordinate of the second endpoint of the second line
intersect - a Point in which to store the intersection point
Returns:
the intersection code. One of NO_INTERSECTION, COINCIDENT, or PARALLEL.

intersectLineRectangle

public static int intersectLineRectangle(java.awt.geom.Point2D a1,
                                         java.awt.geom.Point2D a2,
                                         java.awt.geom.Rectangle2D r,
                                         java.awt.geom.Point2D[] pts)
Compute the intersection of a line and a rectangle.

Parameters:
a1 - the first endpoint of the line
a2 - the second endpoint of the line
r - the rectangle
pts - a length 2 or greater array of points in which to store the results
Returns:
the intersection code. One of NO_INTERSECTION, COINCIDENT, or PARALLEL.

intersectLineRectangle

public static int intersectLineRectangle(java.awt.geom.Line2D l,
                                         java.awt.geom.Rectangle2D r,
                                         java.awt.geom.Point2D[] pts)
Compute the intersection of a line and a rectangle.

Parameters:
l - the line
r - the rectangle
pts - a length 2 or greater array of points in which to store the results
Returns:
the intersection code. One of NO_INTERSECTION, COINCIDENT, or PARALLEL.

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,...] order
len - 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 array
len - the length of the range of the array to consider
amt - 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

public static java.awt.geom.GeneralPath cardinalSpline(float[] pts,
                                                       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 spline
slack - a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable value
closed - 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 java.awt.geom.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 spline
start - the starting index from which to read points
npoints - the number of points to consider
slack - a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable value
closed - 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 java.awt.geom.GeneralPath cardinalSpline(java.awt.geom.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 result
pts - the points to interpolate with a cardinal spline
slack - a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable value
closed - true if the cardinal spline should be closed (i.e. return to the starting point), false for an open curve
tx - a value by which to translate the curve along the x-dimension
ty - a value by which to translate the curve along the y-dimension
Returns:
the cardinal spline as a Java2D GeneralPath instance.

cardinalSpline

public static java.awt.geom.GeneralPath cardinalSpline(java.awt.geom.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 result
pts - the points to interpolate with a cardinal spline
start - the starting index from which to read points
npoints - the number of points to consider
slack - a parameter controlling the "tightness" of the spline to the control points, 0.10 is a typically suitable value
closed - true if the cardinal spline should be closed (i.e. return to the starting point), false for an open curve
tx - a value by which to translate the curve along the x-dimension
ty - a value by which to translate the curve along the y-dimension
Returns:
the cardinal spline as a Java2D GeneralPath instance.

stackSpline

public static java.awt.geom.GeneralPath stackSpline(java.awt.geom.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 result
pts - the points to interpolate with the spline
epsilon - 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 value
closed - true if the spline should be closed (i.e. return to the starting point), false for an open curve
tx - a value by which to translate the curve along the x-dimension
ty - a value by which to translate the curve along the y-dimension
Returns:
the stack spline as a Java2D GeneralPath instance.

stackSpline

public static java.awt.geom.GeneralPath stackSpline(java.awt.geom.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 result
pts - the points to interpolate with the spline
start - the starting index from which to read points
npoints - the number of points to consider
epsilon - 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 value
closed - true if the spline should be closed (i.e. return to the starting point), false for an open curve
tx - a value by which to translate the curve along the x-dimension
ty - a value by which to translate the curve along the y-dimension
Returns:
the stack spline as a Java2D GeneralPath instance.

expand

public static void expand(java.awt.geom.Rectangle2D r,
                          double amount)
Expand a rectangle by the given amount.

Parameters:
r - the rectangle to expand
amount - the amount by which to expand the rectangle