@@ -48,6 +48,29 @@ impl Point {
4848 pub fn min ( & self , other : & Point ) -> Self {
4949 Point :: new ( self . line . min ( other. line ) , self . column . min ( other. column ) )
5050 }
51+
52+ #[ inline]
53+ pub fn is_aligned ( & self , other : & Point ) -> bool {
54+ self . line == other. line || self . column == other. column
55+ }
56+
57+ #[ inline]
58+ pub fn is_between_inclusive ( & self , a : & Point , b : & Point ) -> bool {
59+ if !self . is_aligned ( a) || !self . is_aligned ( b) || !a. is_aligned ( b) {
60+ return false ;
61+ }
62+
63+ let min_line = a. line . min ( b. line ) ;
64+ let max_line = a. line . max ( b. line ) ;
65+ let min_column = a. column . min ( b. column ) ;
66+ let max_column = a. column . max ( b. column ) ;
67+
68+ let is_between_lines = ( min_line..=max_line) . contains ( & self . line ) ;
69+ let is_between_columns = ( min_column..=max_column) . contains ( & self . column ) ;
70+
71+ ( a. line == b. line && a. line == self . line && is_between_columns)
72+ || ( a. column == b. column && a. column == self . column && is_between_lines)
73+ }
5174}
5275
5376impl Hash for Point {
@@ -100,6 +123,49 @@ mod tests {
100123 assert_eq ! ( min. column, 1 ) ;
101124 }
102125
126+ #[ test]
127+ pub fn test_point_is_aligned ( ) {
128+ let point = Point :: new ( 1 , 2 ) ;
129+ let other = Point :: new ( 1 , 3 ) ;
130+ assert ! ( point. is_aligned( & other) ) ;
131+
132+ let point = Point :: new ( 1 , 2 ) ;
133+ let other = Point :: new ( 3 , 1 ) ;
134+ assert ! ( !point. is_aligned( & other) ) ;
135+
136+ let point = Point :: new ( 1 , 2 ) ;
137+ let other = Point :: new ( 1 , 2 ) ;
138+ assert ! ( point. is_aligned( & other) ) ;
139+ }
140+
141+ #[ test]
142+ pub fn test_point_is_between_inclusive ( ) {
143+ let point = Point :: new ( 1 , 2 ) ;
144+ let a = Point :: new ( 0 , 2 ) ;
145+ let b = Point :: new ( 2 , 2 ) ;
146+ assert ! ( point. is_between_inclusive( & a, & b) ) ;
147+
148+ let point = Point :: new ( 1 , 2 ) ;
149+ let a = Point :: new ( 1 , 0 ) ;
150+ let b = Point :: new ( 1 , 4 ) ;
151+ assert ! ( point. is_between_inclusive( & a, & b) ) ;
152+
153+ let point = Point :: new ( 1 , 2 ) ;
154+ let a = Point :: new ( 0 , 1 ) ;
155+ let b = Point :: new ( 2 , 3 ) ;
156+ assert ! ( !point. is_between_inclusive( & a, & b) ) ;
157+
158+ let point = Point :: new ( 1 , 2 ) ;
159+ let a = Point :: new ( 1 , 2 ) ;
160+ let b = Point :: new ( 2 , 3 ) ;
161+ assert ! ( !point. is_between_inclusive( & a, & b) ) ;
162+
163+ let point = Point :: new ( 1 , 2 ) ;
164+ let a = Point :: new ( 2 , 1 ) ;
165+ let b = Point :: new ( 3 , 2 ) ;
166+ assert ! ( !point. is_between_inclusive( & a, & b) ) ;
167+ }
168+
103169 // #[test]
104170 // pub fn test_infinite_grid_to_real_grid() {
105171 // let point = Point::new(45, 20);
0 commit comments