Class Rectangle2D

Object
Rectangle2D

public class Rectangle2D extends Object
Utility class for dealing with rectangles in 2D. Note: this implementation is intended as a drop-in replacement for java.awt.geom.Rectangle2D.Double. Methods are implemented only on an as-needed basis.
Author:
Christoph Hauert
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    The bottom segment of the rectangle.
    protected double
    The height of the rectangle.
    The left segment of the rectangle.
    protected Point2D
    The origin of the rectangle (lower left corner).
    The right segment of the rectangle.
    The top segment of the rectangle.
    protected double
    The width of the rectangle.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new empty 2D rectangle with lower left corner at (0,0) and zero width and zero height.
    Rectangle2D(double x, double y, double width, double height)
    Create a new 2D rectangle with lower left corner at (x,y), width width and height height.
    Create a copy of 2D the rectangle r.
  • Method Summary

    Modifier and Type
    Method
    Description
    adjust(double dx, double dy, double dw, double dh)
    Adjust the position of the origin, the lower left corner (x,y), as well as the dimensions, i.e.
    boolean
    contains(double px, double py)
    Check if the rectangle contains the point (px, py).
    boolean
    Check if the rectangle contains the point p.
    double
    Get the height of the rectangle.
    double
    Get the width of the rectangle.
    double
    Get the x-coordinate of the lower left corner of the rectangle.
    double
    Get the y-coordinate of the lower left corner of the rectangle.
    boolean
    Check if the line (or line segment) l intersects the rectangle.
    set(double x, double y, double width, double height)
    Set the coordinates of the lower left corner (x,y) and the dimensions of the rectangle.
    setOrigin(double x, double y)
    Set the coordinates of the origin, lower left corner (x,y), of the rectangle.
    setSize(double width, double height)
    Set the dimensions, the width and height, of the rectangle.
    shift(double dx, double dy)
    Shift/translate the rectangle.
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • top

      public Segment2D top
      The top segment of the rectangle.
    • left

      public Segment2D left
      The left segment of the rectangle.
    • bottom

      public Segment2D bottom
      The bottom segment of the rectangle.
    • origin

      protected Point2D origin
      The origin of the rectangle (lower left corner).
    • width

      protected double width
      The width of the rectangle.
    • height

      protected double height
      The height of the rectangle.
  • Constructor Details

    • Rectangle2D

      public Rectangle2D()
      Create a new empty 2D rectangle with lower left corner at (0,0) and zero width and zero height.
    • Rectangle2D

      public Rectangle2D(Rectangle2D r)
      Create a copy of 2D the rectangle r.
      Parameters:
      r - the 2D rectangle to copy
    • Rectangle2D

      public Rectangle2D(double x, double y, double width, double height)
      Create a new 2D rectangle with lower left corner at (x,y), width width and height height.
      Parameters:
      x - the first coordinate of rectangle (lower left corner)
      y - the second coordinate of rectangle (lower left corner)
      width - the width of rectangle
      height - the height of rectangle
  • Method Details

    • set

      public Rectangle2D set(double x, double y, double width, double height)
      Set the coordinates of the lower left corner (x,y) and the dimensions of the rectangle.
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
      width - the width
      height - the height
      Returns:
      the new rectangle
    • setOrigin

      public Rectangle2D setOrigin(double x, double y)
      Set the coordinates of the origin, lower left corner (x,y), of the rectangle.
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
      Returns:
      the transformed rectangle
    • setSize

      public Rectangle2D setSize(double width, double height)
      Set the dimensions, the width and height, of the rectangle.
      Parameters:
      width - the width
      height - the height
      Returns:
      the transformed rectangle
    • adjust

      public Rectangle2D adjust(double dx, double dy, double dw, double dh)
      Adjust the position of the origin, the lower left corner (x,y), as well as the dimensions, i.e. the width and height, of the rectangle.
      Parameters:
      dx - the horizontal shift of the origin
      dy - the vertical shift of the origin
      dw - the increase in width
      dh - the increase in height
      Returns:
      the transformed rectangle
    • shift

      public Rectangle2D shift(double dx, double dy)
      Shift/translate the rectangle.
      Parameters:
      dx - the horizontal shift of the origin
      dy - the vertical shift of the origin
      Returns:
      the translated rectangle
    • contains

      public boolean contains(Point2D p)
      Check if the rectangle contains the point p.
      Parameters:
      p - the point to check
      Returns:
      true if the point lies inside
    • contains

      public boolean contains(double px, double py)
      Check if the rectangle contains the point (px, py).
      Parameters:
      px - the x-coordinate
      py - the y-coordinate
      Returns:
      true if the point lies inside
    • intersects

      public boolean intersects(Line2D l)
      Check if the line (or line segment) l intersects the rectangle.
      Parameters:
      l - the line to check
      Returns:
      true if the line intersects
    • getX

      public double getX()
      Get the x-coordinate of the lower left corner of the rectangle.

      Method to ensure compatibility with java.awt.geom.Rectangle2D.Double.

      Returns:
      the x-coordinate of lower left corner
    • getY

      public double getY()
      Get the y-coordinate of the lower left corner of the rectangle.

      Method to ensure compatibility with java.awt.geom.Rectangle2D.Double.

      Returns:
      the y-coordinate of lower left corner
    • getWidth

      public double getWidth()
      Get the width of the rectangle.

      Method to ensure compatibility with java.awt.geom.Rectangle2D.Double.

      Returns:
      the width of rectangle
    • getHeight

      public double getHeight()
      Get the height of the rectangle.

      Method to ensure compatibility with java.awt.geom.Rectangle2D.Double.

      Returns:
      the height of rectangle
    • toString

      public String toString()
      Overrides:
      toString in class Object