AxisAlignedBox class
An axis-aligned bounding box.
class AxisAlignedBox { /** Bottom left vertex of bounding box. */ Vector lowerBound; /** Top right vertex of bounding box. */ Vector upperBound; /** * Constructs a new box with the given lower and upper bounds. If no bounds * are specified, constructs the box with both bounds at the origin. */ AxisAlignedBox([this.lowerBound = null, this.upperBound = null]) { if (lowerBound == null) lowerBound = new Vector(); if (upperBound == null) upperBound = new Vector(); } /** * Sets this box to be a combination of the two given boxes. * The combination is determined by picking and choosing the lowest x and y * values from the lowerBounds to form a new lower bound and picking and * choosing the largest x and y values from the upperBounds to form a new * upperBound. */ void setFromCombination(AxisAlignedBox boxOne, AxisAlignedBox boxTwo) { lowerBound.x = Math.min(boxOne.lowerBound.x, boxTwo.lowerBound.x); lowerBound.y = Math.min(boxOne.lowerBound.y, boxTwo.lowerBound.y); upperBound.x = Math.max(boxOne.upperBound.x, boxTwo.upperBound.x); upperBound.y = Math.max(boxOne.upperBound.y, boxTwo.upperBound.y); } /** * Sets the bounds to the given values. */ AxisAlignedBox setBounds(Vector lower, Vector upper) { lowerBound.setFrom(lower); upperBound.setFrom(upper); return this; } /** * Returns true if the given box overlaps with this box. */ static bool testOverlap(AxisAlignedBox a, AxisAlignedBox b) => !((b.lowerBound.x > a.upperBound.x || b.lowerBound.y > a.upperBound.y) || (a.lowerBound.x > b.upperBound.x || a.lowerBound.y > b.upperBound.y)); /** * Returns true if the lower bound is strictly less than the upper bound and * both bounds are themselves valid (Vector.isValid() returns true). */ bool isValid() => lowerBound.isValid() && upperBound.isValid() && lowerBound.x < upperBound.x && lowerBound.y < upperBound.y; /** * Returns the center of this box. */ Vector get center() { Vector c = new Vector.copy(lowerBound); c.addLocal(upperBound); c.mulLocal(.5); return c; } /** * Returns true if this box contains the given box. */ bool contains(AxisAlignedBox aabb) => lowerBound.x > aabb.lowerBound.x && lowerBound.y > aabb.lowerBound.y && upperBound.y < aabb.upperBound.y && upperBound.x < aabb.upperBound.x; /** * Sets this box to be a copy of the given box. */ void setFrom(AxisAlignedBox other) { lowerBound.setFrom(other.lowerBound); upperBound.setFrom(other.upperBound); } String toString() => "$lowerBound, $upperBound"; }
Constructors
new AxisAlignedBox([Vector lowerBound = null, Vector upperBound = null]) #
Constructs a new box with the given lower and upper bounds. If no bounds are specified, constructs the box with both bounds at the origin.
AxisAlignedBox([this.lowerBound = null, this.upperBound = null]) { if (lowerBound == null) lowerBound = new Vector(); if (upperBound == null) upperBound = new Vector(); }
Static Methods
bool testOverlap(AxisAlignedBox a, AxisAlignedBox b) #
Returns true if the given box overlaps with this box.
static bool testOverlap(AxisAlignedBox a, AxisAlignedBox b) => !((b.lowerBound.x > a.upperBound.x || b.lowerBound.y > a.upperBound.y) || (a.lowerBound.x > b.upperBound.x || a.lowerBound.y > b.upperBound.y));
Properties
final Vector center #
Returns the center of this box.
Vector get center() { Vector c = new Vector.copy(lowerBound); c.addLocal(upperBound); c.mulLocal(.5); return c; }
final Type runtimeType #
A representation of the runtime type of the object.
external Type get runtimeType;
Operators
bool operator ==(other) #
The equality operator.
The default behavior for all Object
s is to return true if and
only if this
and
other are the same object.
If a subclass overrides the equality operator it should override
the hashCode
method as well to maintain consistency.
bool operator ==(other) => identical(this, other);
Methods
new AxisAlignedBox([Vector lowerBound = null, Vector upperBound = null]) #
Constructs a new box with the given lower and upper bounds. If no bounds are specified, constructs the box with both bounds at the origin.
AxisAlignedBox([this.lowerBound = null, this.upperBound = null]) { if (lowerBound == null) lowerBound = new Vector(); if (upperBound == null) upperBound = new Vector(); }
bool contains(AxisAlignedBox aabb) #
Returns true if this box contains the given box.
bool contains(AxisAlignedBox aabb) => lowerBound.x > aabb.lowerBound.x && lowerBound.y > aabb.lowerBound.y && upperBound.y < aabb.upperBound.y && upperBound.x < aabb.upperBound.x;
int hashCode() #
Get a hash code for this object.
All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator ==
. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.
If a subclass overrides hashCode
it should override the
equality operator as well to maintain consistency.
external int hashCode();
bool isValid() #
Returns true if the lower bound is strictly less than the upper bound and both bounds are themselves valid (Vector.isValid() returns true).
bool isValid() => lowerBound.isValid() && upperBound.isValid() && lowerBound.x < upperBound.x && lowerBound.y < upperBound.y;
noSuchMethod(String name, List args) #
noSuchMethod
is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to noSuchMethod
. If noSuchMethod
returns a value, that value becomes the result of the original
invocation.
The default behavior of noSuchMethod
is to throw a
noSuchMethodError
.
external Dynamic noSuchMethod(String name, List args);
const Object() #
Creates a new Object
instance.
Object
instances have no meaningful state, and are only useful
through their identity. An Object
instance is equal to itself
only.
const Object();
AxisAlignedBox setBounds(Vector lower, Vector upper) #
Sets the bounds to the given values.
AxisAlignedBox setBounds(Vector lower, Vector upper) { lowerBound.setFrom(lower); upperBound.setFrom(upper); return this; }
void setFrom(AxisAlignedBox other) #
Sets this box to be a copy of the given box.
void setFrom(AxisAlignedBox other) { lowerBound.setFrom(other.lowerBound); upperBound.setFrom(other.upperBound); }
void setFromCombination(AxisAlignedBox boxOne, AxisAlignedBox boxTwo) #
Sets this box to be a combination of the two given boxes. The combination is determined by picking and choosing the lowest x and y values from the lowerBounds to form a new lower bound and picking and choosing the largest x and y values from the upperBounds to form a new upperBound.
void setFromCombination(AxisAlignedBox boxOne, AxisAlignedBox boxTwo) { lowerBound.x = Math.min(boxOne.lowerBound.x, boxTwo.lowerBound.x); lowerBound.y = Math.min(boxOne.lowerBound.y, boxTwo.lowerBound.y); upperBound.x = Math.max(boxOne.upperBound.x, boxTwo.upperBound.x); upperBound.y = Math.max(boxOne.upperBound.y, boxTwo.upperBound.y); }
String toString() #
Returns a string representation of this object.
String toString() => "$lowerBound, $upperBound";