math.geom2d
Class Box2D

java.lang.Object
  extended by math.geom2d.Box2D
All Implemented Interfaces:
java.lang.Cloneable, GeometricObject2D

public class Box2D
extends java.lang.Object
implements GeometricObject2D, java.lang.Cloneable

This class defines bounds of a shape. It stores limits in each direction: x and y. It also provides methods for clipping others shapes, depending on their type.


Field Summary
static Box2D INFINITE_BOX
          The box corresponding to the the whole plane, with infinite bounds in each direction.
static Box2D UNIT_SQUARE_BOX
          The box corresponding to the unit square, with bounds [0 1] in each direction
 
Constructor Summary
Box2D()
          Empty constructor (size and position zero)
Box2D(double x0, double x1, double y0, double y1)
          Main constructor, given bounds for x coord, then bounds for y coord.
Box2D(Point2D point, double w, double h)
          Constructor from a point, a width and an height
Box2D(Point2D p1, Point2D p2)
          Constructor from 2 points, giving extreme coordinates of the box.
Box2D(java.awt.geom.Rectangle2D rect)
          Constructor from awt, to allow easy construction from existing apps.
 
Method Summary
 boolean almostEquals(GeometricObject2D obj, double eps)
          Tests if boxes are the same.
 java.awt.Rectangle asAwtRectangle()
          Converts to AWT rectangle.
 java.awt.geom.Rectangle2D asAwtRectangle2D()
          Converts to AWT Rectangle2D.
 Polygon2D asRectangle()
          Converts to a rectangle.
 Boundary2D boundary()
          Returns the boundary of this box.
 Box2D boundingBox()
          Deprecated. useless (0.11.1)
 Box2D clip(Box2D box)
          Clip this bounding box such that after clipping, it is totally contained in the given box.
 java.util.Collection<StraightLine2D> clippingLines()
          Returns a set of straight of lines defining half-planes, that all contain the box.
 Box2D clone()
           
 boolean contains(double x, double y)
          Checks if this box contains the point defined by the given coordinates.
 boolean contains(Point2D point)
          Checks if this box contains the given point.
 boolean containsBounds(Shape2D shape)
          Tests if the specified Shape is totally contained in this Box2D.
static Box2D create(double xmin, double xmax, double ymin, double ymax)
          Deprecated. since 0.11.1
static Box2D create(Point2D p1, Point2D p2)
          Deprecated. since 0.11.1
 void draw(java.awt.Graphics2D g2)
          Draws the boundary of the box on the specified graphics.
 java.util.Collection<LinearShape2D> edges()
          Returns the set of linear shapes that constitutes the boundary of this box.
 boolean equals(java.lang.Object obj)
          Test if boxes are the same. two boxes are the same if the have exactly the same bounds.
 void fill(java.awt.Graphics2D g2)
          Fills the content of the box on the specified graphics.
 double getHeight()
           
 double getMaxX()
           
 double getMaxY()
           
 double getMinX()
           
 double getMinY()
           
 double getWidth()
           
 Box2D intersection(Box2D box)
          Returns the Box2D which is contained both by this box and by the specified box.
 boolean isBounded()
          Returns true if all bounds are finite.
 Box2D merge(Box2D box)
          Changes the bounds of this box to also include bounds of the argument.
 java.lang.String toString()
           
 Box2D transform(AffineTransform2D trans)
          Returns the new box created by an affine transform of this box.
 Box2D union(Box2D box)
          Returns the Box2D which contains both this box and the specified box.
 int vertexNumber()
          Returns the number of vertices of the box.
 java.util.Collection<Point2D> vertices()
           
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

UNIT_SQUARE_BOX

public static final Box2D UNIT_SQUARE_BOX
The box corresponding to the unit square, with bounds [0 1] in each direction

Since:
0.9.1

INFINITE_BOX

public static final Box2D INFINITE_BOX
The box corresponding to the the whole plane, with infinite bounds in each direction.

Since:
0.9.1
Constructor Detail

Box2D

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


Box2D

public Box2D(double x0,
             double x1,
             double y0,
             double y1)
Main constructor, given bounds for x coord, then bounds for y coord.


Box2D

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


Box2D

public Box2D(Point2D p1,
             Point2D p2)
