|
PM_INLINE pfloat | pf_clamp (pfloat val, pfloat min, pfloat max) |
| Clamps the value to the given range.
|
|
PM_INLINE pfloat | pf_sign (pfloat val) |
| Computes the sign of the number.
|
|
PM_INLINE bool | pf_equal (pfloat c1, pfloat c2) |
| Returns true if the values are within epsilon of one another.
|
|
PM_INLINE pfloat | pf_lerp (pfloat a, pfloat b, pfloat alpha) |
| Linearly interpolates the two values.
|
|
pfloat | pf_lerp_angle (pfloat angle1, pfloat angle2, pfloat alpha) |
| Linearly interpolates between two angles.
|
|
PM_INLINE pfloat | pf_normalize_angle (pfloat angle) |
| Clamps the angle to be in [0, 2 * PI].
|
|
PM_INLINE bool | pv2_equal (pv2 v1, pv2 v2) |
| Returns true if the vectors are equal (within epsilon)
|
|
PM_INLINE pv2 | pv2_add (pv2 v1, pv2 v2) |
| Adds two vectors.
|
|
PM_INLINE pv2 | pv2_sub (pv2 v1, pv2 v2) |
| Subtracts two vectors.
|
|
PM_INLINE pv2 | pv2_scale (pv2 v, pfloat c) |
| Scales a vector.
|
|
PM_INLINE pfloat | pv2_dot (pv2 v1, pv2 v2) |
| Dot product.
|
|
PM_INLINE pfloat | pv2_len2 (pv2 v) |
| Returns the square of the length of the vector.
|
|
PM_INLINE pfloat | pv2_len (pv2 v) |
| Returns the length of the vector.
|
|
PM_INLINE pv2 | pv2_normalize (pv2 v) |
| Normalizes a vector (sets its length to one)
|
|
PM_INLINE pv2 | pv2_reflect (pv2 v) |
| Negates a vector (scales it by -1.0)
|
|
PM_INLINE pv2 | pv2_perp (pv2 v) |
| Construct a vector that is perpendicular to the specified vector.
|
|
PM_INLINE pfloat | pv2_cross (pv2 v1, pv2 v2) |
| A 2D analog of the 3D cross product.
|
|
PM_INLINE pfloat | pv2_angle (pv2 v) |
| Returns the angle the vector with respect to the current basis.
|
|
PM_INLINE pv2 | pv2_proj (pv2 v1, pv2 v2) |
| Projects a vector onto another.
|
|
PM_INLINE pfloat | pv2_dist (pv2 v1, pv2 v2) |
| Returns the distance between the two vectors.
|
|
PM_INLINE pv2 | pv2_lerp (pv2 v1, pv2 v2, pfloat alpha) |
| Linearly interpolates between two vectors.
|
|
PM_INLINE pv2 | pv2_polar (pfloat angle, pfloat len) |
| Contructs a vector in polar coordinates.
|
|
PM_INLINE pv2 | pv2_min (pv2 v1, pv2 v2) |
| Computes the component-wise minimum of two vectors.
|
|
PM_INLINE pv2 | pv2_max (pv2 v1, pv2 v2) |
| Computes the component-wise maximum of two vectors.
|
|
PM_INLINE pv2 | pv2_floor (pv2 v) |
| Computes the component-wise floor of the specified vector.
|
|
PM_INLINE pv2 | pv2_ceil (pv2 v) |
| Computes the component-wise ceiling of the specified vector.
|
|
PM_INLINE pt2 | pt2_identity (void) |
| Return the identity transform.
|
|
bool | pt2_equal (const pt2 *t1, const pt2 *t2) |
| Returns true if the transforms are equal (within epsilon)
|
|
PM_INLINE pv2 | pt2_get_pos (const pt2 *t) |
| Gets the translation components of the transform.
|
|
PM_INLINE void | pt2_set_pos (pt2 *t, pv2 pos) |
| Sets the translation components of the transform.
|
|
PM_INLINE pfloat | pt2_get_angle (const pt2 *t) |
| Gets the angle of rotation of the transform.
|
|
void | pt2_set_scale (pt2 *t, pv2 scale) |
| Sets the scale of the transform.
|
|
pv2 | pt2_get_scale (const pt2 *t) |
| Gets the scale of the transform.
|
|
void | pt2_set_angle (pt2 *t, pfloat angle) |
| Sets the angle of the transform.
|
|
PM_INLINE pv2 | pt2_map (const pt2 *t, pv2 v) |
| Transforms a vector.
|
|
PM_INLINE pfloat | pt2_det (const pt2 *t) |
| Returns the determinant of the transform.
|
|
pt2 | pt2_inv (const pt2 *t) |
| Calculates the inverse of the transform.
|
|
pt2 | pt2_mult (const pt2 *t1, const pt2 *t2) |
| Composes two transformations.
|
|
pt2 | pt2_lerp (const pt2 *t1, const pt2 *t2, pfloat alpha) |
| Linearly interpolates two transforms.
|
|
PM_INLINE pt2 | pt2_scaling (pv2 scale) |
| Constructs a scaling transform.
|
|
PM_INLINE pt2 | pt2_rotation (pfloat angle) |
| Constructs a rotation transform.
|
|
PM_INLINE pt2 | pt2_translation (pv2 pos) |
| Constructs a translation transform.
|
|
PM_INLINE void | pt2_scale (pt2 *t, pv2 scale) |
| Scales a transform.
|
|
PM_INLINE void | pt2_rotate (pt2 *t, pfloat angle) |
| Applies a rotation to a transform.
|
|
PM_INLINE void | pt2_translate (pt2 *t, pv2 pos) |
| Applies a translation a transform.
|
|
PM_INLINE pv2 | pb2_get_pos (const pb2 *b) |
| Returns the position of an AABB.
|
|
PM_INLINE pv2 | pb2_get_size (const pb2 *b) |
| Returns the dimensions of an AABB.
|
|
PM_INLINE void | pb2_set_pos (pb2 *b, pv2 pos) |
| Sets the position of an AABB.
|
|
PM_INLINE void | pb2_set_size (pb2 *b, pv2 size) |
| Sets the dimensions of an AABB.
|
|
bool | pb2_equal (const pb2 *b1, const pb2 *b2) |
| Returns true if the bounding boxes are equal (within epsilon)
|
|
pb2 | pb2_combine (const pb2 *b1, const pb2 *b2) |
| Computes the union of b1 and `b2.
|
|
pb2 | pb2_overlap (const pb2 *b1, const pb2 *b2) |
| Computes the intersection of b1 and b2
|
|
PM_INLINE bool | pb2_overlaps (const pb2 *b1, const pb2 *b2) |
| Return true if the two bounding boxes intersect.
|
|
PM_INLINE bool | pb2_contains (const pb2 *b1, const pb2 *b2) |
| Returns true if the first box is contained within the second.
|
|
PM_INLINE bool | pb2_contains_point (const pb2 *b, pv2 v) |
| Returns true if the box contains the point v
|
|
PM_INLINE pfloat | pb2_area (const pb2 *b) |
| Returns the area of the box.
|
|
PM_INLINE pv2 | pb2_center (const pb2 *b) |
| Computes the center of the box.
|
|
pb2 | pb2_enclosing (const pv2 verts[], int count) |
| Computes the minimum box containing all of the vertices.
|
|
pb2 | pb2_transform (const pt2 *t, const pb2 *b) |
| Computes the minimum AABB obtained by transforming the vertices of the specified AABB.
|
|
void | prng_seed (prng_t *rng, uint64_t seed) |
| Initialize and seed the RNG.
|
|
uint32_t | prng_random (prng_t *rng) |
| Generates a pseudo random number in [0, UINT32_MAX].
|
|
pfloat | pf_random (prng_t *rng) |
| Generates a psuedo random number in [0, 1].
|
|
A 2D math library for games.
Licensing information at end of header
Notice:
This library has undergone a major revision with release 2.0. Although the functionality is more or less the same, the naming conventions differ substantially. To summarize, 'pm_v2', 'pm_t2', and 'pm_b2' have all been replaced by 'pv2', 'pt2', and 'pb2'. These changes affect both type defintions as well as function names. With scalar functions 'pm_' has been replaced by 'pf_', for example 'pm_equal' is now 'pf_equal'. The type 'pm_float' has been replaced by 'pfloat'. The purpose of these changes is largely to make type and function names more specific and compact. The old (and no longer maintained) version can be found at https://github.com/empyreanx/pico_headers_deprecated
Features:
- Written in C99
- Single header library for easy build system integration
- Arithmetic for 2D vectors, transforms, and AABBs
- Functions for creating and manipulating affine transformations
- Strikes a solid balance between simplicity and performance
- Extensive test suite
- Permissive license (zlib or public domain)
Summary:
This library provides functions that act on three 2D types: vectors (pv2), transforms (pt2), and axis-align bounding boxes (pb2). The library also provides some scalar functions as well as a random number generator.
This library aims to strike a balance between performance and simplicity. Most functions return by value. All vectors are passed by value. Otherwise, transforms and AABBs are passed by pointer. There is no dynamic memory allocation.
Vector functions comprise basic vector creation and manipulation, as well as computing lengths, dot products, projections, and more.
Transformation functions include functions for computing multiplications, determinants, inverses, as well as extracting and inserting transformation parameters. There are also functions for applying rotations, scaling, and translations to a given transform. The provided functions are sufficient for implementing a scene graph.
This library provides linear interpolation for transforms, vectors, and scalars. Interpolating transforms can be used in a variety of contexts, for example, interpolated rendering when using a fixed timestep, or smoothing when performing networked physics.
Bounding box functions provide tests for intersection of AABBs and determnining if a point is contained within a given AABB. There are functions for computing unions and intersections of AABBs as well as for computing the minimum enclosing AABB for a set of points.
The random number generator uses the xoshiro128** algorithm, which is substantially better than rand()
in terms of the quality of its output without sacrificing too much performance.
Please see the unit tests for some concrete examples.
Usage:
To use this library in your project, add the following
#define PICO_MATH_IMPLEMENTATION #include "pico_ml.h"
to a source file (once), then simply include the header normally.