Skip to content

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) function

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) function

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) function

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) function

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) function

rect: A rectangle {x,y,w,h}.

Returns: A random point within the rectangle (uniform distribution).

cwh2rect(center, wh) function

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) function

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) function

rect: A rectangle {x,y,w,h}.

Returns: A 2D vector [x,y] giving the rectangle's position.

rect_move(rect, offset) function

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) function

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 object

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.

circle object

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.

ngon(radius, n) function

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) function

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) function

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) function

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) function

points: An array of 2D points.

Returns: The centroid (average x,y) of the given points.