diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..860d634 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "linearscale" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..fb39d2d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "linearscale" +version = "0.1.0" +authors = ["Bill Thiede "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs index e30dd50..3485a23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,41 @@ -pub struct LinearScale {} +/// You configure it with an input range and an output range +/// then you pass it values within the input range and it returns where that value falls in the +/// output range +/// The trick is that input and output can be either integers or floats +/// and if it's all integers no floating point math is used. If either is a float then floating +/// point math is used +use std::ops::Add; +use std::ops::Div; +use std::ops::Mul; +use std::ops::RangeInclusive; +use std::ops::Sub; + +pub struct LinearScale +where + InT: Sub + Add + Mul + Div + From, + OutT: Sub + Add + Mul + Div + From, +{ + input: RangeInclusive, + output: RangeInclusive, +} + +impl LinearScale +where + InT: Sub + Add + Mul + Div + From, + OutT: Sub + Add + Mul + Div + From, +{ + fn new(input: RangeInclusive, output: RangeInclusive) -> LinearScale { + LinearScale { input, output } + } + + fn output_for(&self, v: InT) -> OutT { + OutT::from(v) + //let in_alpha = (v - *self.input.start()) / (*self.input.end() - *self.input.start()); + // (*self.output.start() + (in_alpha * (*self.output.end() - *self.output.start()))) / (*self.output.end() - *self.output.start()) + } +} + +fn main() {} #[cfg(test)] mod tests {