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 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 where 0 degrees points NORTH.
      Parameters:
      centerX - the x coordinate of the center point
      centerY - the y coordinate of the center point
      targetX - the x coordinate of the target point
      targetY - the y coordinate of the target point
      Returns:
      the clockwise rotation angle in degrees
    • calcRotationAngleInDegrees

      public static double calcRotationAngleInDegrees(Point2D centerPt, Point2D targetPt)

      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

      public static boolean contains(Rectangle2D rectangle, Point2D p)
      Contains.
      Parameters:
      rectangle - the rectangle
      p - 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 point
      p1Y - the y coordinate of the first point
      p2X - the x coordinate of the second point
      p2Y - the y coordinate of the second point
      Returns:
      the Euclidean distance
    • distance

      public static double distance(Point2D p1, Point2D p2)
      Computes the Euclidean distance between two points.
      Parameters:
      p1 - the first point
      p2 - the second point
      Returns:
      the Euclidean distance
    • distance

      public static double distance(Rectangle2D rect, Point2D p)
      Distance.
      Parameters:
      rect - the rect
      p - the p
      Returns:
      the double
    • extrude

      public static Rectangle2D extrude(Rectangle2D rect, double ext)
      Returns a new rectangle with the given extension applied to all sides of the input rectangle.
      Parameters:
      rect - the input rectangle
      ext - the number of units to add to each side
      Returns:
      the extruded rectangle
    • equals

      public static boolean equals(Point2D point1, Point2D point2, double epsilon)
      Determines whether two points are within the given epsilon distance of each other.
      Parameters:
      point1 - the first point
      point2 - the second point
      epsilon - the maximum allowed distance for the points to be considered equal
      Returns:
      true if the distance between the points is less than epsilon
    • getConnectingLines

      public static Line2D[] getConnectingLines(Point2D point, Point2D[] rectPoints)
      Returns the line segments that connect the supplied point to each of the supplied rectangle corner points.
      Parameters:
      point - the starting point
      rectPoints - the corner points to connect to
      Returns:
      the connecting line segments, one per corner point
    • getConstrainingLines

      public static List<Line2D.Double> getConstrainingLines(Area area)
      Extracts the constraining line segments of the given Area by iterating over its path. The returned list contains every PathIterator.SEG_LINETO as 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 where 0 degrees 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 where 0 degrees 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 degrees
      delta - 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 degrees
      delta - the magnitude
      Returns:
      the Y displacement
    • getIntersectionPoint

      public static Point2D getIntersectionPoint(Line2D lineA, Line2D lineB)
      Gets the intersection point.
      Parameters:
      lineA - the line a
      lineB - the line b
      Returns:
      the intersection point
    • getIntersectionPoint

      public static Point2D getIntersectionPoint(Line2D line, Rectangle2D rectangle)
      Intersects.
      Parameters:
      line - the line
      rectangle - the rectangle
      Returns:
      the point2 d
    • getIntersectionPoints

      public static List<Point2D> getIntersectionPoints(Line2D line, Rectangle2D rectangle)
      Gets the intersection points.
      Parameters:
      line - the line
      rectangle - the rectangle
      Returns:
      the intersection points
    • getLines

      public static Line2D[] getLines(Rectangle2D rectangle)
      Gets the lines.
      Parameters:
      rectangle - the rectangle
      Returns:
      the lines
    • getDiagonal

      public static double getDiagonal(Rectangle2D rect)
      Computes the diagonal length of the given rectangle.
      Parameters:
      rect - the rectangle; may be null
      Returns:
      the diagonal length, or 0 if rect is null
    • getCenter

      public static Point2D getCenter(Line2D line)
      Returns the midpoint of the given line.
      Parameters:
      line - the line
      Returns:
      the midpoint
    • getCenter

      public static Point2D getCenter(Point2D p1, Point2D p2)
      Returns the midpoint between two points.
      Parameters:
      p1 - the first point
      p2 - the second point
      Returns:
      the midpoint
    • getCenter

      public static Point2D getCenter(double x1, double y1, double x2, double y2)
      Returns the midpoint of the rectangle defined by the two given coordinate pairs.
      Parameters:
      x1 - the x coordinate of the first point
      y1 - the y coordinate of the first point
      x2 - the x coordinate of the second point
      y2 - the y coordinate of the second point
      Returns:
      the midpoint
    • getCenter

      public static Point2D getCenter(RectangularShape shape)

      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

      public static Ellipse2D getCircle(Point2D center, double radius)
      Creates a circle (as an Ellipse2D) with the given center and radius.
      Parameters:
      center - the center of the circle
      radius - the radius of the circle
      Returns:
      the resulting ellipse
    • getAverageLocation

      public static Point2D getAverageLocation(Collection<Point2D> points)
      Returns the average (centroid) location of the supplied collection of points.
      Parameters:
      points - the points
      Returns:
      the average location, or null if points is empty
    • getAverageLocation

      public static Point2D getAverageLocation(Point2D... points)
      Returns the average (centroid) location of the supplied points.
      Parameters:
      points - the points
      Returns:
      the average location, or null if no points are supplied
    • getPerpendicularIntersection

      public static Point2D getPerpendicularIntersection(Point2D point, Line2D line)
      Gets the perpendicular intersection.
      Parameters:
      point - the point
      line - the line
      Returns:
      the perpendicular intersection
    • getPointOnCircle

      public static Point2D getPointOnCircle(Point2D center, double radius, double angle)
      Returns the point on the circle defined by center and radius at the given angle.
      Parameters:
      center - the center of the circle
      radius - the radius of the circle
      angle - the angle in degrees (clockwise, 0 pointing EAST)
      Returns:
      the point on the circle
    • getPoints

      public static List<Point2D> getPoints(Path2D path)
      Extracts the start points of all segments of the supplied path.
      Parameters:
      path - the path to iterate
      Returns:
      the list of segment start points
    • getPoints

      public static List<Point2D> getPoints(Rectangle2D rectangle)
      Gets the points.
      Parameters:
      rectangle - the rectangle
      Returns:
      the points
    • getPointsBetweenPoints

      public static List<Point2D> getPointsBetweenPoints(Point2D point1, Point2D point2)
      Gets the points between the specified points using the Bresenham algorithm.
      Parameters:
      point1 - the point1
      point2 - the point2
      Returns:
      the points between points
    • intersects

      public static boolean intersects(Rectangle2D a, Rectangle2D b)
      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 rectangle
      b - the second rectangle
      Returns:
      true if the rectangles overlap by more than the internal epsilon
    • intersects

      public static boolean intersects(Ellipse2D a, Ellipse2D b)
      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 ellipse
      b - the second ellipse
      Returns:
      true if the ellipses overlap
    • project

      public static Point2D project(Point2D start, double angle, double delta)
      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 Point2D where the projection ends.
    • project

      public static Point2D project(Point2D start, Point2D end, double scalar)
      Projects a point from end along the vector (end - start) by the given scalar amount.
      Parameters:
      start - the start
      end - the end
      scalar - the scalar
      Returns:
      the point2 d. double
    • rayCastPoints

      public 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).
      Parameters:
      point - the origin of the ray cast
      rectangle - the rectangle to test against
      Returns:
      the visible corner points of the rectangle
    • scaleRect

      public static Shape scaleRect(Rectangle2D shape, int max)
      Scales the given rectangle so that its longest side equals max pixels, preserving its aspect ratio.
      Parameters:
      shape - the rectangle to scale
      max - the maximum side length in pixels
      Returns:
      the scaled shape
    • scaleWithRatio

      public static Dimension2D scaleWithRatio(double width, double height, int max)
      Computes a Dimension2D preserving the aspect ratio of width:height so that the longest side equals max.
      Parameters:
      width - the original width
      height - the original height
      max - the desired maximum side length
      Returns:
      the scaled dimension, or null if width or height is 0
    • scaleShape

      public static Shape scaleShape(Shape shape, double scale)
      Applies a uniform scaling transformation to the given shape.
      Parameters:
      shape - the shape to scale
      scale - the scaling factor applied to both axes
      Returns:
      the scaled shape
    • shapeIntersects

      public static boolean shapeIntersects(Shape shapeA, Shape shapeB)
      Shape intersects. WARNING: USE THIS METHOD WITH CAUTION BECAUSE IT IS A VERY SLOW WAY OF CALCULATING INTERSECTIONS.
      Parameters:
      shapeA - the shape a
      shapeB - the shape b
      Returns:
      true, if successful
    • translateShape

      public static Shape translateShape(Shape shape, Point2D newLocation)
      Translates the given shape so that the top-left corner of its bounding box matches the supplied location.
      Parameters:
      shape - the shape to translate
      newLocation - 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.