pub struct Scale { /* private fields */ }Expand description
A stateless f64 data domain mapped to an f64 drawing-coordinate range.
Domain and range may ascend or descend but must have distinct finite endpoints.
Logarithmic domains/values must be positive and their log endpoints distinct.
Values extrapolate by default; Self::clamp bounds both mapping directions.
Units are authored by the caller: this type does not convert meters to AU or
add perspective, an orbital model, a clock, or previous-frame state.
use motion_content::diagram::Scale;
let scale = Scale::log10((0.1, 1000.0), (100.0, 860.0))?;
assert!((scale.map(10.0)? - 480.0).abs() < 0.001);
assert!((scale.invert(480.0)? - 10.0).abs() < 1e-10);Implementations§
Source§impl Scale
impl Scale
Sourcepub fn linear(
domain: (f64, f64),
range: (f64, f64),
) -> Result<Self, ValidationError>
pub fn linear( domain: (f64, f64), range: (f64, f64), ) -> Result<Self, ValidationError>
Validates a linear mapping; endpoints may run in either direction.
Sourcepub fn log10(
domain: (f64, f64),
range: (f64, f64),
) -> Result<Self, ValidationError>
pub fn log10( domain: (f64, f64), range: (f64, f64), ) -> Result<Self, ValidationError>
Validates a base-ten logarithmic mapping with positive domain endpoints.
Sourcepub fn clamp(self, enabled: bool) -> Self
pub fn clamp(self, enabled: bool) -> Self
Replaces clamping for both map and invert; disabled by default. Nonpositive log inputs remain invalid even with clamping enabled.
Sourcepub fn domain(self) -> (f64, f64)
pub fn domain(self) -> (f64, f64)
Returns the original, ordered data endpoints, without unit conversion.
Sourcepub fn map(self, value: f64) -> Result<f64, ValidationError>
pub fn map(self, value: f64) -> Result<f64, ValidationError>
Maps a finite data value to drawing coordinates, preserving endpoints exactly.
Extrapolation that cannot be represented as a finite f64 returns an error.
Sourcepub fn invert(self, coordinate: f64) -> Result<f64, ValidationError>
pub fn invert(self, coordinate: f64) -> Result<f64, ValidationError>
Maps finite drawing coordinates back into data units, with the same clamp policy.
Sourcepub fn ticks(self, count: usize) -> Result<Vec<f64>, ValidationError>
pub fn ticks(self, count: usize) -> Result<Vec<f64>, ValidationError>
Returns 2 through 256 uniformly spaced positions in transformed data space.
Linear values are arithmetic; log values are geometric. Endpoints are exact.
These are not automatically rounded “nice” ticks. Requests whose data
values collapse at f64 precision fail rather than returning duplicates.
Sourcepub fn nice_ticks(self, max_count: usize) -> Result<Vec<f64>, ValidationError>
pub fn nice_ticks(self, max_count: usize) -> Result<Vec<f64>, ValidationError>
Returns at most max_count aligned ticks without changing domain or range.
Accepts 2..=256. Linear steps use 1, 2 or 5 times a power of ten;
logarithmic steps use integer powers of ten, skipping decades when needed.
Log domains containing fewer than two decade powers use linear nice steps.
Endpoints are not forced; a narrow domain may yield zero or one tick.
Unrepresentable steps/indices or collapsed values return an error.