Struct nannou::geom::Range

source ·
pub struct Range<S = f32> {
    pub start: S,
    pub end: S,
}
Expand description

Some start and end position along a single axis.

As an example, a Rect is made up of two Ranges; one along the x axis, and one along the y axis.

Fields§

§start: S

The start of some Range along an axis.

§end: S

The end of some Range along an axis.

Implementations§

source§

impl<S> Range<S>
where S: Copy,

source

pub fn new(start: S, end: S) -> Range<S>

Construct a new Range from a given range, i.e. Range::new(start, end).

Examples
use nannou::geom::Range;

assert_eq!(Range { start: 0.0, end: 10.0 }, Range::new(0.0, 10.0));
source

pub fn from_pos_and_len(pos: S, len: S) -> Range<S>
where S: Float,

Construct a new Range from a given length and its centered position.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.0, 10.0), Range::from_pos_and_len(5.0, 10.0));
assert_eq!(Range::new(-5.0, 1.0), Range::from_pos_and_len(-2.0, 6.0));
assert_eq!(Range::new(-100.0, 200.0), Range::from_pos_and_len(50.0, 300.0));
source

pub fn magnitude(&self) -> S
where S: Sub<Output = S>,

The start value subtracted from the end value.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(-5.0, 5.0).magnitude(), 10.0);
assert_eq!(Range::new(5.0, -5.0).magnitude(), -10.0);
assert_eq!(Range::new(15.0, 10.0).magnitude(), -5.0);
source

pub fn len(&self) -> S
where S: Neg<Output = S> + PartialOrd + Sub<Output = S> + Zero,

The absolute length of the Range aka the absolute magnitude.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(-5.0, 5.0).len(), 10.0);
assert_eq!(Range::new(5.0, -5.0).len(), 10.0);
assert_eq!(Range::new(15.0, 10.0).len(), 5.0);
source

pub fn middle(&self) -> S
where S: Float,

Return the value directly between the start and end values.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(-5.0, 5.0).middle(), 0.0);
assert_eq!(Range::new(5.0, -5.0).middle(), 0.0);
assert_eq!(Range::new(10.0, 15.0).middle(), 12.5);
assert_eq!(Range::new(20.0, 40.0).middle(), 30.0);
assert_eq!(Range::new(20.0, -40.0).middle(), -10.0);
source

pub fn invert(self) -> Range<S>

The current range with its start and end values swapped.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(-5.0, 5.0).invert(), Range::new(5.0, -5.0));
assert_eq!(Range::new(-10.0, 10.0).invert(), Range::new(10.0, -10.0));
assert_eq!(Range::new(0.0, 7.25).invert(), Range::new(7.25, 0.0));
assert_eq!(Range::new(5.0, 1.0).invert(), Range::new(1.0, 5.0));
source

pub fn map_value(&self, value: S, other: &Range<S>) -> S
where S: NumCast,

Map the given scalar from Self to some other given Range.

Examples
use nannou::geom::Range;

let a = Range::new(0.0, 5.0);

let b = Range::new(0.0, 10.0);
assert_eq!(a.map_value(2.5, &b), 5.0);
assert_eq!(a.map_value(0.0, &b), 0.0);
assert_eq!(a.map_value(5.0, &b), 10.0);
assert_eq!(a.map_value(-5.0, &b), -10.0);
assert_eq!(a.map_value(10.0, &b), 20.0);

let c = Range::new(10.0, -10.0);
assert_eq!(a.map_value(2.5, &c), 0.0);
assert_eq!(a.map_value(0.0, &c), 10.0);
assert_eq!(a.map_value(5.0, &c), -10.0);
assert_eq!(a.map_value(-5.0, &c), 30.0);
assert_eq!(a.map_value(10.0, &c), -30.0);
source

pub fn lerp(&self, amount: S) -> S
where S: Float,

Interpolates the Range using the given weight.

Examples
use nannou::geom::Range;

let r = Range::new(-5.0, 5.0);
assert_eq!(r.lerp(0.0), -5.0);
assert_eq!(r.lerp(1.0), 5.0);
assert_eq!(r.lerp(0.5), 0.0);
source

pub fn shift(self, amount: S) -> Range<S>
where S: Add<Output = S>,

Shift the Range start and end points by a given scalar.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.0, 5.0).shift(5.0), Range::new(5.0, 10.0));
assert_eq!(Range::new(0.0, 5.0).shift(-5.0), Range::new(-5.0, 0.0));
assert_eq!(Range::new(5.0, -5.0).shift(-5.0), Range::new(0.0, -10.0));
source

