Class Geometry2D
- java.lang.Object
-
- de.xima.fc.common.geometry.Geometry2D
-
public final class Geometry2D extends Object
Utility class for geometrical operations in a 2D space.- Since:
- 8.4.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
areRectanglesOverlapping(Point2DDouble pos1, Rectangular2DDouble size1, Point2DDouble pos2, Rectangular2DDouble size2)
Checks if two 2D rectangles have any overlap.static boolean
areRectanglesOverlappingOrTouching(Point2DDouble pos1, Rectangular2DDouble size1, Point2DDouble pos2, Rectangular2DDouble size2)
Checks if two 2D rectangles have any overlap.static OneOfN.OneOf3<ECardinalDirection2D,EPrimaryIntercardinalDirection2D,Unit>
classifyRectangleCardinalPosition(Point2DDouble pos1, Rectangular2DDouble size1, Point2DDouble pos2, Rectangular2DDouble size2)
Classifies the relative position of two non-overlapping rectangles r1 and r2 in a 2D space in regard to their cardinal relationship.static double
edgeCoordinate(Point2DDouble position, Rectangular2DDouble boundingBox, ECardinalDirection2D direction)
Finds the coordinate of the edge in the specified direction, assuming a rectangular bounding with the given dimensions box positioned at the given position.static Point2DDouble
interpolateLinearly(Point2DDouble p1, Point2DDouble p2, double ratio)
Linearly interpolates between two points in 2D space and returns a point at the given ratio between the two points.static double
maxCoordinateInDirection(EAxisPolarity direction, double x, double y)
Returns the maximal coordinate in the specified axis direction for the two given coordinates in 2D space.static double
maxCoordinateInDirection(EAxisPolarity direction, double a, double b, double c)
Returns the maximal coordinate in the specified axis direction for the 3 given coordinates in 2D space.static double
maxCoordinateInDirection(EAxisPolarity direction, double a, double b, double c, double d)
Returns the maximal coordinate in the specified axis direction for the 4 given coordinates in 2D space.static double
maxCoordinateInDirection(ECardinalDirection2D direction, Point2DDouble p1, Point2DDouble p2)
Returns the maximal coordinate in the specified direction for the two given points in 2D space.static double
offsetPointLine(double px, double py, double lineX1, double lineY1, double lineX2, double lineY2)
Computes the offset of a point from a line segment defined by two points in 2D space.static double
offsetPointLine(Point2D<?> point, Point2D<?> lineStart, Point2D<?> lineEnd)
Computes the offset of a point from a line segment defined by two points in 2D space.
-
-
-
Method Detail
-
areRectanglesOverlapping
public static boolean areRectanglesOverlapping(Point2DDouble pos1, Rectangular2DDouble size1, Point2DDouble pos2, Rectangular2DDouble size2)
Checks if two 2D rectangles have any overlap. Rectangles that are only touching at the edges or corners are not considered overlapping.- Parameters:
pos1
- The position of the first rectangle, i.e. its top-left corner.size1
- The size of the first rectangle.pos2
- The position of the second rectangle, i.e. its top-left corner.size2
- The size of the second rectangle.- Returns:
true
if the rectangles overlap,false
otherwise.
-
areRectanglesOverlappingOrTouching
public static boolean areRectanglesOverlappingOrTouching(Point2DDouble pos1, Rectangular2DDouble size1, Point2DDouble pos2, Rectangular2DDouble size2)
Checks if two 2D rectangles have any overlap. Rectangles that are only touching at the edges or corners are also considered overlapping.- Parameters:
pos1
- The position of the first rectangle, i.e. its top-left corner.size1
- The size of the first rectangle.pos2
- The position of the second rectangle, i.e. its top-left corner.size2
- The size of the second rectangle.- Returns:
true
if the rectangles overlap or touch each other,false
otherwise.
-
classifyRectangleCardinalPosition
public static OneOfN.OneOf3<ECardinalDirection2D,EPrimaryIntercardinalDirection2D,Unit> classifyRectangleCardinalPosition(Point2DDouble pos1, Rectangular2DDouble size1, Point2DDouble pos2, Rectangular2DDouble size2)
Classifies the relative position of two non-overlapping rectangles r1 and r2 in a 2D space in regard to their cardinal relationship. The possible classifications are:NORTH
r2 is above r1NORTH_EAST
r2 is above and to the right of r1EAST
r2 is to the right of r1SOUTH_EAST
r2 is below and to the right of r1SOUTH
r2 is below r1SOUTH_WEST
r2 is below and to the left of r1WEST
r2 is to the left of r1NORTH_WEST
r2 is above and to the left of r1UNIT
r2 isoverlapping
with r1
Case 1 - r2 above r1
r2 is above r1, i.e. the bottom edge of r2 is above the top edge of r1. There are different subcases that classify into 3 categories:
NORTH NORTH NORTH NORTH_EAST NORTH_WEST ------ | ---------- | ---------- | ------ | ------ | r2 | | | r2 | | | r2 | | | r2 | | | r2 | ------ | ---------- | ---------- | ------ | ------ | | | | ---------- | ------ | ---------- | ------ | ------ | r1 | | | r1 | | | r1 | | | r1 | | | r1 | ---------- | ------ | ---------- | ------ | ------
- r2 is northeast of r1 if the left edge of r2 is to the right of the right edge of r1.
- r2 is northwest of r1 if the right edge of r2 is to the left of the left edge of r1.
- Otherwise, r2 is north of r1.
Case 2 - r2 below r1
r2 is below r1, i.e. the top edge of r2 is below the bottom edge of r1. There are different subcases that classify into 3 categories:
SOUTH SOUTH SOUTH SOUTH_WEST SOUTH_EAST ------ | ---------- | ---------- | ------ | ------ | r1 | | | r1 | | | r1 | | | r1 | | | r1 | ------ | ---------- | ---------- | ------ | ------ | | | | ---------- | ------ | ---------- | ------ | ------ | r2 | | | r2 | | | r2 | | | r2 | | | r2 | ---------- | ------ | ---------- | ------ | ------
- r2 is southeast of r1 if the left edge of r2 is to the right of the right edge of r1.
- r2 is southwest of r1 if the right edge of r2 is to the left of the left edge of r1.
- Otherwise, r2 is south of r1.
Case 3 - r2 right of r1
r2 is to the right of r1, i.e. the left edge of r2 is to the right of the right edge of r1. There are different subcases that classify into 3 categories:
EAST EAST EAST SOUTH_EAST NORTH_EAST ------ | ------ | ------ | ------ | ------ ------ | | | | | ------ | | | | | | | | | | r1 | | r2 | | | r1 | | r2 | | | r1 | | | r1 | | | r2 | ------ | | | | | ------ | | | ------ | | | | | | ------ | ------ | ------ | | | ------ | ------ | | | r2 | | ------ | ------ | | | | | | | | | | | | ------ | | r2 | | | r1 | | | | | | | | | | | | ------ | ------
- r2 is southeast of r1 if the top edge of r2 is below the bottom edge of r1.
- r2 is northeast of r1 if the bottom edge of r2 is above the top edge of r1.
- Otherwise, r2 is east of r1.
Case 4 - r2 left of r1
r2 is to the right of r1, i.e. the right edge of r2 is to the left of the left edge of r1. There are different subcases that classify into 3 categories:
WEST WEST WEST NORTH_WEST SOUTH_WEST ------ | ------ | ------ | ------ | ------ ------ | | | | | ------ | | | | | | | | | | r2 | | r1 | | | r2 | | r1 | | | r2 | | | r2 | | | r1 | ------ | | | | | ------ | | | ------ | | | | | | ------ | ------ | ------ | | | ------ | ------ | | | r1 | | ------ | ------ | | | | | | | | | | | | ------ | | r1 | | | r2 | | | | | | | | | | | | ------ | ------
- r2 is southeast of r1 if the top edge of r2 is below the bottom edge of r1.
- r2 is northeast of r1 if the bottom edge of r2 is above the top edge of r1.
- Otherwise, r2 is east of r1.
- Parameters:
pos1
- The position of the first rectangle r1, i.e. its top-left corner.size1
- The size of the first rectangle r1.pos2
- The position of the second rectangle r2, i.e. its top-left corner.size2
- The size of the second rectangle r2.- Returns:
- The relative position of the first rectangle to the second rectangle, never null.
-
edgeCoordinate
public static double edgeCoordinate(Point2DDouble position, Rectangular2DDouble boundingBox, ECardinalDirection2D direction)
Finds the coordinate of the edge in the specified direction, assuming a rectangular bounding with the given dimensions box positioned at the given position.position | | v NORTH +-----------------+ ^ | | | WEST | | EAST | height | | | +-----------------+ v SOUTH <-----------------> width
- Parameters:
position
- the position of the rectangle, i.e. its top-left corner.boundingBox
- The dimensions of the rectangle.direction
- The direction of the edge.- Returns:
- The coordinate of the edge in the specified direction.
-
interpolateLinearly
public static Point2DDouble interpolateLinearly(Point2DDouble p1, Point2DDouble p2, double ratio)
Linearly interpolates between two points in 2D space and returns a point at the given ratio between the two points.interpolateLinearly(p1, p2, ratio) = p1 + ratio * (p2 - p1)
- Parameters:
p1
- The first point in 2D space. null is treated as the origin (0, 0).p2
- The second point in 2D space. null is treated as the origin (0, 0).ratio
- The ratio between the two points, where 0.0 is p1 and 1.0 is p2.- Returns:
- A new point between p1 and p2 at the given ratio.
-
maxCoordinateInDirection
public static double maxCoordinateInDirection(ECardinalDirection2D direction, Point2DDouble p1, Point2DDouble p2)
Returns the maximal coordinate in the specified direction for the two given points in 2D space.- NORTH: returns the minimum y-coordinate of the two points
- EAST: returns the maximum x-coordinate of the two points
- SOUTH: returns the maximum y-coordinate of the two points
- WEST: returns the minimum x-coordinate of the two points
- Parameters:
direction
- The direction in which to find the maximum coordinatep1
- The first point in 2D spacep2
- The second point in 2D space- Returns:
- The maximum coordinate in the specified direction.
-
maxCoordinateInDirection
public static double maxCoordinateInDirection(EAxisPolarity direction, double x, double y)
Returns the maximal coordinate in the specified axis direction for the two given coordinates in 2D space.- POSITIVE: returns the maximum of the two coordinates
- NEGATIVE: returns the minimum of the two coordinates
- Parameters:
direction
- The direction in which to find the maximum coordinatex
- The first coordinatey
- The second coordinate- Returns:
- The maximum coordinate in the specified direction.
-
maxCoordinateInDirection
public static double maxCoordinateInDirection(EAxisPolarity direction, double a, double b, double c)
Returns the maximal coordinate in the specified axis direction for the 3 given coordinates in 2D space.- POSITIVE: returns the maximum of the two coordinates
- NEGATIVE: returns the minimum of the two coordinates
- Parameters:
direction
- The direction in which to find the maximum coordinatea
- The first coordinateb
- The second coordinatec
- The third coordinate- Returns:
- The maximum coordinate in the specified direction.
-
maxCoordinateInDirection
public static double maxCoordinateInDirection(EAxisPolarity direction, double a, double b, double c, double d)
Returns the maximal coordinate in the specified axis direction for the 4 given coordinates in 2D space.- POSITIVE: returns the maximum of the two coordinates
- NEGATIVE: returns the minimum of the two coordinates
- Parameters:
direction
- The direction in which to find the maximum coordinatea
- The first coordinateb
- The second coordinatec
- The third coordinated
- The fourth coordinate- Returns:
- The maximum coordinate in the specified direction.
-
offsetPointLine
public static double offsetPointLine(double px, double py, double lineX1, double lineY1, double lineX2, double lineY2)
Computes the offset of a point from a line segment defined by two points in 2D space. The offset is the oriented distance, its sign indicates whether the point is to the left or right of the line segment. If the offset is positive, the point is to the left of the line segment (when looking from the start point to the end point). If the offset is negative, the point is to the right of the line segment.If the line start and end point are the same, returns the positive distance between the point and the center of the line's start and end point.
- Parameters:
px
- The x-coordinate of the point.py
- The y-coordinate of the point.lineX1
- The x-coordinate of the first point of the line segment.lineY1
- The y-coordinate of the first point of the line segment.lineX2
- The x-coordinate of the second point of the line segment.lineY2
- The y-coordinate of the second point of the line segment.- Returns:
- The offset of the point from the line segment.
-
offsetPointLine
public static double offsetPointLine(Point2D<?> point, Point2D<?> lineStart, Point2D<?> lineEnd)
Computes the offset of a point from a line segment defined by two points in 2D space. The offset is the oriented distance, its sign indicates whether the point is to the left or right of the line segment. If the line start and end point are the same, returns the distance between the point and the center of the line's start and end point.- Parameters:
point
- The point to compute the offset for.lineStart
- The start point of the line segment.lineEnd
- The end point of the line segment.- Returns:
- The offset of the point from the line segment.
-
-