File-By-File
Rectangle

Rectangle

A rectangle! It has four vertices and four lines. This class represents a rectangle defined by four PointXY vertices. It provides methods for geometric operations such as rotation, shifting, scaling, and checking for point containment or collisions.

Key Concepts

  • Vertices: A rectangle is defined by four PointXY objects (a, b, c, d) that represent its corners.
  • Lines: The sides and diagonals of the rectangle are represented by Line objects.
  • Immutability: Like other geometric objects in Pathfinder, Rectangle objects are immutable. Operations that modify the rectangle (e.g., rotate, shift) return new Rectangle instances.
  • Construction: Rectangles can be constructed from four points or two opposite points. When constructing from four points, specific adjacency and parallelism rules must be followed to ensure a valid rectangle.

Methods

Rectangle(PointXY a, PointXY b, PointXY c, PointXY d)

Constructs a new Rectangle from four PointXY vertices. The points must be provided in a specific order (e.g., counter-clockwise or clockwise around the perimeter) such that ab is parallel to cd, and bc is parallel to da. Throws an IllegalArgumentException if points are duplicated or parallelism conditions are not met.

Rectangle(PointXY a, PointXY c)

Constructs a new Rectangle from two opposite PointXY vertices (e.g., bottom-left and top-right). This constructor automatically determines the other two vertices.

Rectangle(double minX, double minY, double maxX, double maxY)

Constructs a new Rectangle from minimum and maximum X and Y values, defining an axis-aligned bounding box.

newRotatedRectangle(double minX, double minY, double maxX, double maxY, Angle rotationAngle)

Static factory method. Creates a new Rectangle from bounding box coordinates and then rotates it by the rotationAngle around its center.

getClosestPoint(PointXY reference)

Returns the point on the perimeter of the rectangle that is closest to the given reference point.

isPointInShape(PointXY reference)

Checks if a given reference point is inside or on the boundary of the rectangle. This method uses a combination of checks, including proximity to vertices and line segments, and a ray-casting algorithm for more complex cases.

doesCollideWith(Shape<?> shape)

Determines if this Rectangle collides with another Shape. It checks if the closest point on this rectangle to the other shape's center is inside the other shape.

getCenter()

Returns the PointXY representing the center of the rectangle.

rotate(Angle rotation)

Rotates the rectangle by the given rotation angle around its own center. Returns a new Rectangle instance.

rotate(Angle rotation, PointXY centerOfRotation)

Rotates the rectangle by the given rotation angle around a specified centerOfRotation. Returns a new Rectangle instance.

shift(double shiftX, double shiftY)

Shifts (translates) the rectangle by the given shiftX and shiftY offsets. Returns a new Rectangle instance.

moveTo(PointXY newCenter)

Moves the rectangle so that its center is at the newCenter PointXY. Returns a new Rectangle instance.

scale(double scale)

Scales the rectangle by a scale factor relative to its center. Returns a new Rectangle instance.

growBy(double growth)

Grows or shrinks the rectangle by adding growth to its dimensions, effectively expanding or contracting it from its center. Returns a new Rectangle instance.

getMinimumX()

Returns the minimum X-coordinate among all vertices of the rectangle.

getMinimumY()

Returns the minimum Y-coordinate among all vertices of the rectangle.

getMaximumX()

Returns the maximum X-coordinate among all vertices of the rectangle.

getMaximumY()

Returns the maximum Y-coordinate among all vertices of the rectangle.

getSizeX()

Returns the width of the rectangle (difference between maximum and minimum X-coordinates).

getSizeY()

Returns the height of the rectangle (difference between maximum and minimum Y-coordinates).