pub fn direction(&self) -> S
where S: Neg<Output = S> + One + PartialOrd + Zero,

The direction of the Range represented as a normalised scalar.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.0, 5.0).direction(), 1.0);
assert_eq!(Range::new(0.0, 0.0).direction(), 0.0);
assert_eq!(Range::new(0.0, -5.0).direction(), -1.0);
source

pub fn absolute(self) -> Range<S>
where S: PartialOrd,

Converts the Range to an absolute Range by ensuring that start <= end.

If start > end, then the start and end points will be swapped.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.0, 5.0).absolute(), Range::new(0.0, 5.0));
assert_eq!(Range::new(5.0, 1.0).absolute(), Range::new(1.0, 5.0));
assert_eq!(Range::new(10.0, -10.0).absolute(), Range::new(-10.0, 10.0));
source

pub fn max(self, other: Range<S>) -> Range<S>
where S: Float,

The Range that encompasses both self and the given Range.

The returned Range’s start will always be <= its end.

Examples
use nannou::geom::Range;

let a = Range::new(0.0, 3.0);
let b = Range::new(7.0, 10.0);
assert_eq!(a.max(b), Range::new(0.0, 10.0));

let c = Range::new(-20.0, -30.0);
let d = Range::new(5.0, -7.5);
assert_eq!(c.max(d), Range::new(-30.0, 5.0));
source

pub fn overlap(self, other: Range<S>) -> Option<Range<S>>
where S: PartialOrd + Sub<Output = S> + Zero,

The Range that represents the range of the overlap between two Ranges if there is some.

Note that If one end of self aligns exactly with the opposite end of other, Some Range will be returned with a magnitude of 0.0. This is useful for algorithms that involve calculating the visibility of widgets, as it allows for including widgets whose bounding box may be a one dimensional straight line.

The returned Range’s start will always be <= its end.

Examples
use nannou::geom::Range;

let a = Range::new(0.0, 6.0);
let b = Range::new(4.0, 10.0);
assert_eq!(a.overlap(b), Some(Range::new(4.0, 6.0)));

let c = Range::new(10.0, -30.0);
let d = Range::new(-5.0, 20.0);
assert_eq!(c.overlap(d), Some(Range::new(-5.0, 10.0)));

let e = Range::new(0.0, 2.5);
let f = Range::new(50.0, 100.0);
assert_eq!(e.overlap(f), None);
source

pub fn max_directed(self, other: Range<S>) -> Range<S>
where S: Float,

The Range that encompasses both self and the given Range.

The same as Range::max but retains self’s original direction.

Examples
use nannou::geom::Range;

let a = Range::new(0.0, 3.0);
let b = Range::new(7.0, 10.0);
assert_eq!(a.max_directed(b), Range::new(0.0, 10.0));

let c = Range::new(-20.0, -30.0);
let d = Range::new(5.0, -7.5);
assert_eq!(c.max_directed(d), Range::new(5.0, -30.0));
source

pub fn contains(&self, pos: S) -> bool
where S: PartialOrd,

Is the given scalar within our range.

Examples
use nannou::geom::Range;

let range = Range::new(0.0, 10.0);
assert!(range.contains(5.0));
assert!(!range.contains(12.0));
assert!(!range.contains(-1.0));
assert!(range.contains(0.0));
assert!(range.contains(10.0));
source

pub fn round(self) -> Range<S>
where S: Float,

Round the values at both ends of the Range and return the result.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.25, 9.5).round(), Range::new(0.0, 10.0));
assert_eq!(Range::new(4.95, -5.3).round(), Range::new(5.0, -5.0));
source

pub fn floor(self) -> Range<S>
where S: Float,

Floor the values at both ends of the Range and return the result.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.25, 9.5).floor(), Range::new(0.0, 9.0));
assert_eq!(Range::new(4.95, -5.3).floor(), Range::new(4.0, -6.0));
source

pub fn pad_start(self, pad: S) -> Range<S>
where S: Add<Output = S> + Neg<Output = S> + PartialOrd,

The Range with some padding given to the start value.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.0, 10.0).pad_start(2.0), Range::new(2.0, 10.0));
assert_eq!(Range::new(10.0, 0.0).pad_start(2.0), Range::new(8.0, 0.0));
source

