public class Rectangle extends Object implements Shape, ReadWriteable, Serializable
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 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.
|
Modifier and Type | Method and Description |
---|---|
String |
asciiHeader()
Header for ascii input.
|
Polygon |
asPolygon()
Convert the shape to a polygon representation
|
byte[] |
binaryHeader()
Header for binary input.
|
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)
Read internal state from in.
|
void |
readBinary(DataInput in)
Read internal state from 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.
|
public float x
public float y
public float width
public float height
public Rectangle()
public Rectangle(float x, float y, float width, float height)
x
- x-coordinate of top-lefty
- y-coordinate of top-leftwidth
- widthheight
- heightpublic Rectangle(Rectangle r)
r
- rectangle to copy frompublic boolean isInside(Point2d point)
Shape
public Rectangle calculateRegularBoundingBox()
GeometricObject2d
calculateRegularBoundingBox
in interface GeometricObject2d
public void translate(float x, float y)
GeometricObject2d
translate
in interface GeometricObject2d
x
- x-translationy
- y-translationpublic void scale(float sc)
GeometricObject2d
scale
in interface GeometricObject2d
sc
- the scale factor.public void scale(Point2d centre, float sc)
GeometricObject2d
scale
in interface GeometricObject2d
centre
- the centre of the scaling operationsc
- the scale factorpublic void scaleCentroid(float sc)
GeometricObject2d
scaleCentroid
in interface GeometricObject2d
sc
- the scale factorpublic Point2d calculateCentroid()
GeometricObject2d
calculateCentroid
in interface GeometricObject2d
public double calculateArea()
Shape
calculateArea
in interface Shape
public double minX()
minX
in interface GeometricObject2d
public double minY()
minY
in interface GeometricObject2d
public double maxX()
maxX
in interface GeometricObject2d
public double maxY()
maxY
in interface GeometricObject2d
public double getWidth()
getWidth
in interface GeometricObject2d
public double getHeight()
getHeight
in interface GeometricObject2d
public Point2d getTopLeft()
public Point2d getBottomRight()
public Shape transform(Jama.Matrix transform)
GeometricObject2d
GeometricObject2d
and
return ittransform
in interface GeometricObject2d
transform
in interface Shape
transform
- 3x3 transform matrixpublic Polygon asPolygon()
Shape
public void setBounds(float x, float y, float width, float height)
x
- x-coordinate of top-lefty
- y-coordinate of top-leftwidth
- widthheight
- heightpublic boolean isOverlapping(Rectangle other)
other
- the rectangle to test with.public boolean isInside(Rectangle rect)
rect
- the rectangle to test with.public Rectangle overlapping(Rectangle other)
other
- the rectangle to test with.public double percentageOverlap(Rectangle other)
other
- public Rectangle union(Rectangle other)
other
- the other rectanglepublic double intersectionArea(Shape that)
Shape
Polygon.intersectionArea(Shape, int)
with 1 step per pixel
dimension. Subsequently this function returns the shared whole pixels of
this polygon and that.intersectionArea
in interface Shape
public double intersectionArea(Shape that, int nStepsPerDimension)
Shape
intersectionArea
in interface Shape
public void readASCII(Scanner in) throws IOException
ReadableASCII
readASCII
in interface ReadableASCII
in
- source to read from.IOException
- an error reading inputpublic String asciiHeader()
ReadableASCII
asciiHeader
in interface ReadableASCII
asciiHeader
in interface WriteableASCII
public void readBinary(DataInput in) throws IOException
ReadableBinary
readBinary
in interface ReadableBinary
in
- source to read from.IOException
- an error reading inputpublic byte[] binaryHeader()
ReadableBinary
binaryHeader
in interface ReadableBinary
binaryHeader
in interface WriteableBinary
public void writeASCII(PrintWriter out) throws IOException
WriteableASCII
writeASCII
in interface WriteableASCII
out
- sink to write toIOException
- an error writing to outpublic void writeBinary(DataOutput out) throws IOException
WriteableBinary
writeBinary
in interface WriteableBinary
out
- sink to write toIOException
- an error writing to outpublic double calculatePerimeter()
Shape
calculatePerimeter
in interface Shape
public RotatedRectangle minimumBoundingRectangle()
Shape
minimumBoundingRectangle
in interface Shape
public RotatedRectangle rotate(Point2d p, double angle)
Rectangle
about the given pivot with the given angle
(in radians)p
- the pivot of the rotationangle
- the angle in radians