Expand description
Opinionated 2D math library for building GUIs.
Includes vectors, positions, rectangles etc.
Conventions (unless otherwise specified):
- All angles are in radians
- X+ is right and Y+ is down.
- (0,0) is left top.
- Dimension order is always
x y
§Integrating with other math libraries.
emath
does not strive to become a general purpose or all-powerful math library.
For that, use something else (glam
, nalgebra
, …)
and enable the mint
feature flag in emath
to enable implicit conversion to/from emath
.
§Feature flags
Modules§
- align
- One- and two-dimensional alignment (
Align::Center
,Align2::LEFT_TOP
etc). - smart_
aim - Find “simple” numbers is some range. Used by sliders.
Macros§
- emath_
assert - An assert that is only active when
emath
is compiled with theextra_asserts
feature or with theextra_debug_asserts
feature in debug builds.
Structs§
- Align2
- Two-dimension alignment, e.g.
Align2::LEFT_TOP
. - History
- This struct tracks recent values of some time series.
- Pos2
- A position on screen.
- Rangef
- Includive range of floats, i.e.
min..=max
, but more ergonomic thanRangeInclusive
. - Rect
- A rectangular region of space.
- Rect
Transform - Linearly transforms positions from one
Rect
to another. - Rot2
- Represents a rotation in the 2D plane.
A rotation of 𝞃/4 = 90° rotates the X axis to the Y axis.
Normally a
Rot2
is normalized (unit-length). If not, it will also scale vectors. - Vec2
- A vector has a direction and length.
A
Vec2
is often used to represent a size.
Enums§
- Align
- left/center/right or top/center/bottom alignment for e.g. anchors and layouts.
Traits§
- NumExt
- Extends
f32
,Vec2
etc withat_least
andat_most
as aliases formax
andmin
. - Numeric
- Implemented for all builtin numeric types
- One
- Helper trait to implement
lerp
andremap
. - Real
- Helper trait to implement
lerp
andremap
.
Functions§
- almost_
equal - Return true when arguments are the same within some rounding error.
- exponential_
smooth_ factor - Calculate a lerp-factor for exponential smoothing using a time step.
- format_
with_ decimals_ in_ range - format_
with_ minimum_ decimals - inverse_
lerp - Where in the range is this value? Returns 0-1 if within the range.
- lerp
- Linear interpolation.
- normalized_
angle - Wrap angle to
[-PI, PI]
range. - pos2
pos2(x, y) == Pos2::new(x, y)
- remap
- Linearly remap a value from one range to another,
so that when
x == from.start()
returnsto.start()
and whenx == from.end()
returnsto.end()
. - remap_
clamp - Like
remap
, but also clamps the value so that the returned value is always in theto
range. - round_
to_ decimals - Round a value to the given number of decimal places.
- vec2
vec2(x, y) == Vec2::new(x, y)