Crate angulus

source ·
Expand description

Unit agnostic angle.

Overview

Using simple floating point numbers to store an angle value is error-prone : you may add two angle with one in radians and the second in degrees or you may try to compute the cosine of a value in degrees and get an unexpected result.

Angle and AngleUnbounded represent an angle value with no specific unit.

Angle represent a canonical angle, i.e. the internal value fit the range (-π, π] in radians.

For example, 90° and -270° have different value but are the same angle.

let a = Angle::from_degrees(90.0);
let b = Angle::from_degrees(-270.0);

assert_eq!(a, b);

Conversely AngleUnbounded represent any angle value.

let a = AngleUnbounded::from_degrees(90.0);
let b = AngleUnbounded::from_degrees(-270.0);

assert_ne!(a, b);

From value to angle

To create an angle from a value, you can use the from_* methods with the unit of the value…

let deg = Angle::from_degrees(90.0);
let rad = Angle::from_radians(3.14);
let turns = Angle::from_turns(0.75);
let grad = Angle::from_gradians(50.0);

or you use the ToAngle trait directly on the value.

let deg = 90.0.deg();
let rad = 3.14.rad();
let turns = 0.75.turns();
let grad = 50.0.grad();

From angle to value

To convert back an angle to a value you can use the to_* methods with the desired unit.

let a = Angle32::QUARTER;

assert_eq!(a.to_radians(), std::f32::consts::FRAC_PI_2);
assert_eq!(a.to_degrees(), 90.0);
assert_eq!(a.to_turns(), 0.25);
assert_eq!(a.to_gradians(), 100.0);

Display

Since Angle and AngleUnbounded are unit agnotic they didn’t implement the Display trait. But you can use one of the unit wrapper from the units module to specify a unit.

Serde support

The serde feature flag enable the support of serde.

Even if Angle and AngleUnbounded are unit agnostic they (de)serialize from/into radians for convenience. But you can use one of the unit wrapper from the units module to specify a unit.

Modules

Defines which types can be used as a floating-point value for the angle.
This module provides wrappers to “colorize” an angle with a specific unit.

Structs

Represents the canonical value of an angle.
Represents an angle that may not be the canonical value.

Traits

Helper trait to convert a numerical value into an angle.

Type Definitions