org.openimaj.math.geometry.shape

Class Rectangle

• Field Summary

Fields
Modifier and Type Field and Description
`float` `height`
The height of the rectangle
`float` `width`
The width of the rectangle
`float` `x`
The x-coordinate of the top-left of the rectangle
`float` `y`
The y-coordinate of the top-left of the rectangle
• Constructor Summary

Constructors
Constructor and Description
`Rectangle()`
Construct a unit rectangle
```Rectangle(float x, float y, float width, float height)```
Construct a Rectangle with the given parameters.
```Rectangle(Point2d topLeft, Point2d bottomRight)```
Construct a Rectangle with the given parameters.
`Rectangle(Rectangle r)`
Construct a Rectangle by copying from another rectangle.
• Method Summary

All Methods
Modifier and Type Method and Description
`String` `asciiHeader()`
`Polygon` `asPolygon()`
Convert the shape to a polygon representation
`byte[]` `binaryHeader()`
`double` `calculateArea()`
Calculate the area of the shape
`Point2d` `calculateCentroid()`
Calculate the centroid of the shape
`double` `calculatePerimeter()`
Calculate the perimeter of the shape
`Rectangle` `calculateRegularBoundingBox()`
Compute the regular (oriented to the axes) bounding box of the shape.
`Rectangle` `clone()`
`boolean` `equals(Object obj)`
`Point2d` `getBottomRight()`
`double` `getHeight()`
`Point2d` `getTopLeft()`
`double` `getWidth()`
`int` `hashCode()`
`double` `intersectionArea(Shape that)`
Calls `Polygon.intersectionArea(Shape, int)` with 1 step per pixel dimension.
`double` ```intersectionArea(Shape that, int nStepsPerDimension)```
Return an estimate for the area of the intersection of this polygon and another polygon.
`boolean` `isConvex()`
Test if the shape is convex.
`boolean` `isInside(Point2d point)`
Test whether the point p is inside the shape.
`boolean` `isInside(Rectangle rect)`
Test if the given rectangle is inside this one.
`boolean` `isOverlapping(Rectangle other)`
Test if rectangles overlap.
`double` `maxX()`
`double` `maxY()`
`RotatedRectangle` `minimumBoundingRectangle()`
Compute the minimum size rotated bounding rectangle that contains this shape.
`double` `minX()`
`double` `minY()`
`Rectangle` `overlapping(Rectangle other)`
Get the overlapping rectangle between this rectangle and another.
`double` `percentageOverlap(Rectangle other)`
Compute the percentage by which the given rectangle overlaps this one.
`void` `readASCII(Scanner in)`
`void` `readBinary(DataInput in)`
`RotatedRectangle` ```rotate(Point2d p, double angle)```
Rotate the `Rectangle` about the given pivot with the given angle (in radians)
`void` `scale(float sc)`
Scale the shape by the given amount about (0,0).
`void` ```scale(Point2d centre, float sc)```
Scale the shape by the given amount about the given point.
`void` `scaleCentroid(float sc)`
Scale the shape about its centroid.
`void` ```setBounds(float x, float y, float width, float height)```
Set the position and size of this rectangle
`String` `toString()`
`Shape` `transform(Jama.Matrix transform)`
Apply a 3x3 transform matrix to a copy of the `GeometricObject2d` and return it
`void` ```translate(float x, float y)```
Translate the shapes position
`Rectangle` `union(Rectangle other)`
Find the rectangle that just contains this rectangle and another rectangle.
`void` `writeASCII(PrintWriter out)`
Write the content of this as ascii to out.
`void` `writeBinary(DataOutput out)`
Write the content of this as binary to out.
• Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`
• Field Detail

• x

`public float x`
The x-coordinate of the top-left of the rectangle
• y

`public float y`
The y-coordinate of the top-left of the rectangle
• width

`public float width`
The width of the rectangle
• height

`public float height`
The height of the rectangle
• Constructor Detail

• Rectangle

`public Rectangle()`
Construct a unit rectangle
• Rectangle

```public Rectangle(float x,
float y,
float width,
float height)```
Construct a Rectangle with the given parameters.
Parameters:
`x` - x-coordinate of top-left
`y` - y-coordinate of top-left
`width` - width
`height` - height
• Rectangle

`public Rectangle(Rectangle r)`
Construct a Rectangle by copying from another rectangle.
Parameters:
`r` - rectangle to copy from
• Rectangle

```public Rectangle(Point2d topLeft,
Point2d bottomRight)```
Construct a Rectangle with the given parameters.
Parameters:
`topLeft` - top-left corner
`bottomRight` - bottom-right corner
• Method Detail

• isInside

`public boolean isInside(Point2d point)`
Description copied from interface: `Shape`
Test whether the point p is inside the shape.
Specified by:
`isInside` in interface `Shape`
Parameters:
`point` - the point
Returns:
true if the point is inside; false otherwise
• calculateRegularBoundingBox

`public Rectangle calculateRegularBoundingBox()`
Description copied from interface: `GeometricObject2d`
Compute the regular (oriented to the axes) bounding box of the shape.
Specified by:
`calculateRegularBoundingBox` in interface `GeometricObject2d`
Returns:
the regular bounding box as [x,y,width,height]
• translate

```public void translate(float x,
float y)```
Description copied from interface: `GeometricObject2d`
Translate the shapes position
Specified by:
`translate` in interface `GeometricObject2d`
Parameters:
`x` - x-translation
`y` - y-translation
• scale

`public void scale(float sc)`
Description copied from interface: `GeometricObject2d`
Scale the shape by the given amount about (0,0). Scalefactors between 0 and 1 shrink the shape.
Specified by:
`scale` in interface `GeometricObject2d`
Parameters:
`sc` - the scale factor.
• scale

```public void scale(Point2d centre,
float sc)```
Description copied from interface: `GeometricObject2d`
Scale the shape by the given amount about the given point. Scalefactors between 0 and 1 shrink the shape.
Specified by:
`scale` in interface `GeometricObject2d`
Parameters:
`centre` - the centre of the scaling operation
`sc` - the scale factor
• scaleCentroid

`public void scaleCentroid(float sc)`
Description copied from interface: `GeometricObject2d`
Scale the shape about its centroid. Scalefactors between 0 and 1 shrink the shape.
Specified by:
`scaleCentroid` in interface `GeometricObject2d`
Parameters:
`sc` - the scale factor
• calculateCentroid

`public Point2d calculateCentroid()`
Description copied from interface: `GeometricObject2d`
Calculate the centroid of the shape
Specified by:
`calculateCentroid` in interface `GeometricObject2d`
Returns:
the centroid of the shape
• calculateArea

`public double calculateArea()`
Description copied from interface: `Shape`
Calculate the area of the shape
Specified by:
`calculateArea` in interface `Shape`
Returns:
the area of the shape
• minX

`public double minX()`
Specified by:
`minX` in interface `GeometricObject2d`
Returns:
the minimum x-ordinate
• minY

`public double minY()`
Specified by:
`minY` in interface `GeometricObject2d`
Returns:
the minimum y-ordinate
• maxX

`public double maxX()`
Specified by:
`maxX` in interface `GeometricObject2d`
Returns:
the maximum x-ordinate
• maxY

`public double maxY()`
Specified by:
`maxY` in interface `GeometricObject2d`
Returns:
the maximum y-ordinate
• getWidth

`public double getWidth()`
Specified by:
`getWidth` in interface `GeometricObject2d`
Returns:
the width of the regular bounding box
• getHeight

`public double getHeight()`
Specified by:
`getHeight` in interface `GeometricObject2d`
Returns:
the height of the regular bounding box
• getTopLeft

`public Point2d getTopLeft()`
Returns:
The top-left coordinate
• getBottomRight

`public Point2d getBottomRight()`
Returns:
The bottom-right coordinate
• transform

`public Shape transform(Jama.Matrix transform)`
Description copied from interface: `GeometricObject2d`
Apply a 3x3 transform matrix to a copy of the `GeometricObject2d` and return it
Specified by:
`transform` in interface `GeometricObject2d`
Specified by:
`transform` in interface `Shape`
Parameters:
`transform` - 3x3 transform matrix
Returns:
the transformed shape
• asPolygon

`public Polygon asPolygon()`
Description copied from interface: `Shape`
Convert the shape to a polygon representation
Specified by:
`asPolygon` in interface `Shape`
Returns:
a polygon representation of the shape
• setBounds

```public void setBounds(float x,
float y,
float width,
float height)```
Set the position and size of this rectangle
Parameters:
`x` - x-coordinate of top-left
`y` - y-coordinate of top-left
`width` - width
`height` - height
• toString

`public String toString()`
Overrides:
`toString` in class `Object`
• isOverlapping

`public boolean isOverlapping(Rectangle other)`
Test if rectangles overlap.
Parameters:
`other` - the rectangle to test with.
Returns:
true if there is overlap; false otherwise.
• isInside

`public boolean isInside(Rectangle rect)`
Test if the given rectangle is inside this one.
Parameters:
`rect` - the rectangle to test with.
Returns:
true if this rectangle is inside the other; false otherwise.
• overlapping

`public Rectangle overlapping(Rectangle other)`
Get the overlapping rectangle between this rectangle and another.
Parameters:
`other` - the rectangle to test with.
Returns:
the overlap rectangle, or null if there is no overlap.
• percentageOverlap

`public double percentageOverlap(Rectangle other)`
Compute the percentage by which the given rectangle overlaps this one.
Parameters:
`other` -
Returns:
the percentage overlap
• union

`public Rectangle union(Rectangle other)`
Find the rectangle that just contains this rectangle and another rectangle.
Parameters:
`other` - the other rectangle
Returns:
a rectangle
• intersectionArea

`public double intersectionArea(Shape that)`
Description copied from interface: `Shape`
Calls `Polygon.intersectionArea(Shape, int)` with 1 step per pixel dimension. Subsequently this function returns the shared whole pixels of this polygon and that.
Specified by:
`intersectionArea` in interface `Shape`
Returns:
intersection area
• intersectionArea

```public double intersectionArea(Shape that,
int nStepsPerDimension)```
Description copied from interface: `Shape`
Return an estimate for the area of the intersection of this polygon and another polygon. For each pixel step 1 is added if the point is inside both polygons. The length of a step in each direction is calculated as follows: max(intersectionWidth,intersectionHeight)/ (nStepsPerDimention) The total number of points inside the intersection of the shames is divided by the number of points read and multiplied by the total area of the intersection.
Specified by:
`intersectionArea` in interface `Shape`
Returns:
normalised intersection area

```public void readASCII(Scanner in)
throws IOException```
Description copied from interface: `ReadableASCII`
Specified by:
`readASCII` in interface `ReadableASCII`
Parameters:
`in` - source to read from.
Throws:
`IOException` - an error reading input

`public String asciiHeader()`
Description copied from interface: `ReadableASCII`
Specified by:
`asciiHeader` in interface `ReadableASCII`
Specified by:
`asciiHeader` in interface `WriteableASCII`
Returns:

```public void readBinary(DataInput in)
throws IOException```
Description copied from interface: `ReadableBinary`
Specified by:
`readBinary` in interface `ReadableBinary`
Parameters:
`in` - source to read from.
Throws:
`IOException` - an error reading input

`public byte[] binaryHeader()`
Description copied from interface: `ReadableBinary`
Specified by:
`binaryHeader` in interface `ReadableBinary`
Specified by:
`binaryHeader` in interface `WriteableBinary`
Returns:
• writeASCII

```public void writeASCII(PrintWriter out)
throws IOException```
Description copied from interface: `WriteableASCII`
Write the content of this as ascii to out.
Specified by:
`writeASCII` in interface `WriteableASCII`
Parameters:
`out` - sink to write to
Throws:
`IOException` - an error writing to out
• writeBinary

```public void writeBinary(DataOutput out)
throws IOException```
Description copied from interface: `WriteableBinary`
Write the content of this as binary to out.
Specified by:
`writeBinary` in interface `WriteableBinary`
Parameters:
`out` - sink to write to
Throws:
`IOException` - an error writing to out
• clone

`public Rectangle clone()`
Specified by:
`clone` in interface `Shape`
Overrides:
`clone` in class `Object`
Returns:
a copy of the shape
• hashCode

`public int hashCode()`
Overrides:
`hashCode` in class `Object`
• equals

`public boolean equals(Object obj)`
Overrides:
`equals` in class `Object`
• calculatePerimeter

`public double calculatePerimeter()`
Description copied from interface: `Shape`
Calculate the perimeter of the shape
Specified by:
`calculatePerimeter` in interface `Shape`
Returns:
the perimeter of the shape
• minimumBoundingRectangle

`public RotatedRectangle minimumBoundingRectangle()`
Description copied from interface: `Shape`
Compute the minimum size rotated bounding rectangle that contains this shape.
Specified by:
`minimumBoundingRectangle` in interface `Shape`
Returns:
the minimum bounding box
• rotate

```public RotatedRectangle rotate(Point2d p,
double angle)```
Rotate the `Rectangle` about the given pivot with the given angle (in radians)
Parameters:
`p` - the pivot of the rotation
`angle` - the angle in radians
Returns:
the rotated rectangle
• isConvex

`public boolean isConvex()`
Description copied from interface: `Shape`
Test if the shape is convex.
Specified by:
`isConvex` in interface `Shape`
Returns:
true if the shape is convex; false if non-convex