math.geom2d
Class Box2D
- java.lang.Object
-
- 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:xandy. It also provides methods for clipping others shapes, depending on their type.
-
-
Field Summary
Fields Modifier and Type Field and Description static Box2DINFINITE_BOXThe box corresponding to the the whole plane, with infinite bounds in each direction.static Box2DUNIT_SQUARE_BOXThe box corresponding to the unit square, with bounds [0 1] in each direction
-
Constructor Summary
Constructors Constructor and Description Box2D()Empty constructor (size and position zero)Box2D(double xmin, double xmax, double ymin, double ymax)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 heightBox2D(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
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description booleanalmostEquals(GeometricObject2D obj, double eps)Tests if boxes are the same.java.awt.RectangleasAwtRectangle()Converts to AWT rectangle.java.awt.geom.Rectangle2DasAwtRectangle2D()Converts to AWT Rectangle2D.Polygon2DasRectangle()Converts to a rectangle.Boundary2Dboundary()Returns the boundary of this box.Box2DboundingBox()Deprecated.useless (0.11.1)Box2Dclip(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.Box2Dclone()Deprecated.not necessary to clone immutable objects (0.11.2)booleancontains(double x, double y)Checks if this box contains the point defined by the given coordinates.booleancontains(Point2D point)Checks if this box contains the given point.booleancontainsBounds(Shape2D shape)Tests if the specified Shape is totally contained in this Box2D.static Box2Dcreate(double xmin, double xmax, double ymin, double ymax)Deprecated.since 0.11.1static Box2Dcreate(Point2D p1, Point2D p2)Deprecated.since 0.11.1voiddraw(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.booleanequals(java.lang.Object obj)Test if boxes are the same.voidfill(java.awt.Graphics2D g2)Fills the content of the box on the specified graphics.doublegetHeight()doublegetMaxX()doublegetMaxY()doublegetMinX()doublegetMinY()doublegetWidth()Box2Dintersection(Box2D box)Returns the Box2D which is contained both by this box and by the specified box.booleanisBounded()Returns true if all bounds are finite.Box2Dmerge(Box2D box)Changes the bounds of this box to also include bounds of the argument.java.lang.StringtoString()Box2Dtransform(AffineTransform2D trans)Returns the new box created by an affine transform of this box.Box2Dunion(Box2D box)Returns the Box2D which contains both this box and the specified box.intvertexNumber()Returns the number of vertices of the box.java.util.Collection<Point2D>vertices()
-
-
-
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 xmin, double xmax, double ymin, double ymax)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
-
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:
almostEqualsin interfaceGeometricObject2D- Parameters:
obj- the object to compareeps- 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:
toStringin classjava.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:
equalsin classjava.lang.Object
-
clone
@Deprecated public Box2D clone()
Deprecated. not necessary to clone immutable objects (0.11.2)- Overrides:
clonein classjava.lang.Object
-
-
DMelt 3.0 © DataMelt by jWork.ORG