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

§Optional dependencies

  • bytemuckbytemuck enables you to cast emath types to &[u8].
  • document-features — Enable this when generating docs.
  • mintmint enables interoperability with other math libraries such as glam and nalgebra.
  • serde — Allow serialization using serde.

Re-exports§

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

Modules§

align
One- and two-dimensional alignment (Align::Center, Align2::LEFT_TOP etc).
easing
Easing functions for animations.
smart_aim
Find “simple” numbers is some range. Used by sliders.

Structs§

History
This struct tracks recent values of some time series.
OrderedFloat
Wraps a floating-point value to add total order and hash. Possible types for T are f32 and f64.
Pos2
A position on screen.
Rangef
Inclusive range of floats, i.e. min..=max, but more ergonomic than RangeInclusive.
Rect
A rectangular region of space.
RectTransform
Linearly transforms positions from one Rect to another.
Rot2
Represents a rotation in the 2D plane.
TSTransform
Linearly transforms positions via a translation, then a scaling.
Vec2
A vector has a direction and length. A Vec2 is often used to represent a size.
Vec2b
Two bools, one for each axis (X and Y).

Constants§

GUI_ROUNDING
We (sometimes) round sizes and coordinates to an even multiple of this value.

Traits§

Float
Extension trait to provide ord() method.
GuiRounding
Trait for rounding coordinates and sizes to align with either .
NumExt
Extends f32, Vec2 etc with at_least and at_most as aliases for max and min.
Numeric
Implemented for all builtin numeric types
One
Helper trait to implement lerp and remap.
Real
Helper trait to implement lerp and remap.

Functions§

almost_equal
Return true when arguments are the same within some rounding error.
ease_in_ease_out
Ease in, ease out.
exponential_smooth_factor
Calculate a lerp-factor for exponential smoothing using a time step.
format_with_decimals_in_range
Use as few decimals as possible to show the value accurately, but within the given range.
format_with_minimum_decimals
interpolation_factor
If you have a value animating over time, how much towards its target do you need to move it this frame?
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() returns to.start() and when x == from.end() returns to.end().
remap_clamp
Like remap, but also clamps the value so that the returned value is always in the to range.
round_to_decimals
Round a value to the given number of decimal places.
vec2
vec2(x, y) == Vec2::new(x, y)