math.geom2d.polygon
Class Rectangle2D

java.lang.Object
  extended by math.geom2d.polygon.Rectangle2D
All Implemented Interfaces:
CirculinearDomain2D, CirculinearShape2D, Domain2D, GeometricObject2D, Polygon2D, Shape2D

public class Rectangle2D
extends java.lang.Object
implements Polygon2D

Rectangle2D defines a rectangle rotated around its first corner.


Field Summary
 
Fields inherited from interface math.geom2d.Shape2D
ACCURACY
 
Constructor Summary
Rectangle2D()
          Empty constructor (size and position zero)
Rectangle2D(double x0, double y0, double w, double h)
          Main constructor, specifying position of reference corner and rectangle dimensions.
Rectangle2D(Point2D p1, Point2D p2)
          Creates a rectangle from two corner points.
Rectangle2D(java.awt.geom.Rectangle2D rect)
          Constructor from awt, to allow easy construction from existing apps.
 
Method Summary
 void addVertex(Point2D point)
          Adds a vertex as last vertex of this polygon.
 boolean almostEquals(GeometricObject2D obj, double eps)
          Checks if the two objects are similar up to a given threshold value.
 double area()
          Computes the area of this rectangle, given by the product of width by height.
 Polygon2D asPolygon(int n)
          Returns an approximation of the domain as a polygon, or a MultiPolygon.
 CirculinearContourArray2D<LinearRing2D> boundary()
          Overrides the definition of boundary() such that the boundary of a polygon is defined as a set of LinearRing2D.
 Box2D boundingBox()
          Returns the bounding box of the rectangle.
 CirculinearDomain2D buffer(double dist)
          Computes the buffer of the shape, formed by the set of points located at a distance from the shape that is lower or equal to d.
 Point2D centroid()
          Computes the centroid (center of mass) of this rectangle.
 Polygon2D clip(Box2D box)
          Returns the clipped polygon.
 int closestVertexIndex(Point2D point)
          Computes the index of the closest vertex to the input point.
 Polygon2D complement()
          Returns a new simple Polygon whose vertices are in reverse order of this rectangle.
 boolean contains(double x, double y)
          Checks if this rectangle contains the point given by (x,y)
 boolean contains(Point2D point)
          Checks if this rectangle contains the given point.
 java.util.Collection<LinearRing2D> contours()
          Returns the set of contours that enclose this domain.
 double distance(double x, double y)
          Returns the distance of the point to the polygon.
 double distance(Point2D p)
          Returns the distance of the point to the polygon.
 void draw(java.awt.Graphics2D g2)
          Draws the shape on the given graphics.
 int edgeNumber()
          Returns 4, as a rectangle has four edges.
 java.util.Collection<LineSegment2D> edges()
          Returns the four edges that constitute the boundary of this rectangle.
 boolean equals(java.lang.Object obj)
          Tests if rectangles are the same.
 void fill(java.awt.Graphics2D g2)
          Fills the interior of the domain, using the Graphics current Paint.
 double getHeight()
           
 double getWidth()
           
 double getX()
           
 double getY()
           
 void insertVertex(int i, Point2D point)
          Inserts a vertex at the specified position.
 boolean isBounded()
          Always returns true, because a rectangle is always bounded.
 boolean isEmpty()
          Returns true if the shape does not contain any point.
 void removeVertex(int i)
          Removes the vertex at the given index.
 void setVertex(int i, Point2D point)
          Sets the position of the i-th vertex
 SimplePolygon2D transform(AffineTransform2D trans)
          Returns the new Polygon created by an affine transform of this polygon.
 CirculinearDomain2D transform(CircleInversion2D inv)
          Transforms the shape by a circle inversion.
 Point2D vertex(int i)
          Returns the i-th vertex of the polygon.
 int vertexNumber()
          Returns the number of vertices of the rectangle, which is 4.
 java.util.Collection<Point2D> vertices()
          Returns the vertices of the rectangle as a collection of points.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Rectangle2D

public Rectangle2D(double x0,
                   double y0,
                   double w,
                   double h)
