Crate emath

source ·
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

  • extra_debug_asserts — Enable additional checks if debug assertions are enabled (debug builds).

  • extra_asserts — Always enable additional checks.

Optional dependencies

  • bytemuckbytemuck enables you to cast emath types to &[u8].

  • document-features — Enable this when generating docs.

  • mintmint enables interopability with other math libraries such as glam and nalgebra.

  • serde — Allow serialization using serde.


pub use align::Align;
pub use align::Align2;


One- and two-dimensional alignment (Align::Center, Align2::LEFT_TOP etc).
Find “simple” numbers is some range. Used by sliders.


An assert that is only active when emath is compiled with the extra_asserts feature or with the extra_debug_asserts feature in debug builds.


This struct tracks recent values of some time series.
A position on screen.
A rectangular region of space.
Linearly transforms positions from one Rect to another.
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.
A vector has a direction and length. A Vec2 is often used to represent a size.


Extends f32, Vec2 etc with at_least and at_most as aliases for max and min.
Implemented for all builtin numeric types
Helper trait to implement lerp and remap.
Helper trait to implement lerp and remap.


Return true when arguments are the same within some rounding error.
Calculate a lerp-factor for exponential smoothing using a time step.
Where in the range is this value? Returns 0-1 if within the range.
Linear interpolation.
Wrap angle to [-PI, PI] range.
pos2(x, y) == Pos2::new(x, y)
Linearly remap a value from one range to another, so that when x == from.start() returns to.start() and when x == from.end() returns to.end().
Like remap, but also clamps the value so that the returned value is always in the to range.
Round a value to the given number of decimal places.
vec2(x, y) == Vec2::new(x, y)