shapes/
size.rs

1use std::convert::From;
2use std::ops::Mul;
3
4use graphics::math::{ Scalar, Vec2d };
5
6/// The size of a shape.
7#[derive(Clone, Copy, Debug)]
8pub struct Size {
9    /// The horizontal length of the shape (width).
10    pub w: Scalar,
11    /// The vertical length of the shape (height).
12    pub h: Scalar,
13}
14
15impl From<Size> for Vec2d {
16    fn from(size: Size) -> Vec2d {
17        [size.w, size.h]
18    }
19}
20
21impl From<Vec2d> for Size {
22    fn from(v: Vec2d) -> Size {
23        Size { w: v[0], h: v[1] }
24    }
25}
26
27impl From<(Scalar, Scalar)> for Size {
28    fn from((w, h): (Scalar, Scalar)) -> Size {
29        Size { w: w, h: h }
30    }
31}
32
33impl<T: Into<Size>> Mul<T> for Size {
34    type Output = Size;
35
36    fn mul(self, v: T) -> Size {
37        let v: Size = v.into();
38        Size { w: self.w * v.w, h: self.h * v.h }
39    }
40}
41
42impl Mul<Scalar> for Size {
43    type Output = Size;
44
45    fn mul(self, s: Scalar) -> Size {
46        Size { w: self.w * s, h: self.h * s }
47    }
48}