Skip to main content

ToBoundingBox

Trait ToBoundingBox 

Source
pub trait ToBoundingBox {
    // Required method
    fn bounding_box(&self) -> BoundingBox;
}
Expand description

This trait provides a standardized way of deriving a BoundingBox from another type T with the bounding_box method.

Implementing ToBoundingBox also auto-implements a From<&T> implementation for BoundingBox.

use bounding_box::{BoundingBox, ToBoundingBox};

struct Circle {
    center: [f64; 2],
    radius: f64
}

impl ToBoundingBox for Circle {
    fn bounding_box(&self) -> BoundingBox {
        return BoundingBox::new(self.center[0] - self.radius,
                                self.center[0] + self.radius,
                                self.center[1] - self.radius,
                                self.center[1] + self.radius);
    }
}

let c = Circle {center: [0.0, 0.0], radius: 1.0};
assert_eq!(c.bounding_box(), BoundingBox::from(&c));

Required Methods§

Source

fn bounding_box(&self) -> BoundingBox

Returns a bounding box for the implementor.

§Example
use bounding_box::{BoundingBox, ToBoundingBox};

struct Circle {
    center: [f64; 2],
    radius: f64
}

impl ToBoundingBox for Circle {
    fn bounding_box(&self) -> BoundingBox {
        return BoundingBox::new(self.center[0] - self.radius,
                                self.center[0] + self.radius,
                                self.center[1] - self.radius,
                                self.center[1] + self.radius);
    }
}

let c = Circle {center: [0.0, 0.0], radius: 1.0};
let bb = c.bounding_box();
assert_eq!(bb.xmin(), -1.0);
assert_eq!(bb.ymin(), -1.0);
assert_eq!(bb.xmax(), 1.0);
assert_eq!(bb.ymax(), 1.0);

Implementors§