|
CS101 Package Problem Set Javadocs | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object cs101.awt.geom.ShapeUtils
This utility class holds routines for doing conversions on classes that
implement the Shape
interface. Generally the first step is
in each routine is to convert the shape to a GeneralPath
, and
return values of type Shape
will in fact be instances of
the GeneralPath
class unless specified otherwise.
Java's GeneralPath
class is a series of concatenated line
segments, second order (quadratic) Bezier curves and third order (cubic)
Bezier curves. Bezier Curves consist of two types of points: knot
points which are points that lie on the curve, and control points
which do not lie on the curve but denote vectors that influence the shape
of the curve between knot points.
None of the routines in this class attempt to convert between lines, quadratics, and cubics. A shape consisting entirely of lines, will remain entirely line based, and a purely cubic shape will also remain cubic. Mixed shapes will remain mixed, but the ratios of the number of segements of any two types is almost certain to change.
Method Summary | |
static double |
angleToVert(java.awt.geom.Line2D.Float aLine)
Find the angle between a given line segment and the vertical axis in direction that roataes the positive x axis towards positive y axis |
static java.awt.geom.Line2D.Float |
bounce(java.awt.geom.Line2D.Float surface,
java.awt.geom.Line2D.Float velVect)
Compute a velocity vector after a bounce off a given surface. |
static void |
createIntermediateCurve(java.awt.geom.Point2D.Float start,
float[] pts)
Mutate the supplied array of float to contain an additional knot and modify the associated control points. |
static void |
createIntermediateLine(java.awt.geom.Point2D.Float start,
float[] pts)
Mutate the suppled array of float to contain an additional knot. |
static void |
createIntermediateQuad(java.awt.geom.Point2D.Float start,
float[] pts)
Mutate the suppled array of float to contain an additional knot and modify the associated control points. |
static java.awt.geom.Line2D.Float |
doReflect(java.awt.Shape s1,
java.awt.Shape s2,
double v1X,
double v1Y)
Find the resultant velocity from the collision of a mobile and imobile shape. |
static java.awt.geom.Line2D.Float |
doReflect(java.awt.Shape s1,
java.awt.Shape s2,
java.awt.geom.Line2D.Float velVect1)
Find the resultant velocity from the collision of a mobile and imobile shape. |
static double |
doXReflect(java.awt.Shape s1,
java.awt.Shape s2,
double v1X,
double v1Y)
Find the new x velocity after reflecting a moving shape from a imobile shape. |
static double |
doYReflect(java.awt.Shape s1,
java.awt.Shape s2,
double v1X,
double v1Y)
Find the new x velocity after reflecting a moving shape from a imobile shape. |
static java.util.ArrayList |
getContainedPoints(java.awt.Shape container,
java.awt.Shape containee)
Get a list of the points belonging to one shape and contained within another. |
static java.awt.geom.GeneralPath |
getImprovedPath(java.awt.geom.GeneralPath aPath,
float precision)
Used iteratively by getPrecisePath to acheive as specified precision. |
static java.awt.geom.Line2D.Float |
getLineFromNeighbors(java.awt.Shape s,
java.awt.geom.Point2D.Float pt)
Find the line between points on either side of a knot point on a given shape. |
static java.awt.geom.Point2D.Float |
getNearestPoint(java.awt.Shape s,
java.awt.geom.Point2D.Float pt)
Find the knot point in the given shape that is closest to the specified point. |
static java.awt.geom.GeneralPath |
getPrecisePath(java.awt.geom.GeneralPath aPath,
float precision)
Increase the precision of a given GeneralPath object. |
static java.awt.Shape |
getPreciseShape(java.awt.Shape aShape,
float precision)
Convert any AWT shape into a shape with a specified precision. |
static boolean |
isOverlapping(java.awt.Shape shapeA,
java.awt.Shape shapeB)
Test to see if two Shape objects overlap. |
static java.awt.geom.Point2D.Float |
meanContainedPoint(java.awt.Shape container,
java.awt.Shape containee)
Calcualte the average of all points contained within one shape and belonging to another. |
static java.awt.geom.Point2D.Float |
meanPoint(java.util.List pointList)
Find the mean location of a list of Point2D.Float . |
static java.awt.geom.Point2D.Float |
midPoint(float x1,
float y1,
float x2,
float y2)
Find the midpoint of a specifed line segment. |
static java.awt.geom.Line2D.Float |
nearestSegment(java.awt.Shape aShape,
java.awt.geom.Point2D.Float aPoint)
Find the line segment between two consecutive knot points of the given Shape that is closest to a specified point. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public static java.awt.geom.Line2D.Float bounce(java.awt.geom.Line2D.Float surface, java.awt.geom.Line2D.Float velVect)
surface
- The line representing the angle of the surface to bounce
off of.velVect
- A line with starting point 0,0 representing the velocity
vector to be bounced.
public static java.awt.geom.Line2D.Float doReflect(java.awt.Shape s1, java.awt.Shape s2, double v1X, double v1Y)
s1
- The moving shape (tiny mass)s2
- The imobile shape (infinite mass)v1X
- The x component of the velocity of s1v1Y
- The y component of the velocity of s1
public static java.awt.geom.Line2D.Float doReflect(java.awt.Shape s1, java.awt.Shape s2, java.awt.geom.Line2D.Float velVect1)
s1
- The moving shape (tiny mass)s2
- The imobile shape (infinite mass)velVect1
- The velocity vector of s1, (x1,y1 point at 0.0 x2,y2
representing the x and y components of the velocity)
public static double doXReflect(java.awt.Shape s1, java.awt.Shape s2, double v1X, double v1Y)
s1
- The moving shape (tiny mass)s2
- The imobile shape (infinite mass)v1X
- The x component of the velocity of s1v1Y
- The y component of the velocity of s1public static double doYReflect(java.awt.Shape s1, java.awt.Shape s2, double v1X, double v1Y)
s1
- The moving shape (tiny mass)s2
- The imobile shape (infinite mass)v1X
- The x component of the velocity of s1v1Y
- The y component of the velocity of s1public static double angleToVert(java.awt.geom.Line2D.Float aLine)
aLine
- A line to find the angle of
public static java.awt.geom.Line2D.Float getLineFromNeighbors(java.awt.Shape s, java.awt.geom.Point2D.Float pt)
s
- The shape to find points from, comprised of than one point.pt
- The point that is a knot of s for which we want a neighbors
line.public static java.awt.geom.Point2D.Float getNearestPoint(java.awt.Shape s, java.awt.geom.Point2D.Float pt)
s
- The shape to find a point frompt
- The point to which the points of the shape are compared.
public static java.awt.geom.Line2D.Float nearestSegment(java.awt.Shape aShape, java.awt.geom.Point2D.Float aPoint) throws NoUniqueLineException, NotEnoughPointsException
Shape
that is closest to a specified point. Note that this
method will return a line even if the segment it represents in the shape
is a cubic or quadratic bezier curve.
aShape
- The shape from which to find the line segmentaPoint
- The point to which the distance is compared.
NoUniqueLineException
- If there are two or more lines equidistant
from the point.
NotEnoughPointsException
- If the aShape contains less than 2 pointspublic static java.awt.geom.Point2D.Float meanPoint(java.util.List pointList)
Point2D.Float
.
pointList
- A list containing only Point2D.Float
java.lang.ClassCastException
- If the objects in the supplied list are not
of type Point2D.Floatpublic static java.awt.geom.Point2D.Float meanContainedPoint(java.awt.Shape container, java.awt.Shape containee) throws NotEnoughPointsException
container
- The shape which may contain pointscontainee
- The shape whose points may be contained
NotEnoughPointsException
public static java.util.ArrayList getContainedPoints(java.awt.Shape container, java.awt.Shape containee)
container
- The shape which may contain pointscontainee
- The shape whose points may be contained
public static boolean isOverlapping(java.awt.Shape shapeA, java.awt.Shape shapeB)
Shape
objects overlap.
The algorithim in this test will only detect the type of
overlap where a knot point from one shape is
contained by the other shape (as determined by each shape's
implementation of the contains(double, double)
method.
shapeA
- the first shapeshapeB
- the second shape
public static java.awt.Shape getPreciseShape(java.awt.Shape aShape, float precision)
aShape
- the shape to be converted if necessary.precision
- the maximum tolerable distance between knot points.
public static java.awt.geom.GeneralPath getPrecisePath(java.awt.geom.GeneralPath aPath, float precision)
GeneralPath
object.
The specified precision indicates the maximum tolerable distance
between knot points. If the path is already
precise enough then it is returned unmodified.
aPath
- The path to convert if necessary.precision
- the maximum tolerable distance between knot points.
public static java.awt.geom.GeneralPath getImprovedPath(java.awt.geom.GeneralPath aPath, float precision)
Note that because GeneralPath does not overide equals we return a reference to the SAME object passed in if there are no improvements to be made. aPath.equals(newPath) is equivalent to aPath == newPath.
aPath
- The path that may need to be improved.precision
- The minimum acceptable distance between knot points.public static void createIntermediateLine(java.awt.geom.Point2D.Float start, float[] pts)
start
- the start point of the Line.pts
- An array of at least size 4 containing the end point x,y
coordinates at position 0 and 1 respectively. This array
format matches that returned by PathIterator
public static void createIntermediateQuad(java.awt.geom.Point2D.Float start, float[] pts)
start
- the start point of the quadratic Bezier spline.pts
- An array of at least size 8 containing the end point x,y
coordinates at position 2 and 3 respectively. The control
point should be specified at positions 0 and 1. This array
format matches the format returned by
PathIterator
.public static void createIntermediateCurve(java.awt.geom.Point2D.Float start, float[] pts)
start
- the start point of the quadratic Bezier spline.pts
- An array of at least size 8 containing the end point x,y
coordinates at position 4 and 5 respectively. The control
point for the start point should be specified at positions
0 and 1; The control point for the end point should be
specified at positions 2 and 3 This array format matches the
format returned by PathIterator
.public static java.awt.geom.Point2D.Float midPoint(float x1, float y1, float x2, float y2)
x1
- start x coordinatey1
- start y coordinatex2
- end x coordinatey2
- end y coordinate
|
CS101 Package Problem Set Javadocs | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |