# 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) <sub>function</sub>


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




**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) <sub>function</sub>


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




**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) <sub>function</sub>




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


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


### cwh2rect(center, wh) <sub>function</sub>


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




**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) <sub>function</sub>




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


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


### rect_move(rect, offset) <sub>function</sub>




**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) <sub>function</sub>


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 <sub>object</sub>


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 <sub>object</sub>


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


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


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


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


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




**points**: An array of 2D points.


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

