1
- use geo_types:: { Line , CoordFloat } ;
1
+ use geo_types:: { CoordFloat , Line } ;
2
2
3
- use super :: {
4
- LineSplitResult ,
5
- LineSplit ,
6
- } ;
3
+ use super :: { LineSplit , LineSplitResult } ;
7
4
8
- impl < Scalar > LineSplit < Scalar > for Line < Scalar > where Scalar : CoordFloat {
5
+ impl < Scalar > LineSplit < Scalar > for Line < Scalar >
6
+ where
7
+ Scalar : CoordFloat ,
8
+ {
9
9
fn line_split ( & self , fraction : Scalar ) -> Option < LineSplitResult < Self > > {
10
10
if fraction. is_nan ( ) {
11
- return None
11
+ return None ;
12
12
}
13
13
if fraction <= Scalar :: zero ( ) {
14
- Some ( LineSplitResult :: Second ( self . clone ( ) ) )
14
+ Some ( LineSplitResult :: Second ( * self ) )
15
15
} else if fraction >= Scalar :: one ( ) {
16
- Some ( LineSplitResult :: First ( self . clone ( ) ) )
16
+ Some ( LineSplitResult :: First ( * self ) )
17
17
} else {
18
18
let new_midpoint = self . start + self . delta ( ) * fraction;
19
19
Some ( LineSplitResult :: FirstSecond (
@@ -25,10 +25,10 @@ impl<Scalar> LineSplit<Scalar> for Line<Scalar> where Scalar: CoordFloat {
25
25
}
26
26
27
27
#[ cfg( test) ]
28
- mod test{
29
- use geo_types:: coord;
28
+ mod test {
30
29
use super :: super :: LineSplitTwiceResult ;
31
30
use super :: * ;
31
+ use geo_types:: coord;
32
32
33
33
// =============================================================================================
34
34
// Line::line_split()
@@ -38,71 +38,71 @@ mod test{
38
38
fn test_line_split_first_second ( ) {
39
39
// simple x-axis aligned check
40
40
let line = Line :: new (
41
- coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
42
- coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
41
+ coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
42
+ coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
43
43
) ;
44
44
let result = line. line_split ( 0.6 ) ;
45
- assert_eq ! ( result, Some ( LineSplitResult :: FirstSecond (
46
- Line :: new(
47
- coord!{ x: 0.0_f32 , y: 0.0_f32 } ,
48
- coord!{ x: 6.0_f32 , y: 0.0_f32 } ,
49
- ) ,
50
- Line :: new(
51
- coord!{ x: 6.0_f32 , y: 0.0_f32 } ,
52
- coord!{ x: 10.0_f32 , y: 0.0_f32 } ,
53
- )
54
- ) ) ) ;
45
+ assert_eq ! (
46
+ result,
47
+ Some ( LineSplitResult :: FirstSecond (
48
+ Line :: new(
49
+ coord! { x: 0.0_f32 , y: 0.0_f32 } ,
50
+ coord! { x: 6.0_f32 , y: 0.0_f32 } ,
51
+ ) ,
52
+ Line :: new(
53
+ coord! { x: 6.0_f32 , y: 0.0_f32 } ,
54
+ coord! { x: 10.0_f32 , y: 0.0_f32 } ,
55
+ )
56
+ ) )
57
+ ) ;
55
58
56
59
// simple y-axis aligned check
57
60
let line = Line :: new (
58
- coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
59
- coord ! { x: 0.0_f32 , y: 10.0_f32 } ,
61
+ coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
62
+ coord ! { x: 0.0_f32 , y: 10.0_f32 } ,
60
63
) ;
61
64
let result = line. line_split ( 0.3 ) ;
62
- assert_eq ! ( result , Some ( LineSplitResult :: FirstSecond (
63
- Line :: new (
64
- coord! { x : 0.0_f32 , y : 0.0_f32 } ,
65
- coord!{ x: 0.0_f32 , y: 3.0_f32 } ,
66
- ) ,
67
- Line :: new (
68
- coord!{ x: 0.0_f32 , y: 3 .0_f32} ,
69
- coord! { x : 0.0_f32 , y : 10.0_f32 } ,
70
- )
71
- ) ) ) ;
65
+ assert_eq ! (
66
+ result ,
67
+ Some ( LineSplitResult :: FirstSecond (
68
+ Line :: new ( coord! { x: 0.0_f32 , y: 0.0_f32 } , coord! { x : 0.0_f32 , y : 3.0_f32 } , ) ,
69
+ Line :: new (
70
+ coord! { x : 0.0_f32 , y : 3.0_f32 } ,
71
+ coord! { x: 0.0_f32 , y: 10 .0_f32} ,
72
+ )
73
+ ) )
74
+ ) ;
72
75
73
76
// non_trivial check
74
77
let line = Line :: new (
75
- coord ! { x: 1.0_f32 , y: 1.0_f32 } ,
76
- coord ! { x: 10.0_f32 , y: -10.0_f32 } ,
78
+ coord ! { x: 1.0_f32 , y: 1.0_f32 } ,
79
+ coord ! { x: 10.0_f32 , y: -10.0_f32 } ,
77
80
) ;
78
81
let split_point = line. start + line. delta ( ) * 0.7 ;
79
82
let result = line. line_split ( 0.7 ) ;
80
- assert_eq ! ( result, Some ( LineSplitResult :: FirstSecond (
81
- Line :: new(
82
- line. start,
83
- split_point,
84
- ) ,
85
- Line :: new(
86
- split_point,
87
- line. end,
88
- )
89
- ) ) ) ;
83
+ assert_eq ! (
84
+ result,
85
+ Some ( LineSplitResult :: FirstSecond (
86
+ Line :: new( line. start, split_point, ) ,
87
+ Line :: new( split_point, line. end, )
88
+ ) )
89
+ ) ;
90
90
}
91
91
92
92
#[ test]
93
93
fn test_line_split_first ( ) {
94
94
// test one
95
95
let line = Line :: new (
96
- coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
97
- coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
96
+ coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
97
+ coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
98
98
) ;
99
99
let result = line. line_split ( 1.0 ) ;
100
100
assert_eq ! ( result, Some ( LineSplitResult :: First ( line) ) ) ;
101
101
102
102
// Test numbers larger than one
103
103
let line = Line :: new (
104
- coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
105
- coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
104
+ coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
105
+ coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
106
106
) ;
107
107
let result = line. line_split ( 2.0 ) ;
108
108
assert_eq ! ( result, Some ( LineSplitResult :: First ( line) ) ) ;
@@ -111,26 +111,25 @@ mod test{
111
111
fn test_line_split_second ( ) {
112
112
// test zero
113
113
let line = Line :: new (
114
- coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
115
- coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
114
+ coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
115
+ coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
116
116
) ;
117
117
let result = line. line_split ( 0.0 ) ;
118
118
assert_eq ! ( result, Some ( LineSplitResult :: Second ( line) ) ) ;
119
119
120
120
// Test negative numbers
121
121
let line = Line :: new (
122
- coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
123
- coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
122
+ coord ! { x: 0.0_f32 , y: 0.0_f32 } ,
123
+ coord ! { x: 10.0_f32 , y: 0.0_f32 } ,
124
124
) ;
125
125
let result = line. line_split ( -2.0 ) ;
126
126
assert_eq ! ( result, Some ( LineSplitResult :: Second ( line) ) ) ;
127
127
}
128
128
129
-
130
129
// =============================================================================================
131
130
// Line::line_split_twice()
132
131
// =============================================================================================
133
-
132
+
134
133
macro_rules! test_line_split_twice_helper{
135
134
( $a: expr, $b: expr, $enum_variant: ident, $( ( $x1: expr, $x2: expr) ) ,* ) =>{ {
136
135
let line = Line :: new(
@@ -154,14 +153,20 @@ mod test{
154
153
}
155
154
156
155
#[ test]
157
- fn test_line_split_twice ( ) {
158
- test_line_split_twice_helper ! ( 0.6 , 0.8 , FirstSecondThird , ( 0.0 , 6.0 ) , ( 6.0 , 8.0 ) , ( 8.0 , 10.0 ) ) ;
156
+ fn test_line_split_twice ( ) {
157
+ test_line_split_twice_helper ! (
158
+ 0.6 ,
159
+ 0.8 ,
160
+ FirstSecondThird ,
161
+ ( 0.0 , 6.0 ) ,
162
+ ( 6.0 , 8.0 ) ,
163
+ ( 8.0 , 10.0 )
164
+ ) ;
159
165
test_line_split_twice_helper ! ( 0.6 , 1.0 , FirstSecond , ( 0.0 , 6.0 ) , ( 6.0 , 10.0 ) ) ;
160
166
test_line_split_twice_helper ! ( 0.6 , 0.6 , FirstThird , ( 0.0 , 6.0 ) , ( 6.0 , 10.0 ) ) ;
161
167
test_line_split_twice_helper ! ( 0.0 , 0.6 , SecondThird , ( 0.0 , 6.0 ) , ( 6.0 , 10.0 ) ) ;
162
168
test_line_split_twice_helper ! ( 1.0 , 1.0 , First , ( 0.0 , 10.0 ) ) ;
163
169
test_line_split_twice_helper ! ( 0.0 , 1.0 , Second , ( 0.0 , 10.0 ) ) ;
164
170
test_line_split_twice_helper ! ( 0.0 , 0.0 , Third , ( 0.0 , 10.0 ) ) ;
165
171
}
166
-
167
- }
172
+ }
0 commit comments