Constructor from 2 points, giving extreme coordinates of the box.


Box2D

public Box2D(Point2D point,
             double w,
             double h)
Constructor from a point, a width and an height

Method Detail

create

@Deprecated
public static Box2D create(double xmin,
                                      double xmax,
                                      double ymin,
                                      double ymax)
Deprecated. since 0.11.1


create

@Deprecated
public static Box2D create(Point2D p1,
                                      Point2D p2)
Deprecated. since 0.11.1


getMinX

public double getMinX()

getMinY

public double getMinY()

getMaxX

public double getMaxX()

getMaxY

public double getMaxY()

getWidth

public double getWidth()

getHeight

public double getHeight()

isBounded

public boolean isBounded()
Returns true if all bounds are finite.


contains

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


contains

public boolean contains(double x,
                        double y)
Checks if this box contains the point defined by the given coordinates.


containsBounds

public boolean containsBounds(Shape2D shape)
Tests if the specified Shape is totally contained in this Box2D. Note that the test is performed on the bounding box of the shape, then for rotated rectangles, this method can return false with a shape totally contained in the rectangle. The problem does not exist for horizontal rectangle, since edges of rectangle and bounding box are parallel.


clippingLines

public java.util.Collection<StraightLine2D> clippingLines()
Returns a set of straight of lines defining half-planes, that all contain the box. If the box is bounded, the number of straight lines is 4, otherwise it can be less.

Returns:
a set of straight lines

edges

public java.util.Collection<LinearShape2D> edges()
Returns the set of linear shapes that constitutes the boundary of this box.


boundary

public Boundary2D boundary()
Returns the boundary of this box. The boundary can be bounded, in the case of a bounded box. It is unbounded if at least one bound of the box is infinite. If both x bounds or both y-bounds are infinite, the boundary is constituted from 2 straight lines.

Returns:
the box boundary

vertices

public java.util.Collection<Point2D> vertices()

vertexNumber

public int vertexNumber()
Returns the number of vertices of the box.


union

public Box2D union(Box2D box)
Returns the Box2D which contains both this box and the specified box.

Parameters:
box - the bounding box to include
Returns:
a new Box2D

intersection

public Box2D intersection(Box2D box)
Returns the Box2D which is contained both by this box and by the specified box.

Parameters:
box - the bounding box to include
Returns:
a new Box2D

merge

public Box2D merge(Box2D box)
Changes the bounds of this box to also include bounds of the argument.

Parameters:
box - the bounding box to include
Returns:
this

clip

public Box2D clip(Box2D box)
Clip this bounding box such that after clipping, it is totally contained in the given box.

Returns:
the clipped box

transform

public Box2D transform(AffineTransform2D trans)
Returns the new box created by an affine transform of this box. If the box is unbounded, return an infinite box in all directions.


asAwtRectangle

public java.awt.Rectangle asAwtRectangle()
Converts to AWT rectangle.

Returns:
an instance of java.awt.geom.Rectangle2D

asAwtRectangle2D

public java.awt.geom.Rectangle2D asAwtRectangle2D()
Converts to AWT Rectangle2D. Result is an instance of java.awt.geom.Rectangle2D.Double.

Returns:
an instance of java.awt.geom.Rectangle2D

asRectangle

public Polygon2D asRectangle()
Converts to a rectangle.

Returns:
an instance of Polygon2D

draw

public void draw(java.awt.Graphics2D g2)
Draws the boundary of the box on the specified graphics.

Parameters:
g2 - the instance of graphics to draw in.
Throws:
UnboundedBox2DException - if the box is unbounded

fill

public void fill(java.awt.Graphics2D g2)
Fills the content of the box on the specified graphics.

Parameters:
g2 - the instance of graphics to draw in.
Throws:
UnboundedBox2DException - if the box is unbounded

boundingBox

@Deprecated
public Box2D boundingBox()
Deprecated. useless (0.11.1)


almostEquals

public boolean almostEquals(GeometricObject2D obj,
                            double eps)
Tests if boxes are the same. Two boxes are the same if they have the same bounds, up to the specified threshold value.

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

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Test if boxes are the same. two boxes are the same if the have exactly the same bounds.

Overrides:
equals in class java.lang.Object

clone

public Box2D clone()
Overrides:
clone in class java.lang.Object