95 #define PM_INLINE __forceinline
96#elif defined(__GNUC__) || defined(__clang__)
97 #define PM_INLINE static inline __attribute((always_inline))
106#ifdef PICO_MATH_DOUBLE
110 #define PM_EPSILON 1e-7
112 #define PM_PI 3.14159265358979323846264338327950288
113 #define PM_PI2 (2.0 * PM_PI)
114 #define PM_E 2.71828182845904523536028747135266250
116 #define PM_FLOAT_MIN DBL_MIN
117 #define PM_FLOAT_MAX DBL_MAX
124 #define pf_atan2 atan2
129 #define pf_floor floor
139 #define PM_EPSILON 1e-5f
141 #define PM_PI 3.14159265359f
142 #define PM_PI2 (2.0f * PM_PI)
143 #define PM_E 2.71828182845f
145 #define PM_FLOAT_MIN FLT_MIN
146 #define PM_FLOAT_MAX FLT_MAX
148 #define pf_sqrt sqrtf
151 #define pf_acos acosf
152 #define pf_asin asinf
153 #define pf_atan2 atan2f
155 #define pf_fmod fmodf
158 #define pf_floor floorf
159 #define pf_ceil ceilf
160 #define pf_log2 log2f
202 return ((val < min) ? min : ((val > max) ? max : val));
215 return ((0 == val) ? 0.0f : ((val > 0.0f) ? 1.0f : -1.0f));
235 return a + (b - a) * alpha;
268#define pv2_make(x, y) ((const pv2){ x, y })
314 return v1.
x * v2.
x + v1.
y * v2.
y;
423#define pv2_zero() (pv2_make(0.0f, 0.0f))
472#define pt2_make(t00, t01, tx, t10, t11, ty) ((const pt2){ t00, t10, t01, t11, tx, ty })
588 0.0f, scale.
y, 0.0f);
651#define pb2_make_minmax(min, max) ((const pb2){ min, max })
656#define pb2_make(x, y, w, h) ((const pb2){ { x, y }, { x + w, y + h } })
661#define pb2_zero() (pb2_make(0.0f, 0.0f, 0.0f, 0.0f))
746 return x > b->
min.
x &&
814#ifdef PICO_MATH_IMPLEMENTATION
868 t->
t00 = sx * c; t->
t01 = sy * -s;
869 t->
t10 = sx * s; t->
t11 = sy * c;
879 if (0.0f == cos_sign)
905 t->
t00 = sx * c; t->
t01 = sy * -s;
906 t->
t10 = sx * s; t->
t11 = sy * c;
918 pfloat inv_det = 1.0f / det;
922 out.
t00 = t->
t11 * inv_det; out.
t01 = -t->
t01 * inv_det;
923 out.
t10 = -t->
t10 * inv_det; out.
t11 = t->
t00 * inv_det;
973 out.
t00 = sx * c; out.
t01 = sy * -s; out.
tx = tx;
974 out.
t10 = sx * s; out.
t11 = sy * c; out.
ty = ty;
994 return pb2_make(0.0f, 0.0f, 0.0f, 0.0f);
1004 return pb2_make(0.0f, 0.0f, 0.0f, 0.0f);
1009 for (
int i = 1; i < count; i++)
1033 verts[0] =
pt2_map(t, verts[0]);
1034 verts[1] =
pt2_map(t, verts[1]);
1035 verts[2] =
pt2_map(t, verts[2]);
1036 verts[3] =
pt2_map(t, verts[3]);
1048 for (
int i = 0; i < 2; i++)
1050 uint64_t result = (seed += 0x9E3779B97f4A7C15);
1051 result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9;
1052 result = (result ^ (result >> 27)) * 0x94D049BB133111EB;
1053 result = result ^ (result >> 31);
1057 rng->
s[j] = (uint32_t)result;
1058 rng->
s[j + 1] = (uint32_t)(result >> 32);
1062static uint32_t rng_rol32(uint32_t x,
int k)
1064 return (x << k) | (x >> (32 - k));
1069 uint32_t *s = rng->
s;
1070 uint32_t
const result = rng_rol32(s[1] * 5, 7) * 9;
1071 uint32_t
const t = s[1] << 17;
1079 s[3] = rng_rol32(s[3], 45);
PM_INLINE pfloat pv2_dot(pv2 v1, pv2 v2)
Dot product.
Definition pico_math.h:312
PM_INLINE void pt2_translate(pt2 *t, pv2 pos)
Applies a translation a transform.
Definition pico_math.h:641
#define pv2_make(x, y)
Constructs a vector.
Definition pico_math.h:268
#define pf_atan2
Definition pico_math.h:153
PM_INLINE pv2 pv2_min(pv2 v1, pv2 v2)
Computes the component-wise minimum of two vectors.
Definition pico_math.h:436
PM_INLINE pv2 pv2_proj(pv2 v1, pv2 v2)
Projects a vector onto another.
Definition pico_math.h:391
PM_INLINE pv2 pv2_polar(pfloat angle, pfloat len)
Contructs a vector in polar coordinates.
Definition pico_math.h:428
PM_INLINE pt2 pt2_translation(pv2 pos)
Constructs a translation transform.
Definition pico_math.h:608
PM_INLINE pfloat pf_clamp(pfloat val, pfloat min, pfloat max)
Clamps the value to the given range.
Definition pico_math.h:200
PM_INLINE pfloat pt2_det(const pt2 *t)
Returns the determinant of the transform.
Definition pico_math.h:560
PM_INLINE pv2 pv2_lerp(pv2 v1, pv2 v2, pfloat alpha)
Linearly interpolates between two vectors.
Definition pico_math.h:412
pfloat pf_lerp_angle(pfloat angle1, pfloat angle2, pfloat alpha)
Linearly interpolates between two angles.
PM_INLINE pv2 pv2_scale(pv2 v, pfloat c)
Scales a vector.
Definition pico_math.h:304
PM_INLINE pv2 pv2_normalize(pv2 v)
Normalizes a vector (sets its length to one)
Definition pico_math.h:338
PM_INLINE void pt2_scale(pt2 *t, pv2 scale)
Scales a transform.
Definition pico_math.h:619
#define pf_cos
Definition pico_math.h:149
uint32_t prng_random(prng_t *rng)
Generates a pseudo random number in [0, UINT32_MAX].
pv2 pt2_get_scale(const pt2 *t)
Gets the scale of the transform.
pb2 pb2_combine(const pb2 *b1, const pb2 *b2)
Computes the union of b1 and `b2.
PM_INLINE pv2 pb2_get_pos(const pb2 *b)
Returns the position of an AABB.
Definition pico_math.h:666
#define PM_INLINE
Definition pico_math.h:99
#define PM_PI2
Definition pico_math.h:142
void pt2_set_scale(pt2 *t, pv2 scale)
Sets the scale of the transform.
PM_INLINE bool pb2_contains(const pb2 *b1, const pb2 *b2)
Returns true if the first box is contained within the second.
Definition pico_math.h:730
pfloat pf_random(prng_t *rng)
Generates a psuedo random number in [0, 1].
#define pt2_make(t00, t01, tx, t10, t11, ty)
Constructs a 2D transform.
Definition pico_math.h:472
PM_INLINE bool pf_equal(pfloat c1, pfloat c2)
Returns true if the values are within epsilon of one another.
Definition pico_math.h:221
PM_INLINE pv2 pv2_sub(pv2 v1, pv2 v2)
Subtracts two vectors.
Definition pico_math.h:294
pt2 pt2_lerp(const pt2 *t1, const pt2 *t2, pfloat alpha)
Linearly interpolates two transforms.
PM_INLINE void pt2_rotate(pt2 *t, pfloat angle)
Applies a rotation to a transform.
Definition pico_math.h:630
float pfloat
A single precision floating point number.
Definition pico_math.h:137
PM_INLINE pfloat pf_lerp(pfloat a, pfloat b, pfloat alpha)
Linearly interpolates the two values.
Definition pico_math.h:233
#define pf_sin
Definition pico_math.h:150
PM_INLINE bool pv2_equal(pv2 v1, pv2 v2)
Returns true if the vectors are equal (within epsilon)
Definition pico_math.h:273
#define pb2_make_minmax(min, max)
Definition pico_math.h:651
pt2 pt2_mult(const pt2 *t1, const pt2 *t2)
Composes two transformations.
PM_INLINE pfloat pb2_area(const pb2 *b)
Returns the area of the box.
Definition pico_math.h:755
#define pf_sqrt
Definition pico_math.h:148
pb2 pb2_transform(const pt2 *t, const pb2 *b)
Computes the minimum AABB obtained by transforming the vertices of the specified AABB.
bool pb2_equal(const pb2 *b1, const pb2 *b2)
Returns true if the bounding boxes are equal (within epsilon)
#define pf_floor
Definition pico_math.h:158
PM_INLINE pv2 pb2_center(const pb2 *b)
Computes the center of the box.
Definition pico_math.h:763
#define pb2_make(x, y, w, h)
Constructs a 2D box (rectangle)
Definition pico_math.h:656
#define pf_acos
Definition pico_math.h:151
PM_INLINE void pb2_set_pos(pb2 *b, pv2 pos)
Sets the position of an AABB.
Definition pico_math.h:684
PM_INLINE pv2 pt2_get_pos(const pt2 *t)
Gets the translation components of the transform.
Definition pico_math.h:492
#define pf_abs
Definition pico_math.h:154
#define pf_ceil
Definition pico_math.h:159
PM_INLINE void pb2_set_size(pb2 *b, pv2 size)
Sets the dimensions of an AABB.
Definition pico_math.h:695
PM_INLINE pfloat pf_normalize_angle(pfloat angle)
Clamps the angle to be in [0, 2 * PI].
Definition pico_math.h:250
bool pt2_equal(const pt2 *t1, const pt2 *t2)
Returns true if the transforms are equal (within epsilon)
PM_INLINE pt2 pt2_identity(void)
Return the identity transform.
Definition pico_math.h:477
#define PM_EPSILON
Definition pico_math.h:139
PM_INLINE pfloat pv2_angle(pv2 v)
Returns the angle the vector with respect to the current basis.
Definition pico_math.h:380
PM_INLINE pv2 pv2_floor(pv2 v)
Computes the component-wise floor of the specified vector.
Definition pico_math.h:452
PM_INLINE pfloat pv2_len(pv2 v)
Returns the length of the vector.
Definition pico_math.h:328
#define pf_max
Definition pico_math.h:161
PM_INLINE void pt2_set_pos(pt2 *t, pv2 pos)
Sets the translation components of the transform.
Definition pico_math.h:502
PM_INLINE pfloat pv2_dist(pv2 v1, pv2 v2)
Returns the distance between the two vectors.
Definition pico_math.h:400
#define pf_min
Definition pico_math.h:162
PM_INLINE pt2 pt2_scaling(pv2 scale)
Constructs a scaling transform.
Definition pico_math.h:585
void prng_seed(prng_t *rng, uint64_t seed)
Initialize and seed the RNG.
PM_INLINE pfloat pf_sign(pfloat val)
Computes the sign of the number.
Definition pico_math.h:213
PM_INLINE pfloat pt2_get_angle(const pt2 *t)
Gets the angle of rotation of the transform.
Definition pico_math.h:511
PM_INLINE pt2 pt2_rotation(pfloat angle)
Constructs a rotation transform.
Definition pico_math.h:595
PM_INLINE pfloat pv2_cross(pv2 v1, pv2 v2)
A 2D analog of the 3D cross product.
Definition pico_math.h:371
pt2 pt2_inv(const pt2 *t)
Calculates the inverse of the transform.
PM_INLINE pfloat pv2_len2(pv2 v)
Returns the square of the length of the vector.
Definition pico_math.h:320
PM_INLINE pv2 pv2_perp(pv2 v)
Construct a vector that is perpendicular to the specified vector.
Definition pico_math.h:363
PM_INLINE pv2 pb2_get_size(const pb2 *b)
Returns the dimensions of an AABB.
Definition pico_math.h:674
PM_INLINE pv2 pv2_ceil(pv2 v)
Computes the component-wise ceiling of the specified vector.
Definition pico_math.h:460
pb2 pb2_overlap(const pb2 *b1, const pb2 *b2)
Computes the intersection of b1 and b2
PM_INLINE pv2 pv2_add(pv2 v1, pv2 v2)
Adds two vectors.
Definition pico_math.h:284
pb2 pb2_enclosing(const pv2 verts[], int count)
Computes the minimum box containing all of the vertices.
PM_INLINE bool pb2_contains_point(const pb2 *b, pv2 v)
Returns true if the box contains the point v
Definition pico_math.h:741
PM_INLINE bool pb2_overlaps(const pb2 *b1, const pb2 *b2)
Return true if the two bounding boxes intersect.
Definition pico_math.h:719
PM_INLINE pv2 pv2_reflect(pv2 v)
Negates a vector (scales it by -1.0)
Definition pico_math.h:353
PM_INLINE pv2 pv2_max(pv2 v1, pv2 v2)
Computes the component-wise maximum of two vectors.
Definition pico_math.h:444
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.
Definition pico_math.h:549
A 2D axis-aligned-bounding-box (AABB)
Definition pico_math.h:189
pv2 min
Definition pico_math.h:190
pv2 max
Definition pico_math.h:190
The pseudo random number generator (RNG) state.
Definition pico_math.h:786
uint32_t s[4]
Definition pico_math.h:787
A 2D transform.
Definition pico_math.h:181
pfloat t01
Definition pico_math.h:182
pfloat t11
Definition pico_math.h:182
pfloat tx
Definition pico_math.h:182
pfloat t00
Definition pico_math.h:182
pfloat ty
Definition pico_math.h:182
pfloat t10
Definition pico_math.h:182
A 2D vector.
Definition pico_math.h:173
pfloat y
Definition pico_math.h:174
pfloat x
Definition pico_math.h:174