pub fn pad_end(self, pad: S) -> Range<S>
where S: Add<Output = S> + Neg<Output = S> + PartialOrd,

The Range with some padding given to the end value.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.0, 10.0).pad_end(2.0), Range::new(0.0, 8.0));
assert_eq!(Range::new(10.0, 0.0).pad_end(2.0), Range::new(10.0, 2.0));
source

pub fn pad(self, pad: S) -> Range<S>
where S: Add<Output = S> + Neg<Output = S> + PartialOrd,

The Range with some given padding to be applied to each end.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.0, 10.0).pad(2.0), Range::new(2.0, 8.0));
assert_eq!(Range::new(10.0, 0.0).pad(2.0), Range::new(8.0, 2.0));
source

pub fn pad_ends(self, start: S, end: S) -> Range<S>
where S: Add<Output = S> + Neg<Output = S> + PartialOrd,

The Range with some padding given for each end.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.0, 10.0).pad_ends(1.0, 2.0), Range::new(1.0, 8.0));
assert_eq!(Range::new(10.0, 0.0).pad_ends(4.0, 3.0), Range::new(6.0, 3.0));
source

pub fn clamp_value(&self, value: S) -> S
where S: PartialOrd,

Clamp the given value to the range.

Examples
use nannou::geom::Range;

assert_eq!(Range::new(0.0, 5.0).clamp_value(7.0), 5.0);
assert_eq!(Range::new(5.0, -2.5).clamp_value(-3.0), -2.5);
assert_eq!(Range::new(5.0, 10.0).clamp_value(0.0), 5.0);
source

pub fn stretch_to_value(self, value: S) -> Range<S>
where S: PartialOrd,

Stretch the end that is closest to the given value only if it lies outside the Range.

The resulting Range will retain the direction of the original range.

Examples
use nannou::geom::Range;

let a = Range::new(2.5, 5.0);
assert_eq!(a.stretch_to_value(10.0), Range::new(2.5, 10.0));
assert_eq!(a.stretch_to_value(0.0), Range::new(0.0, 5.0));

let b = Range::new(0.0, -5.0);
assert_eq!(b.stretch_to_value(10.0), Range::new(10.0, -5.0));
assert_eq!(b.stretch_to_value(-10.0), Range::new(0.0, -10.0));
source

pub fn has_same_direction(self, other: Range<S>) -> bool
where S: PartialOrd,

Does self have the same direction as other.

Examples
use nannou::geom::Range;

assert!(Range::new(0.0, 1.0).has_same_direction(Range::new(100.0, 200.0)));
assert!(Range::new(0.0, -5.0).has_same_direction(Range::new(-2.5, -6.0)));
assert!(!Range::new(0.0, 5.0).has_same_direction(Range::new(2.5, -2.5)));
source

pub fn align_start_of(self, other: Range<S>) -> Range<S>
where S: PartialOrd + Add<Output = S> + Sub<Output = S>,

Align the start of self to the start of the other Range.

If the direction of other is different to self, self’s end will be aligned to the start of other instead.

Examples
use nannou::geom::Range;

let a = Range::new(2.5, 7.5);
let b = Range::new(0.0, 10.0);
assert_eq!(a.align_start_of(b), Range::new(0.0, 5.0));
assert_eq!(b.align_start_of(a), Range::new(2.5, 12.5));

let c = Range::new(2.5, -2.5);
let d = Range::new(-5.0, 5.0);
assert_eq!(c.align_start_of(d), Range::new(0.0, -5.0));
assert_eq!(d.align_start_of(c), Range::new(-7.5, 2.5));
source

pub fn align_end_of(self, other: Range<S>) -> Range<S>
where S: PartialOrd + Add<Output = S> + Sub<Output = S>,

Align the end of self to the end of the other Range.

If the direction of other is different to self, self’s start will be aligned to the end of other instead.

Examples
use nannou::geom::Range;

let a = Range::new(2.5, 7.5);
let b = Range::new(0.0, 10.0);
assert_eq!(a.align_end_of(b), Range::new(5.0, 10.0));
assert_eq!(b.align_end_of(a), Range::new(-2.5, 7.5));

let c = Range::new(2.5, -2.5);
let d = Range::new(-5.0, 5.0);
assert_eq!(c.align_end_of(d), Range::new(5.0, 0.0));
assert_eq!(d.align_end_of(c), Range::new(-2.5, 7.5));
source