Main constructor, specifying position of reference corner and rectangle dimensions.


Rectangle2D

public Rectangle2D()
Empty constructor (size and position zero)


Rectangle2D

public Rectangle2D(java.awt.geom.Rectangle2D rect)
Constructor from awt, to allow easy construction from existing apps.


Rectangle2D

public Rectangle2D(Point2D p1,
                   Point2D p2)
Creates a rectangle from two corner points. Origin and dimensions are automatically determined.

Method Detail

getX

public double getX()

getY

public double getY()

getWidth

public double getWidth()

getHeight

public double getHeight()

vertices

public java.util.Collection<Point2D> vertices()
Returns the vertices of the rectangle as a collection of points.

Specified by:
vertices in interface Polygon2D
Returns:
the vertices of the rectangle.

vertexNumber

public int vertexNumber()
Returns the number of vertices of the rectangle, which is 4.

Specified by:
vertexNumber in interface Polygon2D
Since:
0.6.3

vertex

public Point2D vertex(int i)
Returns the i-th vertex of the polygon.

Specified by:
vertex in interface Polygon2D
Parameters:
i - index of the vertex, between 0 and 3

setVertex

public void setVertex(int i,
                      Point2D point)
Description copied from interface: Polygon2D
Sets the position of the i-th vertex

Specified by:
setVertex in interface Polygon2D
Parameters:
i - the vertex index
point - the new position of the vertex

addVertex

public void addVertex(Point2D point)
Description copied from interface: Polygon2D
Adds a vertex as last vertex of this polygon.

Specified by:
addVertex in interface Polygon2D
Parameters:
point - the position of the new vertex

insertVertex

public void insertVertex(int i,
                         Point2D point)
Description copied from interface: Polygon2D
Inserts a vertex at the specified position.

Specified by:
insertVertex in interface Polygon2D
Parameters:
i - index at which the specified vertex is to be inserted
point - the position of the new vertex

removeVertex

public void removeVertex(int i)
Description copied from interface: Polygon2D
Removes the vertex at the given index.

Specified by:
removeVertex in interface Polygon2D
Parameters:
i - index of the vertex to remove

closestVertexIndex

public int closestVertexIndex(Point2D point)
Computes the index of the closest vertex to the input point.

Specified by:
closestVertexIndex in interface Polygon2D

edges

public java.util.Collection<LineSegment2D> edges()
Returns the four edges that constitute the boundary of this rectangle.

Specified by:
edges in interface Polygon2D

edgeNumber

public int edgeNumber()
Returns 4, as a rectangle has four edges.

Specified by:
edgeNumber in interface Polygon2D

area

public double area()
Computes the area of this rectangle, given by the product of width by height.

Specified by:
area in interface Polygon2D
Returns:
the signed area of the polygon.
Since:
0.9.1

centroid

public Point2D centroid()
Computes the centroid (center of mass) of this rectangle.

Specified by:
centroid in interface Polygon2D
Returns:
the centroid of the polygon
Since:
0.9.1

asPolygon

public Polygon2D asPolygon(int n)
Description copied from interface: Domain2D
Returns an approximation of the domain as a polygon, or a MultiPolygon.

Specified by:
asPolygon in interface Domain2D
Returns:
a polygon

transform

public CirculinearDomain2D transform(CircleInversion2D inv)
Description copied from interface: CirculinearShape2D
Transforms the shape by a circle inversion. The result is still an instance a CirculinearShape2D.

Specified by:
transform in interface CirculinearDomain2D
Specified by:
transform in interface CirculinearShape2D
Parameters:
inv - the circle inversion
Returns:
the transformed shape

buffer

public CirculinearDomain2D buffer(double dist)
Description copied from interface: CirculinearShape2D
Computes the buffer of the shape, formed by the set of points located at a distance from the shape that is lower or equal to d.

Specified by:
buffer in interface CirculinearShape2D
Parameters:
dist - the maximal distance between a point of the buffer and the shape
Returns:
the buffer of the shape

boundary

