Class GeometricUtilities
public class GeometricUtilities
Static utility methods for 2D geometric calculations used throughout the engine, such as distance and angle computations, shape intersection
checks, scaling, and ray casting.
-
Method Summary
Modifier and TypeMethodDescriptionstatic doublecalcRotationAngleInDegrees(double centerX, double centerY, double targetX, double targetY) Calculates the clockwise rotation angle in degrees from a center point to a target point.static doublecalcRotationAngleInDegrees(Point2D centerPt, Point2D targetPt) Calculates the angle from centerPt to targetPt in degrees.static booleancontains(Rectangle2D rectangle, Point2D p) Contains.static doubledistance(double p1X, double p1Y, double p2X, double p2Y) Computes the Euclidean distance between two points.static doubleComputes the Euclidean distance between two points.static doubledistance(Rectangle2D rect, Point2D p) Distance.static booleanDetermines whether two points are within the given epsilon distance of each other.static Rectangle2Dextrude(Rectangle2D rect, double ext) Returns a new rectangle with the given extension applied to all sides of the input rectangle.static Point2DgetAverageLocation(Point2D... points) Returns the average (centroid) location of the supplied points.static Point2DgetAverageLocation(Collection<Point2D> points) Returns the average (centroid) location of the supplied collection of points.static Point2DgetCenter(double x1, double y1, double x2, double y2) Returns the midpoint of the rectangle defined by the two given coordinate pairs.static Point2DReturns the midpoint of the given line.static Point2DReturns the midpoint between two points.static Point2DgetCenter(RectangularShape shape) Returns the center of a shape whose geometry is defined by a rectangular frame.static Ellipse2DCreates a circle (as anEllipse2D) with the given center and radius.static Line2D[]getConnectingLines(Point2D point, Point2D[] rectPoints) Returns the line segments that connect the supplied point to each of the supplied rectangle corner points.static List<Line2D.Double> getConstrainingLines(Area area) Extracts the constraining line segments of the givenAreaby iterating over its path.static floatgetDeltaX(double angle) Computes the unit X displacement (cosine component) for the given angle in degrees, expressed in the LITIengine coordinate system where0degrees points NORTH.static doublegetDeltaX(double angle, double delta) Computes the X displacement for the given angle and delta magnitude.static floatgetDeltaY(double angle) Computes the unit Y displacement (sine component) for the given angle in degrees, expressed in the LITIengine coordinate system where0degrees points NORTH.static doublegetDeltaY(double angle, double delta) Computes the Y displacement for the given angle and delta magnitude.static doublegetDiagonal(Rectangle2D rect) Computes the diagonal length of the given rectangle.static Point2DgetIntersectionPoint(Line2D lineA, Line2D lineB) Gets the intersection point.static Point2DgetIntersectionPoint(Line2D line, Rectangle2D rectangle) Intersects.getIntersectionPoints(Line2D line, Rectangle2D rectangle) Gets the intersection points.static Line2D[]getLines(Rectangle2D rectangle) Gets the lines.static Point2DgetPerpendicularIntersection(Point2D point, Line2D line) Gets the perpendicular intersection.static Point2DgetPointOnCircle(Point2D center, double radius, double angle) Returns the point on the circle defined bycenterandradiusat the given angle.Extracts the start points of all segments of the supplied path.getPoints(Rectangle2D rectangle) Gets the points.getPointsBetweenPoints(Point2D point1, Point2D point2) Gets the points between the specified points using the Bresenham algorithm.static booleanintersects(Ellipse2D a, Ellipse2D b) Tests whether two ellipses intersect.static booleanintersects(Rectangle2D a, Rectangle2D b) Tests whether two rectangles meaningfully intersect.static doublenormalizeAngle(double angle) Normalizes the specified angle to the range between 0-360 degree.static Point2DProject a line from a point with a given length and angle, return the point where the line ends.static Point2DProjects a point from end along the vector (end - start) by the given scalar amount.static Point2D[]rayCastPoints(Point2D point, Rectangle2D rectangle) Performs a ray cast from the given point against the supplied rectangle and returns the visible corner points (i.e. corners that are not occluded by another corner along the ray).static ShapescaleRect(Rectangle2D shape, int max) Scales the given rectangle so that its longest side equalsmaxpixels, preserving its aspect ratio.static ShapescaleShape(Shape shape, double scale) Applies a uniform scaling transformation to the given shape.static Dimension2DscaleWithRatio(double width, double height, int max) Computes aDimension2Dpreserving the aspect ratio ofwidth:heightso that the longest side equalsmax.static booleanshapeIntersects(Shape shapeA, Shape shapeB) Shape intersects.static ShapetranslateShape(Shape shape, Point2D newLocation) Translates the given shape so that the top-left corner of its bounding box matches the supplied location.
-
Method Details
-
calcRotationAngleInDegrees
public static double calcRotationAngleInDegrees(double centerX, double centerY, double targetX, double targetY) Calculates the clockwise rotation angle in degrees from a center point to a target point. The returned angle is in the range[0, 360)and uses the LITIengine coordinate system where0degrees points NORTH.- Parameters:
centerX- the x coordinate of the center pointcenterY- the y coordinate of the center pointtargetX- the x coordinate of the target pointtargetY- the y coordinate of the target point- Returns:
- the clockwise rotation angle in degrees
-
calcRotationAngleInDegrees
Calculates the angle from centerPt to targetPt in degrees. The return should range from [0,360), rotating CLOCKWISE, 0 and 360 degrees represents NORTH, 90 degrees represents EAST, etc...
Assumes all points are in the same coordinate space. If they are not, you will need to call SwingUtilities.convertPointToScreen or equivalent on all arguments before passing them to this function.
- Parameters:
centerPt- Point we are rotating around.targetPt- Point we want to calcuate the angle to.- Returns:
- angle in degrees. This is the angle from centerPt to targetPt.
-
contains
Contains.- Parameters:
rectangle- the rectanglep- the p- Returns:
- true, if successful
-
distance
public static double distance(double p1X, double p1Y, double p2X, double p2Y) Computes the Euclidean distance between two points.- Parameters:
p1X- the x coordinate of the first pointp1Y- the y coordinate of the first pointp2X- the x coordinate of the second pointp2Y- the y coordinate of the second point- Returns:
- the Euclidean distance
-
distance
-
distance
Distance.- Parameters:
rect- the rectp- the p- Returns:
- the double
-
extrude
Returns a new rectangle with the given extension applied to all sides of the input rectangle.- Parameters:
rect- the input rectangleext- the number of units to add to each side- Returns:
- the extruded rectangle
-
equals
Determines whether two points are within the given epsilon distance of each other.- Parameters:
point1- the first pointpoint2- the second pointepsilon- the maximum allowed distance for the points to be considered equal- Returns:
trueif the distance between the points is less thanepsilon
-
getConnectingLines
Returns the line segments that connect the supplied point to each of the supplied rectangle corner points.- Parameters:
point- the starting pointrectPoints- the corner points to connect to- Returns:
- the connecting line segments, one per corner point
-
getConstrainingLines
Extracts the constraining line segments of the givenAreaby iterating over its path. The returned list contains everyPathIterator.SEG_LINETOas well as the closing segments of every sub-path.- Parameters:
area- the area to inspect- Returns:
- the list of line segments that form the area's boundary
-
getDeltaX
public static float getDeltaX(double angle) Computes the unit X displacement (cosine component) for the given angle in degrees, expressed in the LITIengine coordinate system where0degrees points NORTH.- Parameters:
angle- the angle in degrees- Returns:
- the unit X displacement
-
getDeltaY
public static float getDeltaY(double angle) Computes the unit Y displacement (sine component) for the given angle in degrees, expressed in the LITIengine coordinate system where0degrees points NORTH.- Parameters:
angle- the angle in degrees- Returns:
- the unit Y displacement
-
getDeltaX
public static double getDeltaX(double angle, double delta) Computes the X displacement for the given angle and delta magnitude.- Parameters:
angle- the angle in degreesdelta- the magnitude- Returns:
- the X displacement
-
getDeltaY
public static double getDeltaY(double angle, double delta) Computes the Y displacement for the given angle and delta magnitude.- Parameters:
angle- the angle in degreesdelta- the magnitude- Returns:
- the Y displacement
-
getIntersectionPoint
-
getIntersectionPoint
Intersects.- Parameters:
line- the linerectangle- the rectangle- Returns:
- the point2 d
-
getIntersectionPoints
Gets the intersection points.- Parameters:
line- the linerectangle- the rectangle- Returns:
- the intersection points
-
getLines
Gets the lines.- Parameters:
rectangle- the rectangle- Returns:
- the lines
-
getDiagonal
Computes the diagonal length of the given rectangle.- Parameters:
rect- the rectangle; may benull- Returns:
- the diagonal length, or
0ifrectisnull
-
getCenter
-
getCenter
-
getCenter
Returns the midpoint of the rectangle defined by the two given coordinate pairs.- Parameters:
x1- the x coordinate of the first pointy1- the y coordinate of the first pointx2- the x coordinate of the second pointy2- the y coordinate of the second point- Returns:
- the midpoint
-
getCenter
Returns the center of a shape whose geometry is defined by a rectangular frame.
Works for any subclass of RectuangularShape, including:
Arc2D
Ellipse2D
Rectangle2D
RoundRectangle2D
- Parameters:
shape- the shape to retrieve the center of- Returns:
- a Point2D representing the center of the shape
- See Also:
-
getCircle
-
getAverageLocation
Returns the average (centroid) location of the supplied collection of points.- Parameters:
points- the points- Returns:
- the average location, or
nullifpointsis empty
-
getAverageLocation
-
getPerpendicularIntersection
-
getPointOnCircle
Returns the point on the circle defined bycenterandradiusat the given angle.- Parameters:
center- the center of the circleradius- the radius of the circleangle- the angle in degrees (clockwise,0pointing EAST)- Returns:
- the point on the circle
-
getPoints
-
getPoints
Gets the points.- Parameters:
rectangle- the rectangle- Returns:
- the points
-
getPointsBetweenPoints
-
intersects
Tests whether two rectangles meaningfully intersect. Touching edges (zero-area overlap) and overlaps below an internal floating-point epsilon are treated as non-intersecting in order to avoid spurious collisions.- Parameters:
a- the first rectangleb- the second rectangle- Returns:
trueif the rectangles overlap by more than the internal epsilon
-
intersects
Tests whether two ellipses intersect. Uses a circle/circle fast-path when both ellipses are circles; otherwise falls back to a generic shape intersection test.- Parameters:
a- the first ellipseb- the second ellipse- Returns:
trueif the ellipses overlap
-
project
Project a line from a point with a given length and angle, return the point where the line ends.- Parameters:
start- The starting point of the projection.angle- The angle of the projection in degrees.delta- The distance between starting point and end point.- Returns:
- The
Point2Dwhere the projection ends.
-
project
-
rayCastPoints
Performs a ray cast from the given point against the supplied rectangle and returns the visible corner points (i.e. corners that are not occluded by another corner along the ray).- Parameters:
point- the origin of the ray castrectangle- the rectangle to test against- Returns:
- the visible corner points of the rectangle
-
scaleRect
Scales the given rectangle so that its longest side equalsmaxpixels, preserving its aspect ratio.- Parameters:
shape- the rectangle to scalemax- the maximum side length in pixels- Returns:
- the scaled shape
-
scaleWithRatio
Computes aDimension2Dpreserving the aspect ratio ofwidth:heightso that the longest side equalsmax.- Parameters:
width- the original widthheight- the original heightmax- the desired maximum side length- Returns:
- the scaled dimension, or
nullifwidthorheightis0
-
scaleShape
-
shapeIntersects
-
translateShape
Translates the given shape so that the top-left corner of its bounding box matches the supplied location.- Parameters:
shape- the shape to translatenewLocation- the new top-left location of the shape's bounding box- Returns:
- the translated shape
-
normalizeAngle
public static double normalizeAngle(double angle) Normalizes the specified angle to the range between 0-360 degree.- Parameters:
angle- The angle that will be normalized.- Returns:
- The normalized angle.
-