pub fn align_middle_of(self, other: Range<S>) -> Range<S>
where S: Float,

Align the middle of self to the middle of the other Range.

Examples
use nannou::geom::Range;

let a = Range::new(0.0, 5.0);
let b = Range::new(0.0, 10.0);
assert_eq!(a.align_middle_of(b), Range::new(2.5, 7.5));
assert_eq!(b.align_middle_of(a), Range::new(-2.5, 7.5));

let c = Range::new(2.5, -2.5);
let d = Range::new(-10.0, 0.0);
assert_eq!(c.align_middle_of(d), Range::new(-2.5, -7.5));
assert_eq!(d.align_middle_of(c), Range::new(-5.0, 5.0));
source

pub fn align_after(self, other: Range<S>) -> Range<S>
where S: PartialOrd + Add<Output = S> + Sub<Output = S>,

Aligns the start of self with the end of other.

If the directions are opposite, aligns the end of self with the end of other.

Examples
use nannou::geom::Range;

let a = Range::new(2.5, 7.5);
let b = Range::new(0.0, 10.0);
assert_eq!(a.align_after(b), Range::new(10.0, 15.0));
assert_eq!(b.align_after(a), Range::new(7.5, 17.5));

let c = Range::new(2.5, -2.5);
let d = Range::new(-5.0, 5.0);
assert_eq!(c.align_after(d), Range::new(10.0, 5.0));
assert_eq!(d.align_after(c), Range::new(-12.5, -2.5));
source

pub fn align_before(self, other: Range<S>) -> Range<S>
where S: PartialOrd + Add<Output = S> + Sub<Output = S>,

Aligns the end of self with the start of other.

If the directions are opposite, aligns the start of self with the start of other.

Examples
use nannou::geom::Range;

let a = Range::new(2.5, 7.5);
let b = Range::new(0.0, 10.0);
assert_eq!(a.align_before(b), Range::new(-5.0, 0.0));
assert_eq!(b.align_before(a), Range::new(-7.5, 2.5));

let c = Range::new(2.5, -2.5);
let d = Range::new(-5.0, 5.0);
assert_eq!(c.align_before(d), Range::new(-5.0, -10.0));
assert_eq!(d.align_before(c), Range::new(2.5, 12.5));
source

pub fn align_to(self, align: Align, other: Range<S>) -> Range<S>
where S: Float,

Align self to other along the x axis in accordance with the given Align variant.

source

pub fn closest_edge(&self, scalar: S) -> Edge
where S: PartialOrd + Sub<Output = S>,

The closest Edge of self to the given scalar.

Returns Start if the distance between both Edges is equal.

Examples
use nannou::geom::{Edge, Range};

assert_eq!(Range::new(0.0, 10.0).closest_edge(4.0), Edge::Start);
assert_eq!(Range::new(0.0, 10.0).closest_edge(7.0), Edge::End);
assert_eq!(Range::new(0.0, 10.0).closest_edge(5.0), Edge::Start);

Trait Implementations§

source§

impl<S> Clone for Range<S>
where S: Clone,

source§

fn clone(&self) -> Range<S>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<S> Debug for Range<S>
where S: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<S> PartialEq for Range<S>
where S: PartialEq,

source§

fn eq(&self, other: &Range<S>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<S> PartialOrd for Range<S>
where S: PartialOrd,

source§

fn partial_cmp(&self, other: &Range<S>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<S> Copy for Range<S>
where S: Copy,

source§

impl<S> StructuralPartialEq for Range<S>

Auto Trait Implementations§

§

impl<S> RefUnwindSafe for Range<S>
where S: RefUnwindSafe,

§

impl<S> Send for Range<S>
where S: Send,

§

impl<S> Sync for Range<S>
where S: Sync,

§

impl<S> Unpin for Range<S>
where S: Unpin,

§

impl<S> UnwindSafe for Range<S>
where S: UnwindSafe,

Blanket Implementations§

source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Component + Float, Swp: WhitePoint, Dwp: WhitePoint, D: AdaptFrom<S, Swp, Dwp, T>,

source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<Swp, Dwp, T>,

Convert the source color to the destination color using the specified method
source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default
source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T, U> ConvertInto<U> for T
where U: ConvertFrom<T>,

source§

fn convert_into(self) -> U

Convert into T with values clamped to the color defined bounds Read more
source§

fn convert_unclamped_into(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
source§

fn try_convert_into(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSync for T
where T: Sync,