AABB

public struct AABB<Vector> : GeometricType where Vector : VectorType
extension AABB: BoundableType
extension AABB: Equatable where Vector: Equatable, Scalar: Equatable
extension AABB: Hashable where Vector: Hashable, Scalar: Hashable
extension AABB: Encodable where Vector: Encodable, Scalar: Encodable
extension AABB: Decodable where Vector: Decodable, Scalar: Decodable
extension AABB: VolumetricType where Vector: VectorComparable
extension AABB: SelfIntersectableRectangleType where Vector: VectorAdditive & VectorComparable
extension AABB: RectangleType & ConstructableRectangleType & AdditiveRectangleType where Vector: VectorAdditive
extension AABB: DivisibleRectangleType where Vector: VectorDivisible & VectorComparable
extension AABB: ConvexType where Vector: VectorFloatingPoint
extension AABB: SignedDistanceMeasurableType where Vector: VectorFloatingPoint

Represents an axis-aligned bounding box with two N-dimensional vectors that describe the minimal and maximal coordinates of the box’s opposite corners.

  • Convenience for Vector.Scalar.

    Declaration

    Swift

    public typealias Scalar = Vector.Scalar
  • The minimal coordinate of this box. Must be <= maximum.

    Declaration

    Swift

    public var minimum: Vector
  • The maximal coordinate of this box. Must be >= minimum.

    Declaration

    Swift

    public var maximum: Vector
  • The location of this Box corresponding to its minimal vector. Alias for minimum.

    Declaration

    Swift

    public var location: Vector { get }
  • Initializes a NBox with the given minimum and maximum boundary vectors.

    Declaration

    Swift

    public init(minimum: Vector, maximum: Vector)
  • Returns self.

    Declaration

    Swift

    public var bounds: AABB<Vector> { get }

Available where Vector: Equatable

  • Returns true if the size of this box is zero.

    Size is zero when minimum == maximum.

    let box = AABB2D(left: 1.0, top: 2.0, right: 1.0, bottom: 2.0)
    
    print(box.isSizeZero) // Prints "true"
    

    Declaration

    Swift

    var isSizeZero: Bool { get }

Available where Vector: VectorComparable

  • Returns true iff minimum <= maximum.

    Declaration

    Swift

    public var isValid: Bool { get }
  • Initializes a box containing the minimum area capable of containing the two supplied points.

    let box = AABB2D(of:
        .init(x: -5, y: 4),
        .init(x: 3, y: -2)
    )
    
    print(box) // Prints "(minimum: (x: -5, y: -2), maximum: (x: 3, y: 4))"
    

    Declaration

    Swift

    public init(of p1: Vector, _ p2: Vector)
  • Initializes a box containing the minimum area capable of containing the three supplied points.

    let box = AABB2D(of:
        .init(x: -5, y: 4),
        .init(x: 3, y: 0),
        .init(x: 2, y: -2)
    )
    
    print(box) // Prints "(minimum: (x: -5, y: -2), maximum: (x: 3, y: 4))"
    

    Declaration

    Swift

    public init(of p1: Vector, _ p2: Vector, _ p3: Vector)
  • Initializes a box containing the minimum area capable of containing the four supplied points.

    let box = AABB2D(of:
        .init(x: -2, y: 4),
        .init(x: 3, y: 0),
        .init(x: 2, y: -2),
        .init(x: -5, y: 3)
    )
    
    print(box) // Prints "(minimum: (x: -5, y: -2), maximum: (x: 3, y: 4))"
    

    Declaration

    Swift

    public init(of p1: Vector, _ p2: Vector, _ p3: Vector, _ p4: Vector)
  • Expands this box to include the given point.

    The resulting box is the minimal AABB capable of enclosing the original AABB and vector point.

    var box = AABB2D(left: 1, top: 1, right: 3, bottom: 3)
    
    box.expand(toInclude: .init(x: -5, y: 6))
    
    print(box) // Prints "(minimum: (x: -5, y: 1), maximum: (x: 3, y: 6))"
    

    Declaration

    Swift

    public mutating func expand(toInclude point: Vector)
  • Expands this box to fully include the given set of points.

    Equivalent to calling expand(toInclude:) over each point. If the array is empty, nothing is done.

    The resulting box is the minimal AABB capable of enclosing the original AABB and all vectors in points.

    var box = AABB2D(left: 1, top: 1, right: 3, bottom: 3)
    
    box.expand(toInclude: [
        .init(x: -5, y: 4),
        .init(x: 3, y: -2),
        .init(x: 1, y: 6)
    ])
    
    print(box) // Prints "(minimum: (x: -5, y: -2), maximum: (x: 3, y: 6))"
    

    Declaration

    Swift

    @inlinable
    public mutating func expand<S: Sequence>(
        toInclude points: S
    ) where S.Element == Vector
  • Clamps a given vector’s coordinates to the confines of this AABB.

    Points inside the AABB remain unchanged, while points outside are projected along the edges to the closest point to vector that is within this AABB.

    let box = AABB2D(left: -1, top: -2, right: 3, bottom: 4)
    
    print(box.clamp(.init(x: -3, y: 2))) // Prints "(x: -1, y: 2)"
    print(box.clamp(.init(x: -3, y: -3))) // Prints "(x: -1, y: -2)"
    print(box.clamp(.init(x: 5, y: 1))) // Prints "(x: 3, y: 1)"
    

    Declaration

    Swift

    public func clamp(_ vector: Vector) -> Vector
  • Returns whether a given point is contained within this box.

    The check is inclusive, so the edges of the box are considered to contain the point as well.

    let box = AABB2D(left: -1, top: -2, right: 3, bottom: 4)
    
    print(box.contains(.init(x: -3, y: 2))) // Prints "false"
    print(box.contains(.init(x: 1, y: 4))) // Prints "true"
    

    Declaration

    Swift

    public func contains(_ point: Vector) -> Bool

Available where Vector: VectorAdditive & VectorComparable

  • Returns whether a given box is completely contained inside the boundaries of this box.

    Returns true for aabb.contains(aabb).

    let box1 = AABB2D(left: -1, top: -2, right: 3, bottom: 4)
    let box2 = AABB2D(left: 0, top: 1, right: 2, bottom: 3)
    let box3 = AABB2D(left: 1, top: 1, right: 5, bottom: 5)
    
    print(box1.contains(box: box1)) // Prints "true"
    print(box1.contains(box: box2)) // Prints "true"
    print(box1.contains(box: box3)) // Prints "false"
    

    Declaration

    Swift

    public func contains(_ other: AABB) -> Bool
  • Returns whether this box intersects the given box instance.

    This check is inclusive, so edges of the box are considered to intersect the other bounding box’s edges as well.

    let box1 = AABB2D(left: -1, top: -2, right: 3, bottom: 4)
    let box2 = AABB2D(left: 0, top: 1, right: 2, bottom: 3)
    let box3 = AABB2D(left: 1, top: 1, right: 5, bottom: 5)
    let box4 = AABB2D(left: -1, top: 5, right: 3, bottom: 7)
    
    print(box1.intersects(box: box1)) // Prints "true"
    print(box1.intersects(box: box2)) // Prints "true"
    print(box1.intersects(box: box3)) // Prints "true"
    print(box1.intersects(box: box4)) // Prints "false"
    

    Declaration

    Swift

    public func intersects(_ box: AABB) -> Bool
  • Creates a rectangle which is equal to the positive area shared between this rectangle and other.

    If the AABBs do not intersect (i.e. produce a rectangle with < 0 bounds), nil is returned, instead.

    Declaration

    Swift

    @inlinable
    public func intersection(_ other: `Self`) -> `Self`?
  • Returns a box which is the minimum box capable of fitting self and the given box.

    Returns aabb for aabb.union(aabb).

    let box1 = AABB2D(left: -1, top: -2, right: 3, bottom: 4)
    let box2 = AABB2D(left: 0, top: -3, right: 5, bottom: 3)
    
    print(box1.union(box2)) // Prints (minimum: (x: -1, y: -3), maximum: (x: 5, y: 4))
    

    Declaration

    Swift

    public func union(_ other: AABB) -> AABB
  • Returns a box which is the minimum box capable of fitting left and right.

    Returns aabb for AABB.union(aabb, aabb).

    let box1 = AABB2D(left: -1, top: -2, right: 3, bottom: 4)
    let box2 = AABB2D(left: 0, top: -3, right: 5, bottom: 3)
    
    print(AABB.union(box1, box2)) // Prints (minimum: (x: -1, y: -3), maximum: (x: 5, y: 4))
    

    Declaration

    Swift

    public static func union(_ left: AABB, _ right: AABB) -> AABB