public CirculinearContourArray2D<LinearRing2D> boundary()
Description copied from interface: Polygon2D
Overrides the definition of boundary() such that the boundary of a polygon is defined as a set of LinearRing2D.

Specified by:
boundary in interface CirculinearDomain2D
Specified by:
boundary in interface Domain2D
Specified by:
boundary in interface Polygon2D
Returns:
the boundary of the domain

contours

public java.util.Collection<LinearRing2D> contours()
Description copied from interface: Domain2D
Returns the set of contours that enclose this domain. The result is a collection of shapes that implement the Contour2D interface.

Specified by:
contours in interface CirculinearDomain2D
Specified by:
contours in interface Domain2D
Specified by:
contours in interface Polygon2D
See Also:
Contour2D

complement

public Polygon2D complement()
Returns a new simple Polygon whose vertices are in reverse order of this rectangle.

Specified by:
complement in interface CirculinearDomain2D
Specified by:
complement in interface Domain2D
Specified by:
complement in interface Polygon2D
Returns:
the polygon complementary to this

isBounded

public boolean isBounded()
Always returns true, because a rectangle is always bounded.

Specified by:
isBounded in interface Shape2D

isEmpty

public boolean isEmpty()
Description copied from interface: Shape2D
Returns true if the shape does not contain any point. This is the case for example for PointSet2D without any point.

Specified by:
isEmpty in interface Shape2D
Returns:
true if the shape does not contain any point.

distance

public double distance(Point2D p)
Returns the distance of the point to the polygon. The result is the minimal distance computed for each edge if the polygon, or ZERO if the point lies inside the polygon.

Specified by:
distance in interface Shape2D

distance

public double distance(double x,
                       double y)
Returns the distance of the point to the polygon. The result is the minimal distance computed for each edge if the polygon, or ZERO if the point lies inside the polygon.

Specified by:
distance in interface Shape2D

clip

public Polygon2D clip(Box2D box)
Returns the clipped polygon.

Specified by:
clip in interface Domain2D
Specified by:
clip in interface Polygon2D
Specified by:
clip in interface Shape2D
Parameters:
box - the clipping box
Returns:
the clipped shape

boundingBox

public Box2D boundingBox()
Returns the bounding box of the rectangle.

Specified by:
boundingBox in interface Shape2D
Returns:
the bounding box of the shape.

transform

public SimplePolygon2D transform(AffineTransform2D trans)
Returns the new Polygon created by an affine transform of this polygon.

Specified by:
transform in interface Domain2D
Specified by:
transform in interface Polygon2D
Specified by:
transform in interface Shape2D
Parameters:
trans - an affine transform
Returns:
the transformed shape

contains

public boolean contains(Point2D point)
Checks if this rectangle contains the given point.

Specified by:
contains in interface Shape2D

contains

public boolean contains(double x,
                        double y)
Checks if this rectangle contains the point given by (x,y)

Specified by:
contains in interface Shape2D

draw

public void draw(java.awt.Graphics2D g2)
Description copied from interface: Shape2D
Draws the shape on the given graphics. If the shape is empty, nothing is drawn. If the shape is unbounded, an exception is thrown.

Specified by:
draw in interface Domain2D
Specified by:
draw in interface Shape2D
Parameters:
g2 - the Graphics to draw on

fill

public void fill(java.awt.Graphics2D g2)
Description copied from interface: Domain2D
Fills the interior of the domain, using the Graphics current Paint.

Specified by:
fill in interface Domain2D
Parameters:
g2 - the Graphics to fill on

almostEquals

public boolean almostEquals(GeometricObject2D obj,
                            double eps)
Description copied from interface: GeometricObject2D
Checks if the two objects are similar up to a given threshold value. This method can be used to compare the results of geometric computations, that introduce errors due to numerical computations.

Specified by:
almostEquals in interface GeometricObject2D
Parameters:
obj - the object to compare
eps - a threshold value, for example the minimal coordinate difference
Returns:
true if both object have the same value up to the threshold

equals

public boolean equals(java.lang.Object obj)
Tests if rectangles are the same.

Overrides:
equals in class java.lang.Object