Class Rectangle

java.lang.Object
com.itextpdf.kernel.geom.Rectangle
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
PageSize, TextRectangle

public class Rectangle extends Object implements Cloneable
Class that represent rectangle object.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static float
     
    protected float
     
    protected float
     
    protected float
     
    protected float
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Rectangle(float width, float height)
    Creates new instance of rectangle with (0, 0) as the lower left point.
    Rectangle(float x, float y, float width, float height)
    Creates new instance.
    Creates the copy of given Rectangle
  • Method Summary

    Modifier and Type
    Method
    Description
    applyMargins(float topIndent, float rightIndent, float bottomIndent, float leftIndent, boolean reverse)
    Change the rectangle according the specified margins.
    static Rectangle
    Calculates the bounding box of passed points.
    Creates a "deep copy" of this rectangle, meaning the object returned by this method will be independent of the object being cloned.
    boolean
    Check if this rectangle contains the passed rectangle.
    static Rectangle
    Create the bounding rectangle for the given array of quadpoints.
    static List<Rectangle>
    Create a list of bounding rectangles from an 8 x n array of Quadpoints.
    decreaseHeight(float extra)
    Decreases the height of rectangle by the given value.
    decreaseWidth(float extra)
    Decreases the width of rectangle by the given value.
    boolean
    Compares instance of this rectangle with given deviation equals to 0.0001
    boolean
    equalsWithEpsilon(Rectangle that, float eps)
    Compares instance of this rectangle with given deviation.
    float
    Gets the Y coordinate of the lower edge of the rectangle.
    static Rectangle
    Calculates the common rectangle which includes all the input rectangles.
    float
    Gets the height of rectangle.
    Get the rectangle representation of the intersection between this rectangle and the passed rectangle
    float
    Gets the X coordinate of the left edge of the rectangle.
    static Rectangle
    Gets the rectangle as it looks on the rotated page and returns the rectangle in coordinates relevant to the true page origin.
    float
    Gets the X coordinate of the right edge of the rectangle.
    float
    Gets the Y coordinate of the upper edge of the rectangle.
    float
    Gets the width of rectangle.
    float
    Gets the X coordinate of lower left point.
    float
    Gets the Y coordinate of lower left point.
    increaseHeight(float extra)
    Increases the height of rectangle by the given value.
    increaseWidth(float extra)
    Increases the width of rectangle by the given value.
    boolean
    intersectsLine(float x1, float y1, float x2, float y2)
    Checks if rectangle have common points with line, specified by two points.
    private static boolean
    linesIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
     
    moveDown(float move)
    Decreases the y coordinate.
    moveLeft(float move)
    Decreases the x coordinate.
    moveRight(float move)
    Increases the x coordinate.
    moveUp(float move)
    Increases the y coordinate.
    boolean
    Check if this rectangle and the passed rectangle overlap
    boolean
    overlaps(Rectangle rect, float epsilon)
    Check if this rectangle and the passed rectangle overlap
    setBbox(float llx, float lly, float urx, float ury)
    Sets the rectangle by the coordinates, specifying its lower left and upper right points.
    setHeight(float height)
    Sets the height of rectangle.
    setWidth(float width)
    Sets the width of rectangle.
    setX(float x)
    Sets the X coordinate of lower left point.
    setY(float y)
    Sets the Y coordinate of lower left point.
    Convert rectangle to an array of points
    Gets the string representation of rectangle.

    Methods inherited from class java.lang.Object

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

    • EPS

      static float EPS
    • x

      protected float x
    • y

      protected float y
    • width

      protected float width
    • height

      protected float height
  • Constructor Details

    • Rectangle

      public Rectangle(float x, float y, float width, float height)
      Creates new instance.
      Parameters:
      x - the x coordinate of lower left point
      y - the y coordinate of lower left point
      width - the width value
      height - the height value
    • Rectangle

      public Rectangle(float width, float height)
      Creates new instance of rectangle with (0, 0) as the lower left point.
      Parameters:
      width - the width value
      height - the height value
    • Rectangle

      public Rectangle(Rectangle rect)
      Creates the copy of given Rectangle
      Parameters:
      rect - the copied Rectangle
  • Method Details

    • getCommonRectangle

      public static Rectangle getCommonRectangle(Rectangle... rectangles)
      Calculates the common rectangle which includes all the input rectangles.
      Parameters:
      rectangles - list of input rectangles.
      Returns:
      common rectangle.
    • getRectangleOnRotatedPage

      public static Rectangle getRectangleOnRotatedPage(Rectangle rect, PdfPage page)
      Gets the rectangle as it looks on the rotated page and returns the rectangle in coordinates relevant to the true page origin. This rectangle can be used to add annotations, fields, and other objects to the rotated page.
      Parameters:
      rect - the rectangle as it looks on the rotated page.
      page - the page on which one want to process the rectangle.
      Returns:
      the newly created rectangle with translated coordinates.
    • calculateBBox

      public static Rectangle calculateBBox(List<Point> points)
      Calculates the bounding box of passed points.
      Parameters:
      points - the points which appear inside the area
      Returns:
      the bounding box of passed points.
    • toPointsArray

      public Point[] toPointsArray()
      Convert rectangle to an array of points
      Returns:
      array of four extreme points of rectangle
    • getIntersection

      public Rectangle getIntersection(Rectangle rect)
      Get the rectangle representation of the intersection between this rectangle and the passed rectangle
      Parameters:
      rect - the rectangle to find the intersection with
      Returns:
      the intersection rectangle if the passed rectangles intersects with this rectangle, a rectangle representing a line if the intersection is along an edge or a rectangle representing a point if the intersection is a single point, null otherwise
    • contains

      public boolean contains(Rectangle rect)
      Check if this rectangle contains the passed rectangle. A rectangle will envelop itself, meaning that for any rectangle rect the expression rect.contains(rect) always returns true.
      Parameters:
      rect - a rectangle which is to be checked if it is fully contained inside this rectangle.
      Returns:
      true if this rectangle contains the passed rectangle, false otherwise.
    • overlaps

      public boolean overlaps(Rectangle rect)
      Check if this rectangle and the passed rectangle overlap
      Parameters:
      rect - a rectangle which is to be checked if it overlaps the passed rectangle
      Returns:
      true if there is overlap of some kind
    • overlaps

      public boolean overlaps(Rectangle rect, float epsilon)
      Check if this rectangle and the passed rectangle overlap
      Parameters:
      rect - a rectangle which is to be checked if it overlaps the passed rectangle
      epsilon - if greater than zero, then this is the maximum distance that one rectangle can go to another, but they will not overlap, if less than zero, then this is the minimum required distance between the rectangles so that they do not overlap
      Returns:
      true if there is overlap of some kind
    • setBbox

      public Rectangle setBbox(float llx, float lly, float urx, float ury)
      Sets the rectangle by the coordinates, specifying its lower left and upper right points. May be used in chain.

      Note: this method will normalize coordinates, so the rectangle will have non negative width and height, and its x and y coordinates specified lower left point.
      Parameters:
      llx - the X coordinate of lower left point
      lly - the Y coordinate of lower left point
      urx - the X coordinate of upper right point
      ury - the Y coordinate of upper right point
      Returns:
      this Rectangle instance.
    • getX

      public float getX()
      Gets the X coordinate of lower left point.
      Returns:
      the X coordinate of lower left point.
    • setX

      public Rectangle setX(float x)
      Sets the X coordinate of lower left point. May be used in chain.
      Parameters:
      x - the X coordinate of lower left point to be set.
      Returns:
      this Rectangle instance.
    • getY

      public float getY()
      Gets the Y coordinate of lower left point.
      Returns:
      the Y coordinate of lower left point.
    • setY

      public Rectangle setY(float y)
      Sets the Y coordinate of lower left point. May be used in chain.
      Parameters:
      y - the Y coordinate of lower left point to be set.
      Returns:
      this Rectangle instance.
    • getWidth

      public float getWidth()
      Gets the width of rectangle.
      Returns:
      the width of rectangle.
    • setWidth

      public Rectangle setWidth(float width)
      Sets the width of rectangle. May be used in chain.
      Parameters:
      width - the the width of rectangle to be set.
      Returns:
      this Rectangle instance.
    • getHeight

      public float getHeight()
      Gets the height of rectangle.
      Returns:
      the height of rectangle.
    • setHeight

      public Rectangle setHeight(float height)
      Sets the height of rectangle. May be used in chain.
      Parameters:
      height - the the width of rectangle to be set.
      Returns:
      this Rectangle instance.
    • increaseHeight

      public Rectangle increaseHeight(float extra)
      Increases the height of rectangle by the given value. May be used in chain.
      Parameters:
      extra - the value of the extra height to be added.
      Returns:
      this Rectangle instance.
    • decreaseHeight

      public Rectangle decreaseHeight(float extra)
      Decreases the height of rectangle by the given value. May be used in chain.
      Parameters:
      extra - the value of the extra height to be subtracted.
      Returns:
      this Rectangle instance.
    • increaseWidth

      public Rectangle increaseWidth(float extra)
      Increases the width of rectangle by the given value. May be used in chain.
      Parameters:
      extra - the value of the extra wudth to be added.
      Returns:
      this Rectangle instance.
    • decreaseWidth

      public Rectangle decreaseWidth(float extra)
      Decreases the width of rectangle by the given value. May be used in chain.
      Parameters:
      extra - the value of the extra width to be subtracted.
      Returns:
      this Rectangle instance.
    • getLeft

      public float getLeft()
      Gets the X coordinate of the left edge of the rectangle. Same as: getX().
      Returns:
      the X coordinate of the left edge of the rectangle.
    • getRight

      public float getRight()
      Gets the X coordinate of the right edge of the rectangle. Same as: getX() + getWidth().
      Returns:
      the X coordinate of the right edge of the rectangle.
    • getTop

      public float getTop()
      Gets the Y coordinate of the upper edge of the rectangle. Same as: getY() + getHeight().
      Returns:
      the Y coordinate of the upper edge of the rectangle.
    • getBottom

      public float getBottom()
      Gets the Y coordinate of the lower edge of the rectangle. Same as: getY().
      Returns:
      the Y coordinate of the lower edge of the rectangle.
    • moveDown

      public Rectangle moveDown(float move)
      Decreases the y coordinate.
      Parameters:
      move - the value on which the position will be changed.
      Returns:
      this Rectangle instance.
    • moveUp

      public Rectangle moveUp(float move)
      Increases the y coordinate.
      Parameters:
      move - the value on which the position will be changed.
      Returns:
      this Rectangle instance.
    • moveRight

      public Rectangle moveRight(float move)
      Increases the x coordinate.
      Parameters:
      move - the value on which the position will be changed.
      Returns:
      this Rectangle instance.
    • moveLeft

      public Rectangle moveLeft(float move)
      Decreases the x coordinate.
      Parameters:
      move - the value on which the position will be changed.
      Returns:
      this Rectangle instance.
    • applyMargins

      public Rectangle applyMargins(float topIndent, float rightIndent, float bottomIndent, float leftIndent, boolean reverse)
      Change the rectangle according the specified margins.
      Parameters:
      topIndent - the value on which the top y coordinate will change.
      rightIndent - the value on which the right x coordinate will change.
      bottomIndent - the value on which the bottom y coordinate will change.
      leftIndent - the value on which the left x coordinate will change.
      reverse - if true the rectangle will expand, otherwise it will shrink
      Returns:
      the rectangle with applied margins
    • intersectsLine

      public boolean intersectsLine(float x1, float y1, float x2, float y2)
      Checks if rectangle have common points with line, specified by two points.
      Parameters:
      x1 - the x coordinate of first line's point.
      y1 - the y coordinate of first line's point.
      x2 - the x coordinate of second line's point.
      y2 - the y coordinate of second line's point.
      Returns:
      true if rectangle have common points with line and false otherwise.
    • toString

      public String toString()
      Gets the string representation of rectangle.
      Overrides:
      toString in class Object
      Returns:
      the string representation of rectangle.
    • clone

      public Rectangle clone()
      Creates a "deep copy" of this rectangle, meaning the object returned by this method will be independent of the object being cloned.
      Overrides:
      clone in class Object
      Returns:
      the copied rectangle.
    • equalsWithEpsilon

      public boolean equalsWithEpsilon(Rectangle that)
      Compares instance of this rectangle with given deviation equals to 0.0001
      Parameters:
      that - the Rectangle to compare with.
      Returns:
      true if the difference between corresponding rectangle values is less than deviation and false otherwise.
    • equalsWithEpsilon

      public boolean equalsWithEpsilon(Rectangle that, float eps)
      Compares instance of this rectangle with given deviation.
      Parameters:
      that - the Rectangle to compare with.
      eps - the deviation value.
      Returns:
      true if the difference between corresponding rectangle values is less than deviation and false otherwise.
    • linesIntersect

      private static boolean linesIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
    • createBoundingRectanglesFromQuadPoint

      public static List<Rectangle> createBoundingRectanglesFromQuadPoint(PdfArray quadPoints) throws PdfException
      Create a list of bounding rectangles from an 8 x n array of Quadpoints.
      Parameters:
      quadPoints - 8xn array of numbers representing 4 points
      Returns:
      a list of bounding rectangles for the passed quadpoints
      Throws:
      PdfException - if the passed array's size is not a multiple of 8.
    • createBoundingRectangleFromQuadPoint

      public static Rectangle createBoundingRectangleFromQuadPoint(PdfArray quadPoints) throws PdfException
      Create the bounding rectangle for the given array of quadpoints.
      Parameters:
      quadPoints - an array containing 8 numbers that correspond to 4 points.
      Returns:
      The smallest orthogonal rectangle containing the quadpoints.
      Throws:
      PdfException - if the passed array's size is not a multiple of 8.