Package bagel.util
Class Rectangle
java.lang.Object
bagel.util.Rectangle
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
ConstructorsConstructorDescriptionRectangle(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.Creates a rectangle with a specified top-left position and a given width and height.Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptiondoublebottom()Returns the y-coordinate of the bottom side of the rectangle.Returns aPointrepresenting the bottom-left of the rectangle.Returns aPointrepresenting the bottom-right of the rectangle.centre()Returns aPointrepresenting the centre of the rectangle.booleanintersectedAt(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.booleanintersects(Point point) Returns whether the given point lies inside the rectangle (including its edges).booleanintersects(Rectangle rectangle) Returns whether any part of the given rectangle overlaps with this rectangle.doubleleft()Returns the x-coordinate of the left side of the rectangle.voidMoves the rectangle so that its top-left is at the specified point.doubleright()Returns the x-coordinate of the right side of the rectangle.doubletop()Returns the y-coordinate of the top side of the rectangle.topLeft()Returns aPointrepresenting the top-left of the rectangle.topRight()Returns aPointrepresenting the top-right of the rectangle.toString()
-
Constructor Details
-
Rectangle
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
Creates a rectangle with a specified top-left position and a given width and height.
-
-
Method Details
-
topLeft
Returns aPointrepresenting the top-left of the rectangle. -
topRight
Returns aPointrepresenting the top-right of the rectangle. -
bottomLeft
Returns aPointrepresenting the bottom-left of the rectangle. -
bottomRight
Returns aPointrepresenting the bottom-right of the rectangle. -
centre
Returns aPointrepresenting 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
Moves the rectangle so that its top-left is at the specified point. -
intersects
Returns whether the given point lies inside the rectangle (including its edges). -
intersects
Returns whether any part of the given rectangle overlaps with this rectangle. -
intersectedAt
Estimates which side of the rectangle the point intersected at, using the previous position to calculate a more accurate estimation.- See Also:
-
intersectedAt
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
-
equals
-