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
use crate::geometry::{F64Ext, Transform, Transformation, Vector};
use std::ops::{Add, AddAssign, Sub, SubAssign};
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Point {
pub x: f64,
pub y: f64,
}
impl Point {
pub fn new(x: f64, y: f64) -> Point {
Point { x, y }
}
pub fn origin() -> Point {
Point::new(0.0, 0.0)
}
pub fn to_vector(self) -> Vector {
Vector::new(self.x, self.y)
}
pub fn lerp(self, other: Point, t: f64) -> Point {
Point::new(self.x.ext_lerp(other.x, t), self.y.ext_lerp(other.y, t))
}
}
impl AddAssign<Vector> for Point {
fn add_assign(&mut self, vector: Vector) {
*self = *self + vector;
}
}
impl SubAssign<Vector> for Point {
fn sub_assign(&mut self, vector: Vector) {
*self = *self - vector;
}
}
impl Add<Vector> for Point {
type Output = Point;
fn add(self, v: Vector) -> Point {
Point::new(self.x + v.x, self.y + v.y)
}
}
impl Sub for Point {
type Output = Vector;
fn sub(self, other: Point) -> Vector {
Vector::new(self.x - other.x, self.y - other.y)
}
}
impl Sub<Vector> for Point {
type Output = Point;
fn sub(self, v: Vector) -> Point {
Point::new(self.x - v.x, self.y - v.y)
}
}
impl Transform for Point {
fn transform<T>(self, t: &T) -> Point
where
T: Transformation,
{
t.transform_point(self)
}
fn transform_mut<T>(&mut self, t: &T)
where
T: Transformation,
{
*self = self.transform(t);
}
}