11use crate :: utils:: orientation:: Orientation ;
22use crate :: utils:: point:: Point ;
3+ use crate :: utils:: range:: Range ;
34
45#[ derive( Debug , Copy , Clone , PartialEq ) ]
56pub struct Line {
@@ -47,6 +48,7 @@ impl Line {
4748 Some ( Point :: new ( x as isize , y as isize ) )
4849 }
4950
51+ // todo extract line that can be only horizontal and vertical
5052 fn orientation ( & self ) -> Option < Orientation > {
5153 let a = self . start ;
5254 let b = self . end ;
@@ -70,23 +72,14 @@ impl Line {
7072 let end = self . end ;
7173
7274 return match orientation {
73- Orientation :: Horizontal => {
74- let mut points = Vec :: new ( ) ;
75- // todo range from unordered ?
76- for x in start. x . min ( end. x ) ..=start. x . max ( end. x ) {
77- points. push ( Point :: new ( x, start. y ) ) ;
78- }
79-
80- points
81- }
82- Orientation :: Vertical => {
83- let mut points = Vec :: new ( ) ;
84- for y in start. y . min ( end. y ) ..=start. y . max ( end. y ) {
85- points. push ( Point :: new ( start. x , y) ) ;
86- }
87-
88- points
89- }
75+ Orientation :: Horizontal => Range :: from_unordered ( start. x , end. x )
76+ . iter ( )
77+ . map ( |x| Point :: new ( x, start. y ) )
78+ . collect ( ) ,
79+ Orientation :: Vertical => Range :: from_unordered ( start. y , end. y )
80+ . iter ( )
81+ . map ( |y| Point :: new ( start. x , y) )
82+ . collect ( ) ,
9083 } ;
9184 }
9285
0 commit comments