Crate euclid [−] [src]
A collection of strongly typed math tools for computer graphics with an inclination towards 2d graphics and layout.
All types are generic over the scalar type of their component (f32
, i32
, etc.),
and tagged with a generic Unit parameter which is useful to prevent mixing
values from different spaces. For example it should not be legal to translate
a screen-space position by a world-space vector and this can be expressed using
the generic Unit parameter.
This unit system is not mandatory and all Typed* structures have an alias
with the default unit: UnknownUnit
.
for example Point2D<T>
is equivalent to TypedPoint2D<T, UnknownUnit>
.
Client code typically creates a set of aliases for each type and doesn't need
to deal with the specifics of typed units further. For example:
All euclid types are marked #[repr(C)]
in order to facilitate exposing them to
foreign function interfaces (provided the underlying scalar type is also repr(C)
).
use euclid::*; pub struct ScreenSpace; pub type ScreenPoint = TypedPoint2D<f32, ScreenSpace>; pub type ScreenSize = TypedSize2D<f32, ScreenSpace>; pub struct WorldSpace; pub type WorldPoint = TypedPoint3D<f32, WorldSpace>; pub type ProjectionMatrix = TypedMatrix4D<f32, WorldSpace, ScreenSpace>; // etc...
Components are accessed in their scalar form by default for convenience, and most
types additionally implement strongly typed accessors which return typed Length
wrappers.
For example:
let p = WorldPoint::new(0.0, 1.0, 1.0); // p.x is an f32. println!("p.x = {:?} ", p.x); // p.x is a Length<f32, WorldSpace>. println!("p.x_typed() = {:?} ", p.x_typed()); // Length::get returns the scalar value (f32). assert_eq!(p.x, p.x_typed().get());
Reexports
pub use length::Length; |
pub use scale_factor::ScaleFactor; |
pub use matrix2d::Matrix2D; |
pub use matrix2d::TypedMatrix2D; |
pub use matrix4d::Matrix4D; |
pub use matrix4d::TypedMatrix4D; |
pub use point::Point2D; |
pub use point::TypedPoint2D; |
pub use point::Point3D; |
pub use point::TypedPoint3D; |
pub use point::Point4D; |
pub use point::TypedPoint4D; |
pub use rect::Rect; |
pub use rect::TypedRect; |
pub use side_offsets::SideOffsets2D; |
pub use side_offsets::TypedSideOffsets2D; |
pub use size::Size2D; |
pub use size::TypedSize2D; |
Modules
approxeq | |
length |
A one-dimensional length, tagged with its units. |
matrix2d | |
matrix4d | |
num |
A one-dimensional length, tagged with its units. |
point | |
rect | |
scale_factor |
A type-checked scaling factor between units. |
side_offsets |
A group of side offsets, which correspond to top/left/bottom/right for borders, padding, and margins in CSS. |
size | |
trig |
Structs
Deg |
Unit for angles in degrees. |
Rad |
Unit for angles in radians. |
UnknownUnit |
The default unit. |
Type Definitions
Degrees |
A value in Degrees. |
Radians |
A value in radians. |