8
8
//! ```
9
9
//! # use aoc::util::point::Point;
10
10
//!
11
- //! let a = Point { x: 1, y: 2 } ;
12
- //! let b = Point { x: 3, y: 4 } ;
11
+ //! let a = Point::new( 1, 2) ;
12
+ //! let b = Point::new( 3, 4) ;
13
13
//! let k = 2;
14
14
//!
15
- //! assert_eq!(a + b, Point { x: 4, y: 6 } );
16
- //! assert_eq!(a - b, Point { x: -2, y: -2 } );
17
- //! assert_eq!(a * k, Point { x: 2, y: 4 } );
15
+ //! assert_eq!(a + b, Point::new( 4, 6) );
16
+ //! assert_eq!(a - b, Point::new( -2, -2) );
17
+ //! assert_eq!(a * k, Point::new( 2, 4) );
18
18
//! ```
19
19
//!
20
20
//! Additionally there are [`clockwise`] and [`counter_clockwise`] functions for 90 degree rotations
26
26
//! [`manhattan`]: Point::manhattan
27
27
//! [`Grid`]: crate::util::grid
28
28
use std:: hash:: { Hash , Hasher } ;
29
- use std:: ops:: { Add , AddAssign , Mul , Sub } ;
29
+ use std:: ops:: { Add , AddAssign , Mul , Sub , SubAssign } ;
30
30
31
- pub const ORIGIN : Point = Point { x : 0 , y : 0 } ;
32
- pub const UP : Point = Point { x : 0 , y : - 1 } ;
33
- pub const DOWN : Point = Point { x : 0 , y : 1 } ;
34
- pub const LEFT : Point = Point { x : -1 , y : 0 } ;
35
- pub const RIGHT : Point = Point { x : 1 , y : 0 } ;
31
+ pub const ORIGIN : Point = Point :: new ( 0 , 0 ) ;
32
+ pub const UP : Point = Point :: new ( 0 , - 1 ) ;
33
+ pub const DOWN : Point = Point :: new ( 0 , 1 ) ;
34
+ pub const LEFT : Point = Point :: new ( -1 , 0 ) ;
35
+ pub const RIGHT : Point = Point :: new ( 1 , 0 ) ;
36
36
pub const ORTHOGONAL : [ Point ; 4 ] = [ UP , DOWN , LEFT , RIGHT ] ;
37
37
38
38
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
@@ -41,46 +41,30 @@ pub struct Point {
41
41
pub y : i32 ,
42
42
}
43
43
44
- impl Add for Point {
45
- type Output = Point ;
46
-
44
+ impl Point {
47
45
#[ inline]
48
- fn add ( self , rhs : Point ) -> Point {
49
- Point { x : self . x + rhs . x , y : self . y + rhs . y }
46
+ pub const fn new ( x : i32 , y : i32 ) -> Self {
47
+ Point { x, y }
50
48
}
51
- }
52
-
53
- impl Sub for Point {
54
- type Output = Point ;
55
49
56
50
#[ inline]
57
- fn sub ( self , rhs : Point ) -> Point {
58
- Point { x : self . x - rhs . x , y : self . y - rhs . y }
51
+ pub fn clockwise ( self ) -> Point {
52
+ Point :: new ( - self . y , self . x )
59
53
}
60
- }
61
-
62
- impl Mul < i32 > for Point {
63
- type Output = Point ;
64
54
65
55
#[ inline]
66
- fn mul ( self , rhs : i32 ) -> Self :: Output {
67
- Point { x : self . x * rhs , y : self . y * rhs }
56
+ pub fn counter_clockwise ( self ) -> Point {
57
+ Point :: new ( self . y , - self . x )
68
58
}
69
- }
70
59
71
- impl AddAssign for Point {
72
60
#[ inline]
73
- fn add_assign ( & mut self , rhs : Point ) {
74
- self . x += rhs. x ;
75
- self . y += rhs. y ;
61
+ pub fn manhattan ( self , other : Point ) -> i32 {
62
+ ( self . x - other. x ) . abs ( ) + ( self . y - other. y ) . abs ( )
76
63
}
77
- }
78
64
79
- impl Hash for Point {
80
65
#[ inline]
81
- fn hash < H : Hasher > ( & self , hasher : & mut H ) {
82
- hasher. write_u32 ( self . x as u32 ) ;
83
- hasher. write_u32 ( self . y as u32 ) ;
66
+ pub fn signum ( self , other : Point ) -> Point {
67
+ Point :: new ( ( self . x - other. x ) . signum ( ) , ( self . y - other. y ) . signum ( ) )
84
68
}
85
69
}
86
70
@@ -97,24 +81,53 @@ impl From<u8> for Point {
97
81
}
98
82
}
99
83
100
- impl Point {
84
+ impl Hash for Point {
101
85
#[ inline]
102
- pub fn clockwise ( self ) -> Point {
103
- Point { x : -self . y , y : self . x }
86
+ fn hash < H : Hasher > ( & self , hasher : & mut H ) {
87
+ hasher. write_u32 ( self . x as u32 ) ;
88
+ hasher. write_u32 ( self . y as u32 ) ;
104
89
}
90
+ }
91
+
92
+ impl Add for Point {
93
+ type Output = Point ;
105
94
106
95
#[ inline]
107
- pub fn counter_clockwise ( self ) -> Point {
108
- Point { x : self . y , y : - self . x }
96
+ fn add ( self , rhs : Point ) -> Point {
97
+ Point :: new ( self . x + rhs . x , self . y + rhs . y )
109
98
}
99
+ }
110
100
101
+ impl AddAssign for Point {
111
102
#[ inline]
112
- pub fn manhattan ( self , other : Point ) -> i32 {
113
- ( self . x - other. x ) . abs ( ) + ( self . y - other. y ) . abs ( )
103
+ fn add_assign ( & mut self , rhs : Point ) {
104
+ self . x += rhs. x ;
105
+ self . y += rhs. y ;
114
106
}
107
+ }
108
+
109
+ impl Mul < i32 > for Point {
110
+ type Output = Point ;
115
111
116
112
#[ inline]
117
- pub fn signum ( self , other : Point ) -> Point {
118
- Point { x : ( self . x - other. x ) . signum ( ) , y : ( self . y - other. y ) . signum ( ) }
113
+ fn mul ( self , rhs : i32 ) -> Self :: Output {
114
+ Point :: new ( self . x * rhs, self . y * rhs)
115
+ }
116
+ }
117
+
118
+ impl Sub for Point {
119
+ type Output = Point ;
120
+
121
+ #[ inline]
122
+ fn sub ( self , rhs : Point ) -> Point {
123
+ Point :: new ( self . x - rhs. x , self . y - rhs. y )
124
+ }
125
+ }
126
+
127
+ impl SubAssign for Point {
128
+ #[ inline]
129
+ fn sub_assign ( & mut self , rhs : Point ) {
130
+ self . x -= rhs. x ;
131
+ self . y -= rhs. y ;
119
132
}
120
133
}
0 commit comments