Skip to content
FutureBlitz

    Install

    Import std.math.geometry

    std.math.geometry

    Rectangles, boxes, spheres, rays, planes, and frustums, and the tests between them.

    The shapes for finding what's where: Rect in 2D, and AABB, Sphere, Ray, Plane, and Frustum in 3D, with Contains, Intersects, and Raycast. A shape holds the points on its edges. A Raycast gives how far along the ray it first meets the shape, in lengths of the ray's Direction, so a Direction of length 1 makes it a distance; 0 for a ray that starts inside, and Null for one that misses.

    std/math/geometry.fb

    Types

    Rect struct

    Public Struct Rect Implements Equatable, Printable

    A rectangle in 2D, from its corner with the smallest X and Y, of a Width and Height that aren't negative.

    Examples

    
    Local a = Rect(0, 0, 10, 10)
    Local b = Rect(5, 5, 10, 10)
    Expect(a.Intersects(b))
    Expect(a.Intersection(b) == Rect(5, 5, 5, 5))
    Expect(a.Union(b) == Rect(0, 0, 15, 15))
    Expect(a.Contains(Vec2(2, 3)) && !a.Contains(Vec2(12, 3)))

    std/math/geometry.fb, line 30

    • Public Field X: Float field #

      Its left edge, the smallest X it has.

    • Public Field Y: Float field #

      Its top edge, the smallest Y it has.

    • Public Field Width: Float field #

      How wide it is, which isn't negative.

    • Public Field Height: Float field #

      How high it is, which isn't negative.

    • Public Static Method FromCorners(a: Vec2, b: Vec2): Rect static method #

      The rectangle from one corner to the other, whichever way around they are.

    • Public Static Method FromCenter(center: Vec2, size: Vec2): Rect static method #

      The rectangle of the size, with its center at the point.

    • Public Property Min: Vec2 property #

      The corner with the smallest X and Y.

    • Public Property Max: Vec2 property #

      The corner with the largest X and Y.

    • Public Property Center: Vec2 property #

      The point in its middle.

    • Public Property Size: Vec2 property #

      Its Width and Height, as a Vec2.

    • Public Property Area: Float property #

      Its Width times its Height.

    • Public Method Contains(point: Vec2): Bool method #

      Whether the point is in the rectangle or on its edge.

    • Public Method Contains(other: Rect): Bool method #

      Whether the other rectangle is all in this one.

    • Public Method Intersects(other: Rect): Bool method #

      Whether the two have a point in common, an edge's included.

    • Public Method Intersection(other: Rect): Rect? method #

      The rectangle where the two overlap, or Null where they don't meet.

    • Public Method Union(other: Rect): Rect method #

      The smallest rectangle that holds both.

    • Public Method Grow(amount: Float): Rect method #

      The rectangle with each edge moved out by amount, or in by a negative one. Shrunk past nothing, a side is 0 long, at the center.

    • Public Method ToString(): String method #

      Its parts, as "Rect(0, 0, 10, 5)".

    AABB struct

    Public Struct AABB Implements Equatable, Printable

    An axis-aligned box in 3D, from its corner with the smallest x, y, and z to the one with the largest.

    Examples

    
    Local box = AABB(Vec3(-1, -1, -1), Vec3(1, 1, 1))
    Local ray = Ray(Vec3(0, 0, 5), Vec3(0, 0, -1))
    Expect(box.Raycast(ray) == 4.0)
    Expect(ray.At(4) == Vec3(0, 0, 1))
    Expect(box.Contains(Vec3(0.5, 0, 0)) && !box.Contains(Vec3(2, 0, 0)))

    std/math/geometry.fb, line 133

    • Public Field Min: Vec3 field #

      Its corner with the smallest x, y, and z.

    • Public Field Max: Vec3 field #

      Its corner with the largest x, y, and z.

    • Public Static Method FromCenter(center: Vec3, size: Vec3): AABB static method #

      The box of the size, with its center at the point.

    • @PanicsAtCaller Public Static Method FromPoints(points: Vec3[]): AABB static method #

      The smallest box that holds every point. No points panics.

    • Public Property Center: Vec3 property #

      The point in its middle.

    • Public Property Size: Vec3 property #

      How wide, high, and deep it is.

    • Public Property Extents: Vec3 property #

      Half the size: how far each face is from the center.

    • Public Method Contains(point: Vec3): Bool method #

      Whether the point is in the box or on its surface.

    • Public Method Contains(other: AABB): Bool method #

      Whether the other box is all in this one.

    • Public Method Intersects(other: AABB): Bool method #

      Whether the two have a point in common, a face's included.

    • Public Method Intersects(sphere: Sphere): Bool method #

      Whether the sphere and the box have a point in common.

    • Public Method ClosestPoint(point: Vec3): Vec3 method #

      The point in the box nearest the one given: that point itself when it's inside.

    • Public Method Raycast(ray: Ray): Float? method #

      How far along the ray it first meets the box, by its slabs, one axis at a time.

    • Public Method Union(other: AABB): AABB method #

      The smallest box that holds both.

    • Public Method Union(point: Vec3): AABB method #

      The smallest box that holds this one and the point.

    • Public Method Grow(amount: Float): AABB method #

      The box with each face moved out by amount, or in by a negative one. Shrunk past nothing, an axis is 0 long, at the center.

    • Public Method Transform(m: Mat4): AABB method #

      The smallest box that holds this one transformed by the matrix, as an object's box is moved into the world.

    • Public Method ToString(): String method #

      Its parts, as "AABB((0, 0, 0), (1, 1, 1))".

    Sphere struct

    Public Struct Sphere Implements Equatable, Printable

    A ball: the points no further from its Center than its Radius.

    Examples

    
    Local ball = Sphere(Vec3(0, 0, 0), 1)
    Expect(ball.Contains(Vec3(0.5, 0, 0)))
    Expect(ball.Raycast(Ray(Vec3(0, 0, 5), Vec3(0, 0, -1))) == 4.0)

    std/math/geometry.fb, line 265

    • Public Field Center: Vec3 field #

      The point in its middle.

    • Public Field Radius: Float field #

      How far its surface is from its Center.

    • Public Method Contains(point: Vec3): Bool method #

      Whether the point is in the sphere or on its surface, by squares in Doubles, which no Float's square overflows.

    • Public Method Intersects(other: Sphere): Bool method #

      Whether the two have a point in common, by squares in Doubles.

    • Public Method Intersects(box: AABB): Bool method #

      Whether the box and the sphere have a point in common.

    • Public Method Raycast(ray: Ray): Float? method #

      How far along the ray it first meets the sphere, worked out in Doubles from where the ray comes nearest the center, as Ray Tracing Gems' chapter 7 has it, so a sphere far off or small, or a short Direction, loses nothing to rounding.

    • Public Method ToString(): String method #

      Its parts, as "Sphere((0, 0, 0), 1)".

    Ray struct

    Public Struct Ray Implements Equatable, Printable

    A half-line, from its Origin along its Direction.

    Examples

    
    Expect(Ray(Vec3(0, 0, 0), Vec3(1, 0, 0)).At(3) == Vec3(3, 0, 0))

    std/math/geometry.fb, line 336

    • Public Field Origin: Vec3 field #

      Where it starts.

    • Public Field Direction: Vec3 field #

      Which way it goes, and how far each length of it along the ray is.

    • Public Method At(distance: Float): Vec3 method #

      The point so far along the ray, in lengths of its Direction.

    • Public Method ToString(): String method #

      Its parts, as "Ray((0, 0, 0), (0, 0, -1))".

    Plane struct

    Public Struct Plane Implements Equatable, Printable

    A flat surface without end, of the points p where Dot(Normal, p) is Distance, with a Normal of length 1. The side the Normal points to is in front.

    Examples

    
    Local ground = Plane.FromPointNormal(Vec3(0, 0, 0), Vec3(0, 1, 0))
    Expect(ground.SignedDistance(Vec3(3, 2, 1)) == 2.0)
    Expect(ground.Raycast(Ray(Vec3(0, 5, 0), Vec3(0, -1, 0))) == 5.0)

    std/math/geometry.fb, line 361

    • Public Field Normal: Vec3 field #

      Which way it faces, of length 1.

    • Public Field Distance: Float field #

      Its distance from the origin along its Normal: Dot(Normal, p) for each point p on it.

    • @PanicsAtCaller Public Static Method FromPointNormal(point: Vec3, normal: Vec3): Plane static method #

      The plane through the point, facing the way the normal does. A zero normal panics.

    • @PanicsAtCaller Public Static Method FromPoints(a: Vec3, b: Vec3, c: Vec3): Plane static method #

      The plane through the three points, facing the side they go counterclockwise around. Three on a line panic.

    • Public Method SignedDistance(point: Vec3): Float method #

      How far the point is in front of the plane, negative behind it.

    • Public Method ClosestPoint(point: Vec3): Vec3 method #

      The point on the plane nearest the one given.

    • Public Method Raycast(ray: Ray): Float? method #

      How far along the ray it meets the plane, from either side, 0 for a ray that starts on it, whichever way it goes. A ray along it misses.

    • Public Method ToString(): String method #

      Its parts, as "Plane((0, 1, 0), 0)".

    Frustum struct

    Public Struct Frustum Implements Equatable, Printable

    What a camera sees: the six planes of its projection, each facing in.

    Examples

    
    Local camera = Mat4.Perspective(Deg(90), 1, 0.1, 100) *
                   Mat4.LookAt(Vec3(0, 0, 5), Vec3(0, 0, 0), Vec3(0, 1, 0))
    Local view = Frustum.FromMatrix(camera)
    Expect(view.Contains(Vec3(0, 0, 0)))
    Expect(!view.Contains(Vec3(0, 0, 10)))
    Expect(view.Intersects(Sphere(Vec3(0, 0, 0), 1)))

    std/math/geometry.fb, line 419

    • Public Field Left: Plane field #

      Its left plane, facing in.

    • Public Field Right: Plane field #

      Its right plane, facing in.

    • Public Field Bottom: Plane field #

      Its bottom plane, facing in.

    • Public Field Top: Plane field #

      Its top plane, facing in.

    • Public Field Near: Plane field #

      Its near plane, facing in.

    • Public Field Far: Plane field #

      Its far plane, facing in.

    • Public Static Method FromMatrix(m: Mat4): Frustum static method #

      The frustum of a projection times a view matrix, as Mat4.Perspective and LookAt make them, with depth from 0 to 1, in the space the view matrix takes from: the world's, for a camera.

    • Public Method Contains(point: Vec3): Bool method #

      Whether the point is inside, or on a plane.

    • Public Method Intersects(sphere: Sphere): Bool method #

      Whether the sphere is inside, or partly: False only when it's all behind a plane.

    • Public Method Intersects(box: AABB): Bool method #

      Whether the box is inside, or partly: False only when it's all behind a plane, so a box just past a corner of the frustum can be True, as culling allows.

    • Public Method ToString(): String method #

      Its six planes, left, right, bottom, top, near, and far, as "Frustum(Plane(...), ...)".

    Report an issue with this page or with FutureBlitz on GitHub.