32inline int clamp(
int x,
int a,
int b) {
33 return std::max(std::min(x, b), a);
40 return (a <= b) ?
clamp(x, a, b) :
clamp(x, b, a);
67inline void eucDivMod(
int a,
int b,
int* div,
int* mod) {
88 return n > 0 && (n & (n - 1)) == 0;
96 return x > 0 ? 1 : (x < 0 ? -1 : 0);
106inline float clamp(
float x,
float a = 0.f,
float b = 1.f) {
107 return std::fmax(std::fmin(x, b), a);
113inline float clampSafe(
float x,
float a = 0.f,
float b = 1.f) {
114 return (a <= b) ?
clamp(x, a, b) :
clamp(x, b, a);
124inline float normalizeZero(
float x) {
132 float mod = std::fmod(a, b);
140inline bool isNear(
float a,
float b,
float epsilon = 1e-6f) {
141 return std::fabs(a - b) <= epsilon;
145inline float chop(
float x,
float epsilon = 1e-6f) {
146 return std::fabs(x) <= epsilon ? 0.f : x;
151inline float rescale(
float x,
float xMin,
float xMax,
float yMin,
float yMax) {
152 return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
158 return a + (b - a) * p;
176inline void complexMult(
float ar,
float ai,
float br,
float bi,
float* cr,
float* ci) {
177 *cr = ar * br - ai * bi;
178 *ci = ar * bi + ai * br;
198 return (i == 0) ?
x :
y;
201 return (i == 0) ?
x :
y;
216 return Vec(
x * s,
y * s);
222 return Vec(
x / s,
y / s);
228 return x * b.
x +
y * b.
y;
231 return std::atan2(
y,
x);
234 return std::hypot(
x,
y);
240 return x *
x +
y *
y;
247 float sin = std::sin(angle);
248 float cos = std::cos(angle);
249 return Vec(
x * cos -
y * sin,
x * sin +
y * cos);
258 return Vec(std::fmin(
x, b.
x), std::fmin(
y, b.
y));
261 return Vec(std::fmax(
x, b.
x), std::fmax(
y, b.
y));
264 return Vec(std::fabs(
x), std::fabs(
y));
267 return Vec(std::round(
x), std::round(
y));
270 return Vec(std::floor(
x), std::floor(
y));
273 return Vec(std::ceil(
x), std::ceil(
y));
276 return x == b.
x &&
y == b.
y;
279 return x == 0.f &&
y == 0.f;
282 return std::isfinite(
x) && std::isfinite(
y);
307 Rect(
float posX,
float posY,
float sizeX,
float sizeY) :
pos(
Vec(posX, posY)),
size(
Vec(sizeX, sizeY)) {}
320 return Rect(
Vec(-INFINITY, -INFINITY),
Vec(INFINITY, INFINITY));
498 return a = a.
plus(b);
501 return a = a.
minus(b);
504 return a = a.
mult(b);
507 return a = a.
mult(b);
541#define VEC_ARGS(v) (v).x, (v).y
542#define RECT_ARGS(r) (r).pos.x, (r).pos.y, (r).size.x, (r).size.y
Vec operator/=(Vec &a, const Vec &b)
Definition math.hpp:509
void eucDivMod(int a, int b, int *div, int *mod)
Definition math.hpp:67
int eucDiv(int a, int b)
Euclidean division.
Definition math.hpp:58
bool isEven(T x)
Returns true if x is odd.
Definition math.hpp:19
float chop(float x, float epsilon=1e-6f)
If the magnitude of x if less than epsilon, return 0.
Definition math.hpp:145
Vec operator+(const Vec &a)
Definition math.hpp:470
bool operator==(const Vec &a, const Vec &b)
Definition math.hpp:515
int eucMod(int a, int b)
Euclidean modulus.
Definition math.hpp:47
int clamp(int x, int a, int b)
Limits x between a and b.
Definition math.hpp:32
bool isOdd(T x)
Returns true if x is odd.
Definition math.hpp:25
bool operator!=(const Vec &a, const Vec &b)
Definition math.hpp:518
float rescale(float x, float xMin, float xMax, float yMin, float yMax)
Rescales x from the range [xMin, xMax] to [yMin, yMax].
Definition math.hpp:151
Vec operator-(const Vec &a)
Definition math.hpp:473
float crossfade(float a, float b, float p)
Linearly interpolates between a and b, from p = 0 to p = 1.
Definition math.hpp:157
bool isPow2(T n)
Returns whether n is a power of 2.
Definition math.hpp:87
int log2(int n)
Returns floor(log_2(n)), or 0 if n == 1.
Definition math.hpp:77
bool isNear(float a, float b, float epsilon=1e-6f)
Returns whether a is within epsilon distance from b.
Definition math.hpp:140
Vec operator+=(Vec &a, const Vec &b)
Definition math.hpp:497
Vec operator*(const Vec &a, const Vec &b)
Definition math.hpp:482
Vec operator-=(Vec &a, const Vec &b)
Definition math.hpp:500
__attribute__((optimize("signed-zeros"))) inline float normalizeZero(float x)
Converts -0.f to 0.f.
Definition math.hpp:122
int clampSafe(int x, int a, int b)
Limits x between a and b.
Definition math.hpp:39
float interpolateLinear(const float *p, float x)
Linearly interpolates an array p with index x.
Definition math.hpp:164
T sgn(T x)
Returns 1 for positive numbers, -1 for negative numbers, and 0 for zero.
Definition math.hpp:95
Vec operator/(const Vec &a, const Vec &b)
Definition math.hpp:491
Vec operator*=(Vec &a, const Vec &b)
Definition math.hpp:503
void complexMult(float ar, float ai, float br, float bi, float *cr, float *ci)
Complex multiplication c = a * b.
Definition math.hpp:176
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
2-dimensional rectangle for graphics.
Definition math.hpp:301
Rect(float posX, float posY, float sizeX, float sizeY)
Definition math.hpp:307
Rect nudge(Rect bound) const
Nudges the position to fix inside a bounding box.
Definition math.hpp:393
Vec size
Definition math.hpp:303
Vec pos
Definition math.hpp:302
bool isContaining(Vec v) const
Definition math.hpp:442
Rect(Vec pos, Vec size)
Definition math.hpp:306
Rect expand(Rect b) const
Returns the bounding box of the union of this and b.
Definition math.hpp:401
Vec getCenter() const
Returns the center point of the rectangle.
Definition math.hpp:368
Rect shrink(Vec delta) const
Contracts each corner.
Definition math.hpp:430
Rect clamp(Rect bound) const
Clamps the edges of the rectangle to fit within a bound.
Definition math.hpp:384
Rect grow(Vec delta) const
Expands each corner.
Definition math.hpp:423
bool equals(Rect r) const
Definition math.hpp:344
static Rect fromCorners(Vec a, Vec b)
Constructs a Rect from any two opposite corners.
Definition math.hpp:315
bool contains(Vec v) const
Returns whether this Rect contains a point, inclusive on the left/top, exclusive on the right/bottom.
Definition math.hpp:326
Vec getBottomRight() const
Definition math.hpp:380
float getTop() const
Definition math.hpp:353
float getHeight() const
Definition math.hpp:362
bool isIntersecting(Rect r) const
Definition math.hpp:445
Vec getTopRight() const
Definition math.hpp:374
static Rect fromMinMax(Vec a, Vec b)
Constructs a Rect from a top-left and bottom-right vector.
Definition math.hpp:310
float getRight() const
Definition math.hpp:350
Vec getTopLeft() const
Definition math.hpp:371
Vec interpolate(Vec p)
Returns pos + size * p
Definition math.hpp:437
float getLeft() const
Definition math.hpp:347
bool intersects(Rect r) const
Returns whether this Rect overlaps with another Rect.
Definition math.hpp:340
static Rect inf()
Returns the infinite Rect.
Definition math.hpp:319
bool isEqual(Rect r) const
Definition math.hpp:448
Rect zeroPos() const
Returns a Rect with its position set to zero.
Definition math.hpp:419
bool contains(Rect r) const
Returns whether this Rect contains (is a superset of) a Rect.
Definition math.hpp:333
Rect intersect(Rect b) const
Returns the intersection of this and b.
Definition math.hpp:410
float getBottom() const
Definition math.hpp:356
float getWidth() const
Definition math.hpp:359
Vec getBottomLeft() const
Definition math.hpp:377
Rect()
Definition math.hpp:305
2-dimensional vector of floats, representing a point on the plane for graphics.
Definition math.hpp:189
Vec minus(Vec b) const
Definition math.hpp:212
float square() const
Definition math.hpp:239
Vec normalize() const
Definition math.hpp:236
Vec clampSafe(Rect bound) const
Definition math.hpp:461
Vec plus(Vec b) const
Definition math.hpp:209
bool isFinite() const
Definition math.hpp:281
Vec clamp(Rect bound) const
Definition math.hpp:454
bool isZero() const
Definition math.hpp:278
Vec(float xy)
Definition math.hpp:194
Vec div(Vec b) const
Definition math.hpp:224
bool isEqual(Vec b) const
Definition math.hpp:291
float norm() const
Definition math.hpp:233
float arg() const
Definition math.hpp:230
Vec()
Definition math.hpp:193
Vec round() const
Definition math.hpp:266
Vec crossfade(Vec b, float p)
Definition math.hpp:286
Vec flip() const
Swaps the coordinates.
Definition math.hpp:254
Vec min(Vec b) const
Definition math.hpp:257
Vec mult(float s) const
Definition math.hpp:215
float x
Definition math.hpp:190
Vec abs() const
Definition math.hpp:263
Vec mult(Vec b) const
Definition math.hpp:218
float y
Definition math.hpp:191
Vec neg() const
Negates the vector.
Definition math.hpp:206
Vec max(Vec b) const
Definition math.hpp:260
Vec rotate(float angle)
Rotates counterclockwise in radians.
Definition math.hpp:246
const float & operator[](int i) const
Definition math.hpp:200
float area() const
Definition math.hpp:242
Vec floor() const
Definition math.hpp:269
float & operator[](int i)
Definition math.hpp:197
bool equals(Vec b) const
Definition math.hpp:275
float dot(Vec b) const
Definition math.hpp:227
Vec(float x, float y)
Definition math.hpp:195
Vec div(float s) const
Definition math.hpp:221
Vec ceil() const
Definition math.hpp:272