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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};
use linalg::{Vector, Axis};
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Point {
pub fn new(x: f32, y: f32, z: f32) -> Point {
Point { x: x, y: y, z: z }
}
pub fn broadcast(x: f32) -> Point {
Point { x: x, y: x, z: x }
}
pub fn origin() -> Point {
Point::broadcast(0.0)
}
pub fn distance_sqr(&self, a: &Point) -> f32 {
(*self - *a).length_sqr()
}
pub fn distance(&self, a: &Point) -> f32 {
(*self - *a).length()
}
}
impl Add for Point {
type Output = Point;
fn add(self, rhs: Point) -> Point {
Point { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z }
}
}
impl Add<Vector> for Point {
type Output = Point;
fn add(self, rhs: Vector) -> Point {
Point { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z }
}
}
impl Sub for Point {
type Output = Vector;
fn sub(self, rhs: Point) -> Vector {
Vector { x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z }
}
}
impl Sub<Vector> for Point {
type Output = Point;
fn sub(self, rhs: Vector) -> Point {
Point { x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z }
}
}
impl Mul<f32> for Point {
type Output = Point;
fn mul(self, rhs: f32) -> Point {
Point { x: self.x * rhs, y: self.y * rhs, z: self.z * rhs }
}
}
impl Mul<Point> for f32 {
type Output = Point;
fn mul(self, rhs: Point) -> Point {
Point { x: self * rhs.x, y: self * rhs.y, z: self * rhs.z }
}
}
impl Mul<Vector> for Point {
type Output = Point;
fn mul(self, rhs: Vector) -> Point {
Point { x: self.x * rhs.x, y: self.y * rhs.y, z: self.z * rhs.z }
}
}
impl Div for Point {
type Output = Point;
fn div(self, rhs: Point) -> Point {
Point { x: self.x / rhs.x, y: self.y / rhs.y, z: self.z / rhs.z }
}
}
impl Div<f32> for Point {
type Output = Point;
fn div(self, rhs: f32) -> Point {
Point { x: self.x / rhs, y: self.y / rhs, z: self.z / rhs }
}
}
impl Neg for Point {
type Output = Point;
fn neg(self) -> Point {
Point { x: -self.x, y: -self.y, z: -self.z }
}
}
impl Index<usize> for Point {
type Output = f32;
fn index(&self, i: usize) -> &f32 {
match i {
0 => &self.x,
1 => &self.y,
2 => &self.z,
_ => panic!("Invalid index into point"),
}
}
}
impl Index<Axis> for Point {
type Output = f32;
fn index(&self, i: Axis) -> &f32 {
match i {
Axis::X => &self.x,
Axis::Y => &self.y,
Axis::Z => &self.z,
}
}
}
impl IndexMut<usize> for Point {
fn index_mut(&mut self, i: usize) -> &mut f32 {
match i {
0 => &mut self.x,
1 => &mut self.y,
2 => &mut self.z,
_ => panic!("Invalid index into point"),
}
}
}
#[test]
fn test_distance_sqr() {
let a = Point::new(0f32, 0f32, 0f32);
let b = Point::new(3f32, 4f32, 0f32);
assert!(b.distance_sqr(&a) == 25f32);
}