shapes/
point.rs

1use std::convert::From;
2use std::ops::{ Add, Sub };
3
4use graphics::math::{ Scalar, Vec2d };
5
6/// A point in the Cartesian plane.
7#[derive(Clone, Copy, Debug)]
8pub struct Point {
9    /// The x coordinate.
10    pub x: Scalar,
11    /// The y coordinate.
12    pub y: Scalar,
13}
14
15impl Add<Scalar> for Point {
16    type Output = Point;
17
18    fn add(self, s: Scalar) -> Point {
19        Point { x: self.x + s, y: self.y + s }
20    }
21}
22
23impl<T: Into<Point>> Add<T> for Point {
24    type Output = Point;
25
26    fn add(self, v: T) -> Point {
27        let v: Point = v.into();
28        Point { x: self.x + v.x, y: self.y + v.y }
29    }
30}
31
32impl From<Point> for Vec2d {
33    fn from(point: Point) -> Vec2d {
34        [point.x, point.y]
35    }
36}
37
38impl From<Vec2d> for Point {
39    fn from(v: Vec2d) -> Point {
40        Point { x: v[0], y: v[1] }
41    }
42}
43
44impl From<(Scalar, Scalar)> for Point {
45    fn from((x, y): (Scalar, Scalar)) -> Point {
46        Point { x: x, y: y }
47    }
48}
49
50impl Sub<Scalar> for Point {
51    type Output = Point;
52
53    fn sub(self, s: Scalar) -> Point {
54        Point { x: self.x - s, y: self.y - s }
55    }
56}
57
58impl<T: Into<Point>> Sub<T> for Point {
59    type Output = Point;
60
61    fn sub(self, v: T) -> Point {
62        let v = v.into();
63        Point { x: self.x - v.x, y: self.y - v.y }
64    }
65}