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)))-
Its left edge, the smallest X it has.
-
Its top edge, the smallest Y it has.
-
How wide it is, which isn't negative.
-
How high it is, which isn't negative.
-
The rectangle from one corner to the other, whichever way around they are.
-
The rectangle of the
size, with its center at the point. -
The corner with the smallest X and Y.
-
The corner with the largest X and Y.
-
The point in its middle.
-
Its Width and Height, as a Vec2.
-
Its Width times its Height.
-
Whether the
pointis in the rectangle or on its edge. -
Whether the
otherrectangle is all in this one. -
Whether the two have a point in common, an edge's included.
-
The rectangle where the two overlap, or Null where they don't meet.
-
The smallest rectangle that holds both.
-
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. -
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
-
Its corner with the smallest x, y, and z.
-
Its corner with the largest x, y, and z.
-
The box of the
size, with its center at the point. -
The smallest box that holds every point. No points panics.
-
The point in its middle.
-
How wide, high, and deep it is.
-
Half the size: how far each face is from the center.
-
Whether the
pointis in the box or on its surface. -
Whether the
otherbox is all in this one. -
Whether the two have a point in common, a face's included.
-
Whether the
sphereand the box have a point in common. -
The
pointin the box nearest the one given: thatpointitself when it's inside. -
How far along the
rayit first meets the box, by its slabs, one axis at a time. -
The smallest box that holds both.
-
The smallest box that holds this one and the
point. -
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. -
The smallest box that holds this one transformed by the matrix, as an object's box is moved into the world.
-
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
-
The point in its middle.
-
How far its surface is from its Center.
-
Whether the
pointis in the sphere or on its surface, by squares in Doubles, which no Float's square overflows. -
Whether the two have a point in common, by squares in Doubles.
-
Whether the
boxand the sphere have a point in common. -
How far along the
rayit first meets the sphere, worked out in Doubles from where theraycomes 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. -
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
-
Where it starts.
-
Which way it goes, and how far each length of it along the ray is.
-
The point so far along the ray, in lengths of its Direction.
-
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
-
Which way it faces, of length 1.
-
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): Planestatic method #The plane through the
point, facing the way thenormaldoes. A zeronormalpanics. -
The plane through the three points, facing the side they go counterclockwise around. Three on a line panic.
-
How far the
pointis in front of the plane, negative behind it. -
The
pointon the plane nearest the one given. -
How far along the
rayit meets the plane, from either side, 0 for a ray that starts on it, whichever way it goes. A ray along it misses. -
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
-
Its left plane, facing in.
-
Its right plane, facing in.
-
Its bottom plane, facing in.
-
Its top plane, facing in.
-
Its near plane, facing in.
-
Its far plane, facing in.
-
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.
-
Whether the
pointis inside, or on a plane. -
Whether the
sphereis inside, or partly: False only when it's all behind a plane. -
Whether the
boxis 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. -
Its six planes, left, right, bottom, top, near, and far, as "Frustum(Plane(...), ...)".
Report an issue with this page or with FutureBlitz on GitHub.