5.0 KiB
geometry
A collection of geometry-related functions for circles, spheres, boxes, polygons, and rectangle utilities. Some functionality is implemented in C and exposed here.
rect_intersection(a, b)
Return the intersection of two rectangles. The result may be empty if no intersection.
a: The first rectangle as {x, y, w, h}.
b: The second rectangle as {x, y, w, h}.
Returns: A rectangle that is the intersection of the two. May have zero width/height if no overlap.
rect_intersects(a, b)
a: Rectangle {x,y,w,h}.
b: Rectangle {x,y,w,h}.
Returns: A boolean indicating whether the two rectangles overlap.
rect_expand(a, b)
Merge or combine two rectangles, returning their bounding rectangle.
a: Rectangle {x,y,w,h}.
b: Rectangle {x,y,w,h}.
Returns: A new rectangle that covers the bounds of both input rectangles.
rect_inside(inner, outer)
inner: A rectangle to test.
outer: A rectangle that may contain 'inner'.
Returns: True if 'inner' is completely inside 'outer', otherwise false.
rect_random(rect)
rect: A rectangle {x,y,w,h}.
Returns: A random point within the rectangle (uniform distribution).
cwh2rect(center, wh)
Helper: convert a center point and width/height vector to a rect object.
center: A 2D point [cx, cy].
wh: A 2D size [width, height].
Returns: A rectangle {x, y, w, h} with x,y set to center and w,h set to the given size.
rect_point_inside(rect, point)
rect: A rectangle {x,y,w,h}.
point: A 2D point [px, py].
Returns: True if the point lies inside the rectangle, otherwise false.
rect_pos(rect)
rect: A rectangle {x,y,w,h}.
Returns: A 2D vector [x,y] giving the rectangle's position.
rect_move(rect, offset)
rect: A rectangle {x,y,w,h}.
offset: A 2D vector to add to the rectangle's position.
Returns: A new rectangle with updated x,y offset.
box(w, h)
Construct a box centered at the origin with the given width and height. This overrides the box object above.
w: The width of the box.
h: The height of the box.
Returns: An array of four 2D points representing the corners of a rectangle centered at [0,0].
Sphere-related geometry functions:
- volume(r): Return the volume of a sphere with radius r.
- random(r, theta, phi): Return a random point on or inside a sphere.
volume(r)
r: The sphere radius.
Returns: The volume of the sphere, calculated as (4/3) * pi * r^3.
random(r, theta, phi)
Generate a random point inside a sphere of variable radius, distributing angles in the specified ranges.
r: A single number (radius) or a 2-element array [minRadius, maxRadius].
theta: A single number or 2-element array defining the range in turns for the theta angle, default [0,1].
phi: A single number or 2-element array defining the range in turns for the phi angle, default [-0.5,0.5].
Returns: A 3D point (x,y,z) randomly placed within a sphere.
Circle-related geometry functions:
- area(r): Return the area of a circle with radius r.
- random(r, theta): Return a random 2D point on a circle; uses sphere.random internally and extracts x,z.
area(r)
r: Radius of the circle.
Returns: The area, pi * r^2.
random(r, theta)
r: A radius or [minRadius, maxRadius].
theta: Angle range in turns (single number or [min,max]).
Returns: A 2D point (x,z) in the circle, using the sphere random generator and ignoring y.
points(radius, n)
Shortcut for geometry.arc(radius, 360, n).
radius: The circle's radius.
n: Number of points around the circle.
Returns: An array of 2D points equally spaced around a full 360-degree circle.
ngon(radius, n)
Generates a regular n-gon by calling geometry.arc with full 360 degrees.
radius: The radius of the n-gon from center to each vertex.
n: Number of sides/vertices.
Returns: An array of 2D points forming a regular n-gon.
arc(radius, angle, n, start)
Generate an arc (or partial circle) of n points, each angle spread equally over 'angle' degrees from 'start'.
radius: The distance from center to the arc points.
angle: The total angle (in degrees) over which points are generated, capped at 360.
n: Number of segments (if <=1, empty array is returned).
start: Starting angle (in degrees), default 0.
Returns: An array of 2D points along the arc.
corners2points(ll, ur)
Similar to box.points, but calculates differently.
ll: Lower-left 2D coordinate.
ur: Upper-right 2D coordinate (relative offset in x,y).
Returns: A four-point array of corners [ll, lower-right, upper-right, upper-left].
sortpointsccw(points)
Sort an array of points in CCW order based on their angles from the centroid.
points: An array of 2D points.
Returns: A new array of the same points, sorted counterclockwise around their centroid.
points2cm(points)
points: An array of 2D points.
Returns: The centroid (average x,y) of the given points.