Package bagel.util

Class Rectangle

java.lang.Object
bagel.util.Rectangle

public class Rectangle extends Object
Represents a rectangle, with its top-left at a point and a set size. Contains methods to easily calculate collisions with other types of geometry.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Rectangle(double x, double y, double width, double height)
    Creates a rectangle at a particular (x, y) top-left position with a given width and height.
    Rectangle(Point topLeft, double width, double height)
    Creates a rectangle with a specified top-left position and a given width and height.
    Copy constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Returns the y-coordinate of the bottom side of the rectangle.
    Returns a Point representing the bottom-left of the rectangle.
    Returns a Point representing the bottom-right of the rectangle.
    Returns a Point representing the centre of the rectangle.
    boolean
    equals(Object other)
     
    intersectedAt(Point point, Point lastPoint)
    Estimates which side of the rectangle the point intersected at, using the previous position to calculate a more accurate estimation.
    intersectedAt(Point point, Vector2 velocity)
    Estimates which side of the rectangle the point intersected at, using the velocity of the point to inform the result.
    boolean
    Returns whether the given point lies inside the rectangle (including its edges).
    boolean
    intersects(Rectangle rectangle)
    Returns whether any part of the given rectangle overlaps with this rectangle.
    double
    Returns the x-coordinate of the left side of the rectangle.
    void
    moveTo(Point topLeft)
    Moves the rectangle so that its top-left is at the specified point.
    double
    Returns the x-coordinate of the right side of the rectangle.
    double
    top()
    Returns the y-coordinate of the top side of the rectangle.
    Returns a Point representing the top-left of the rectangle.
    Returns a Point representing the top-right of the rectangle.
     

    Methods inherited from class java.lang.Object

    getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Rectangle

      public Rectangle(Rectangle rect)
      Copy constructor.
    • Rectangle

      public Rectangle(double x, double y, double width, double height)
      Creates a rectangle at a particular (x, y) top-left position with a given width and height.
    • Rectangle

      public Rectangle(Point topLeft, double width, double height)
      Creates a rectangle with a specified top-left position and a given width and height.
  • Method Details

    • topLeft

      public Point topLeft()
      Returns a Point representing the top-left of the rectangle.
    • topRight

      public Point topRight()
      Returns a Point representing the top-right of the rectangle.
    • bottomLeft

      public Point bottomLeft()
      Returns a Point representing the bottom-left of the rectangle.
    • bottomRight

      public Point bottomRight()
      Returns a Point representing the bottom-right of the rectangle.
    • centre

      public Point centre()
      Returns a Point representing the centre of the rectangle.
    • right

      public double right()
      Returns the x-coordinate of the right side of the rectangle.
    • left

      public double left()
      Returns the x-coordinate of the left side of the rectangle.
    • top

      public double top()
      Returns the y-coordinate of the top side of the rectangle.
    • bottom

      public double bottom()
      Returns the y-coordinate of the bottom side of the rectangle.
    • moveTo

      public void moveTo(Point topLeft)
      Moves the rectangle so that its top-left is at the specified point.
    • intersects

      public boolean intersects(Point point)
      Returns whether the given point lies inside the rectangle (including its edges).
    • intersects

      public boolean intersects(Rectangle rectangle)
      Returns whether any part of the given rectangle overlaps with this rectangle.
    • intersectedAt

      public Side intersectedAt(Point point, Point lastPoint)
      Estimates which side of the rectangle the point intersected at, using the previous position to calculate a more accurate estimation.
      See Also:
    • intersectedAt

      public Side intersectedAt(Point point, Vector2 velocity)
      Estimates which side of the rectangle the point intersected at, using the velocity of the point to inform the result. Algorithm: 1. Find the mid-point between `point` and its previous location (via velocity) 2. If either point intersects: (to handle the case where the object "skips" through the rectangle): 3. Find the collision normal (the direction the point struck the rectangle) 4. Return the basis vector with maximal dot product 5. Else return NONE
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object