-
-
Save rust-play/0816ac00de6d13b1e3e8bd996fb831f3 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Clone, Debug)] | |
pub struct Point(f32, f32); | |
#[derive(Clone, Debug)] | |
pub struct ValidBounds { | |
min: Point, | |
max: Point | |
} | |
#[derive(Clone, Debug)] | |
pub enum Bounds { | |
Invalid, | |
Valid(ValidBounds), | |
} | |
impl Bounds { | |
pub fn new() -> Self { | |
Bounds::Invalid | |
} | |
pub fn with_new_point(&self, p: &Point) -> ValidBounds { | |
match self { | |
Self::Invalid => ValidBounds { min: p.clone(), max: p.clone() }, | |
Self::Valid(vb) => vb.with_new_point(p), | |
} | |
} | |
pub fn add_point(&mut self, p: &Point) { | |
let b2 = self.with_new_point(p); | |
*self = Bounds::Valid(b2); | |
} | |
} | |
impl ValidBounds { | |
pub fn min(&self) -> Point { self.min.clone() } | |
pub fn max(&self) -> Point { self.max.clone() } | |
pub fn with_new_point(&self, p: &Point) -> Self { | |
Self { | |
min: Point(f32::min(self.min.0, p.0), f32::min(self.min.1, p.1)), | |
max: Point(f32::max(self.max.0, p.0), f32::max(self.max.1, p.1)), | |
} | |
} | |
pub fn add_point(&mut self, p: &Point) { | |
let b2 = self.with_new_point(p); | |
*self = b2; | |
} | |
pub fn center(&self) -> Point { | |
let x = (self.min.0 + self.max.0) / 2.0; | |
let y = (self.min.1 + self.max.1) / 2.0; | |
Point(x, y) | |
} | |
pub fn size(&self) -> Point { | |
let width = self.max.0 - self.min.0; | |
let height = self.max.1 - self.min.1; | |
Point(width, height) | |
} | |
pub fn contains(&self, p: &Point) -> bool { | |
let in_x = (self.min.0 ..= self.max.0).contains(&p.0); | |
let in_y = (self.min.1 ..= self.max.1).contains(&p.1); | |
return in_x && in_y; | |
} | |
} | |
fn main() { | |
let b = Bounds::new(); | |
let b = b.with_new_point(&Point(10.0, 5.0)); | |
b.center(); | |
println!("{:?}", b); | |
println!("center: {:?}", b.center()); | |
println!("size: {:?}", b.size()); | |
println!("contains (10, 5): {:?}", b.contains(&Point(10.0, 5.0))); | |
println!("contains (20, 20): {:?}", b.contains(&Point(20.0, 20.0))); | |
println!(); | |
println!("-------"); | |
println!(); | |
let mut vec = vec![Bounds::new(); 5]; | |
vec[2].add_point(&Point(10.0, 7.5)); | |
vec[2].add_point(&Point(5.0, 2.5)); | |
let b = match vec[2] { | |
Bounds::Valid(ref vb) => vb.clone(), // without clone, b would be a borrowed ref, locking vec | |
_ => panic!() | |
}; | |
vec[2].add_point(&Point(0.0, 0.0)); | |
println!("{:?}", vec[2]); | |
println!("{:?}", b); | |
println!("min: {:?} max: {:?}", b.min(), b.max()); | |
println!("center: {:?}", b.center()); | |
println!("size: {:?}", b.size()); | |
println!("contains (7.5, 5.0): {:?}", b.contains(&Point(7.5, 5.0))); | |
println!("contains (20, 20): {:?}", b.contains(&Point(20.0, 20.0))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment