Package toxi.geom

Class Rect

java.lang.Object
toxi.geom.Rect
All Implemented Interfaces:
Shape2D
Direct Known Subclasses:
PointQuadtree

public class Rect extends Object implements Shape2D
  • Field Details

    • x

      public float x
    • y

      public float y
    • width

      public float width
    • height

      public float height
  • Constructor Details

    • Rect

      public Rect()
      Constructs an empty rectangle at point 0,0 with no dimensions.
    • Rect

      public Rect(float x, float y, float width, float height)
      Constructs a new rectangle using a point and dimensions
      Parameters:
      x - x of top left
      y - y of top left
      width -
      height -
    • Rect

      public Rect(ReadonlyVec2D p1, ReadonlyVec2D p2)
      Constructs a new rectangle defined by the two points given.
      Parameters:
      p1 -
      p2 -
  • Method Details

    • fromCenterExtent

      public static final Rect fromCenterExtent(ReadonlyVec2D center, Vec2D extent)
      Factory method, constructs a new rectangle from a center point and extent vector.
      Parameters:
      center -
      extent -
      Returns:
      new rect
    • getBoundingRect

      public static final Rect getBoundingRect(List<? extends Vec2D> points)
      Factory method, computes & returns the bounding rect for the given list of points.
      Parameters:
      points -
      Returns:
      bounding rect
      Since:
      0021
    • containsPoint

      public boolean containsPoint(ReadonlyVec2D p)
      Checks if the given point is within the rectangle's bounds.
      Specified by:
      containsPoint in interface Shape2D
      Parameters:
      p - point to check
      Returns:
      true, if point is contained
    • copy

      public Rect copy()
      Creates a copy of this rectangle
      Returns:
      new instance
    • equals

      public boolean equals(Object o)
      Returns true if the Object o is of type Rect and all of the data members of o are equal to the corresponding data members in this rectangle.
      Overrides:
      equals in class Object
      Parameters:
      o - the Object with which the comparison is made
      Returns:
      true or false
    • getArea

      public final float getArea()
      Description copied from interface: Shape2D
      Computes the area of the shape.
      Specified by:
      getArea in interface Shape2D
      Returns:
      area
    • getAspect

      public final float getAspect()
      Computes the aspect ratio of the rect as width over height.
      Returns:
      aspect ratio
    • getBottom

      public float getBottom()
    • getBottomLeft

      public Vec2D getBottomLeft()
    • getBottomRight

      public final Vec2D getBottomRight()
    • getBoundingCircle

      public Circle getBoundingCircle()
      Description copied from interface: Shape2D
      Computes the bounding circle of the shape.
      Specified by:
      getBoundingCircle in interface Shape2D
      Returns:
      circle
    • getBounds

      public Rect getBounds()
      Only provided because the Rect class implements Shape2D. The bounding rect of a rectangle is itself.
      Specified by:
      getBounds in interface Shape2D
      Returns:
      itself
      See Also:
    • getCentroid

      public final Vec2D getCentroid()
      Returns the centroid of the rectangle.
      Returns:
      centroid vector
    • getCircumference

      public final float getCircumference()
      Description copied from interface: Shape2D
      Computes the shape's circumference.
      Specified by:
      getCircumference in interface Shape2D
      Returns:
      circumference
    • getDimensions

      public final Vec2D getDimensions()
      Returns a vector containing the width and height of the rectangle.
      Returns:
      dimension vector
    • getEdge

      public Line2D getEdge(int id)
      Returns one of the rectangles edges as Line2D. The edge IDs are:
      • 0 - top
      • 1 - right
      • 2 - bottom
      • 3 - left
      Parameters:
      id - edge ID
      Returns:
      edge as Line2D
    • getEdges

      public List<Line2D> getEdges()
      Description copied from interface: Shape2D
      Returns a list of the shape's perimeter edges.
      Specified by:
      getEdges in interface Shape2D
      Returns:
      list of Line2D elements
    • getLeft

      public float getLeft()
    • getMappedPointInRect

      public Vec2D getMappedPointInRect(Vec2D p)
      Computes the normalized position of the given point within this rectangle, so that a point at the top-left corner becomes {0,0} and bottom-right {1,1}. The original point is not modified. Together with getUnmappedPointInRect(Vec2D) this function can be used to map a point from one rectangle to another.
      Parameters:
      p - point to be mapped
      Returns:
      mapped Vec2D
    • getRandomPoint

      public Vec2D getRandomPoint()
      Creates a random point within the rectangle.
      Specified by:
      getRandomPoint in interface Shape2D
      Returns:
      Vec2D
    • getRight

      public float getRight()
    • getTop

      public float getTop()
    • getTopLeft

      public final Vec2D getTopLeft()
    • getTopRight

      public Vec2D getTopRight()
    • getUnmappedPointInRect

      public Vec2D getUnmappedPointInRect(Vec2D p)
      Inverse operation of getMappedPointInRect(Vec2D). Given a normalized point it computes the position within this rectangle, so that a point at {0,0} becomes the top-left corner and {1,1} bottom-right. The original point is not modified. Together with getUnmappedPointInRect(Vec2D) this function can be used to map a point from one rectangle to another.
      Parameters:
      p - point to be mapped
      Returns:
      mapped Vec2D
    • growToContainPoint

      public Rect growToContainPoint(ReadonlyVec2D p)
    • hashCode

      public int hashCode()
      Returns a hash code value based on the data values in this object. Two different Rect objects with identical data values (i.e., Rect.equals returns true) will return the same hash code value. Two objects with different data members may return the same hash value, although this is not likely.
      Overrides:
      hashCode in class Object
      Returns:
      the integer hash code value
    • intersectionRectWith

      public final Rect intersectionRectWith(Rect r)
      Creates a new rectangle by forming the intersection of this rectangle and the given other rect. The resulting bounds will be the rectangle of the overlay area or null if the rects do not intersect.
      Parameters:
      r - intersection partner rect
      Returns:
      new Rect or null
    • intersectsCircle

      public boolean intersectsCircle(Vec2D c, float r)
    • intersectsRay

      public ReadonlyVec2D intersectsRay(Ray2D ray, float minDist, float maxDist)
      Checks if the rectangle intersects with the given ray and if so computes the first intersection point. The method takes a min/max distance interval along the ray in which the intersection must occur.
      Parameters:
      ray - intersection ray
      minDist - minimum distance
      maxDist - max distance
      Returns:
      intersection point or null if no intersection in the given interval
    • intersectsRect

      public boolean intersectsRect(Rect r)
      Checks if this rectangle intersects/overlaps the given one.
      Parameters:
      r - another rect
      Returns:
      true, if intersecting
    • scale

      public Rect scale(float s)
    • set

      public final Rect set(float x, float y, float w, float h)
      Sets new bounds for this rectangle.
      Parameters:
      x - x of top left
      y - y of top right
      w - width
      h - height
      Returns:
      itself
    • set

      public final Rect set(Rect r)
    • setDimension

      public final Rect setDimension(Vec2D dim)
    • setPosition

      public final Rect setPosition(Vec2D pos)
    • toPolygon2D

      public Polygon2D toPolygon2D()
      Creates a Polygon2D instance of the rect.
      Specified by:
      toPolygon2D in interface Shape2D
      Returns:
      rect as polygon
    • toPolygon2D

      public Polygon2D toPolygon2D(float radius, int res)
      Turns this rectangle into a rounded rectangle shaped Polygon2D instance with the given corner radius. The number of corner vertices to be used, can be specified as well.
      Parameters:
      radius - corner radius
      res - number of vertices per corner
      Returns:
      rounded rect as polygon
    • toString

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

      public Rect translate(float dx, float dy)
    • translate

      public Rect translate(ReadonlyVec2D offset)
    • union

      @Deprecated public final Rect union(Rect r)
      Deprecated.
      use unionRectWith(Rect) instead. Also note, that unionRectWith(Rect) does NOT modify the original Rect anymore, but produces a new Rect instance.
      Parameters:
      r -
      Returns:
      new Rect
    • unionRectWith

      public final Rect unionRectWith(Rect r)
      Creates a new rectangle by forming an union of this rectangle and the given other Rect. The resulting bounds will be inclusive of both.
      Parameters:
      r -
      Returns:
      new Rect