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, Div, Mul, Sub};
use util::CastF32;
use vec3f::Vec3f;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Vec2f {
pub x: f32,
pub y: f32
}
impl Vec2f {
pub fn new(x: f32, y: f32) -> Vec2f {
Vec2f { x: x,
y: y }
}
pub fn cross(self, other: Vec2f) -> f32 {
self.x * other.y - self.y * other.x
}
pub fn lerp(self, other: Vec2f, t: f32) -> Vec2f {
self * (1.0 - t) + other * t
}
pub fn dot(self, other: Vec2f) -> f32 {
self.x * other.x + self.y * other.y
}
pub fn vec3f(self) -> Vec3f {
Vec3f::new(self.x, self.y, 0.0)
}
pub fn distance_squared(self, other: Vec2f) -> f32 {
(self - other).magnitude_squared()
}
pub fn distance(self, other: Vec2f) -> f32 {
self.distance_squared(other).sqrt()
}
pub fn magnitude_squared(self) -> f32 {
self.dot(self)
}
pub fn magnitude(self) -> f32 {
self.magnitude_squared().sqrt()
}
pub fn normalized(self) -> Option<Vec2f> {
let m = self.magnitude();
if m == 0.0 {
None
}
else {
Some(Vec2f::new(self.x / m, self.y / m))
}
}
}
impl Add for Vec2f {
type Output = Vec2f;
fn add(self, v: Vec2f) -> Vec2f {
Vec2f::new(self.x + v.x, self.y + v.y)
}
}
impl Sub for Vec2f {
type Output = Vec2f;
fn sub(self, v: Vec2f) -> Vec2f {
Vec2f::new(self.x - v.x, self.y - v.y)
}
}
impl Mul<f32> for Vec2f {
type Output = Vec2f;
fn mul(self, s: f32) -> Vec2f {
Vec2f::new(self.x * s, self.y * s)
}
}
impl Div<f32> for Vec2f {
type Output = Vec2f;
fn div(self, s: f32) -> Vec2f {
Vec2f::new(self.x / s, self.y / s)
}
}
pub fn detf_2x2(top_left: f32, top_right: f32,
bot_left: f32, bot_right: f32) -> f32 {
top_left * bot_right - top_right * bot_left
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Line2f {
pub points: (Vec2f, Vec2f)
}
impl Line2f {
pub fn new(a: Vec2f, b: Vec2f) -> Line2f {
Line2f { points: (a, b) }
}
pub fn reverse(self) -> Line2f {
Line2f::new(self.points.1, self.points.0)
}
pub fn closest_parametric_point(self, point: Vec2f) -> f32 {
let p0p = point - self.points.0;
let p0p1 = self.points.1 - self.points.0;
p0p.dot(p0p1) / p0p1.dot(p0p1)
}
pub fn point_distance_squared(self, point: Vec2f) -> f32 {
let para = self.closest_parametric_point(point);
let cart = self.cart_from_para(para);
cart.distance_squared(point)
}
pub fn cart_from_para(self, t: f32) -> Vec2f {
self.points.0.lerp(self.points.1, t)
}
pub fn orient(self, point: Vec2f) -> f32 {
detf_2x2(self.points.0.x - point.x, self.points.0.y - point.y,
self.points.1.x - point.x, self.points.1.y - point.y)
}
}
pub fn vec2f<X: CastF32, Y: CastF32>(x: X, y: Y) -> Vec2f {
Vec2f { x: x.as_f32(), y: y.as_f32() }
}