zoom/vector/
cartesian1.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
extern crate num;
use super::Vector;
use self::num::{Float, Zero, FromPrimitive};
use std::ops::{Add, Sub, Neg, Mul, Div};

#[derive(Copy, Clone)]
pub struct Cartesian1<D> {
    pub x: D
}

impl<D> Cartesian1<D> where D: Copy {
    pub fn new(x: D) -> Self {
        Cartesian1{x: x}
    }
}

impl<D> Zero for Cartesian1<D>
    where D: Float
{
    fn zero() -> Self {
        Cartesian1{x: D::zero()}
    }

    fn is_zero(&self) -> bool {
        self.x.is_zero()
    }
}

impl<D> Add for Cartesian1<D>
    where D: Float
{
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        Cartesian1{x: self.x + rhs.x}
    }
}

impl<D> Sub for Cartesian1<D>
    where D: Float
{
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        Cartesian1{x: self.x - rhs.x}
    }
}

impl<D> Mul<D> for Cartesian1<D>
    where D: Float
{
    type Output = Self;
    fn mul(self, rhs: D) -> Self {
        Cartesian1{x: self.x * rhs}
    }
}

impl<D> Div<D> for Cartesian1<D>
    where D: Float
{
    type Output = Self;
    fn div(self, rhs: D) -> Self {
        Cartesian1{x: self.x / rhs}
    }
}

impl<D> Neg for Cartesian1<D>
    where D: Float
{
    type Output = Self;
    fn neg(self) -> Self {
        Cartesian1{x: -self.x}
    }
}

impl<D> Vector<D> for Cartesian1<D>
    where D: Float + FromPrimitive
{
    fn space_ball(d: D) -> D {
        D::from_u32(2u32).unwrap() * d
    }
    fn dot(&lhs: &Self, rhs: &Self) -> D {
        lhs.x * rhs.x
    }
    fn space_box(&self) -> D {
        self.x
    }
    fn displacement(&self) -> D {
        self.x
    }
}