Available where Vector: VectorAdditive

  • Returns a box with minimum and maximum set to Vector.zero.

    Declaration

    Swift

    public static var zero: `Self` { get }
  • Gets the size of this box.

    Declaration

    Swift

    public var size: Vector { get }
  • Returns true if this box is a AABB.zero instance.

    Declaration

    Swift

    public var isZero: Bool { get }
  • Returns this Box represented as a Rectangle

    Declaration

    Swift

    public var asRectangle: NRectangle<Vector> { get }
  • Initializes an AABB with zero minimal and maximal vectors.

    Declaration

    Swift

    public init()
  • Initializes this AABB with the equivalent coordinates of a rectangle with a given location and size.

    Declaration

    Swift

    public init(location: Vector, size: Vector)

Available where Vector: VectorAdditive & VectorComparable

  • Initializes a box containing the minimum area capable of containing all supplied points.

    If no points are supplied, a zero box is created, instead.

    Convenience for init(points:).

    let box = AABB2D(of:
        .init(x: -5, y: 4),
        .init(x: 3, y: -2),
        .init(x: 1, y: 3),
        .init(x: 2, y: 1),
        .init(x: 2, y: 6)
    )
    
    print(box) // Prints "(minimum: (x: -5, y: -2), maximum: (x: 3, y: 6))"
    

    Declaration

    Swift

    init(of p1: Vector, _ p2: Vector, _ p3: Vector, _ p4: Vector, _ remaining: Vector...)
  • Initializes a box out of a set of points, expanding to the smallest area capable of fitting each point.

    let box = AABB2D(points: [
        .init(x: -5, y: 4),
        .init(x: 3, y: -2),
        .init(x: 1, y: 3),
        .init(x: 2, y: 1),
        .init(x: 2, y: 6)
    ])
    
    print(box) // Prints "(minimum: (x: -5, y: -2), maximum: (x: 3, y: 6))"
    

    Declaration

    Swift

    @inlinable
    init<C>(points: C) where Vector == C.Element, C : Collection
  • Initializes the smallest AABB capable of fully containing all of the provided AABB.

    If aabbs is empty, initializes self as Self.zero.

    Declaration

    Swift

    @inlinable
    init<C>(aabbs: C) where C : Collection, C.Element == AABB<Vector>

Available where Vector: VectorMultiplicative

  • Returns an AABB with minimum .zero and maximum .one.

    Declaration

    Swift

    static var unit: `Self` { get }

Available where Vector: VectorDivisible & VectorComparable

  • Subdivides this AABB into 2 ^ D (where D is the dimensional size of Self.Vector) AABBs that occupy the same area as this AABB but subdivide it into equally-sized AABBs.

    The ordering of the subdivisions is not defined.

    Declaration

    Swift

    @inlinable
    public func subdivided() -> [`Self`]

Available where Vector: VectorFloatingPoint