vector.rs
2.91 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// Stuff for manipulating 3 dimensional vectors.
//
// Georg Hopp <georg@steffers.org>
//
// Copyright © 2019 Georg Hopp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
use std::ops::{Add, Sub, Neg, Mul};
use std::fmt;
use crate::{Fractional};
#[derive(Debug, Eq, Clone, Copy)]
pub struct Vector(pub Fractional, pub Fractional, pub Fractional);
impl Vector {
pub fn x(self) -> Fractional { self.0 }
pub fn y(self) -> Fractional { self.1 }
pub fn z(self) -> Fractional { self.2 }
pub fn abs(self) -> Fractional {
let Vector(x, y, z) = self;
(x * x + y * y + z * z).sqrt().unwrap()
}
pub fn mul(self, s :&Fractional) -> Self {
let Vector(x, y, z) = self;
Vector(x * *s, y * *s, z * *s)
}
pub fn dot(self, other :Self) -> Fractional {
let Vector(x1, y1, z1) = self;
let Vector(x2, y2, z2) = other;
x1 * x2 + y1 * y2 + z1 * z2
}
pub fn norm(self) -> Self {
let Fractional(n, d) = self.abs();
// TODO This can result in 0 or inf Vectors…
// Maybe we need to handle zero and inf abs here…
self.mul(&Fractional(d, n))
}
pub fn distance(self, other :Self) -> Fractional {
(self - other).abs()
}
}
impl fmt::Display for Vector {
fn fmt(&self, f :&mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {}, {})", self.0, self.1, self.2)
}
}
impl PartialEq for Vector {
fn eq(&self, other :&Self) -> bool {
let Vector(x1, y1, z1) = self;
let Vector(x2, y2, z2) = other;
x1 == x2 && y1 == y2 && z1 == z2
}
}
impl Add for Vector {
type Output = Self;
fn add(self, other :Self) -> Self {
let Vector(x1, y1, z1) = self;
let Vector(x2, y2, z2) = other;
Vector(x1 + x2, y1 + y2, z1 + z2)
}
}
impl Sub for Vector {
type Output = Self;
fn sub(self, other :Self) -> Self {
self + -other
}
}
impl Neg for Vector {
type Output = Self;
fn neg(self) -> Self {
let Vector(x, y, z) = self;
Self(-x, -y, -z)
}
}
impl Mul for Vector {
type Output = Self;
fn mul(self, other :Self) -> Self {
let Vector(ax, ay, az) = self;
let Vector(bx, by, bz) = other;
Vector( ay * bz - az * by
, az * bx - ax * bz
, ax * by - ay * bx )
}
}