@@ -263,28 +263,30 @@ bool covered_by(const alt::Point2d & point, const alt::ConvexPolygon2d & poly)
263
263
return false ;
264
264
}
265
265
266
- double cross;
267
266
for (auto it = vertices.cbegin (); it != std::prev (vertices.cend ()); ++it) {
268
267
const auto & p1 = *it;
269
268
const auto & p2 = *std::next (it);
270
269
271
- if (p1.y () <= point.y () && p2.y () >= point.y ()) { // upward edge
272
- cross = (p2 - p1).cross (point - p1);
273
- if (cross > 0 ) { // point is to the left of edge
274
- winding_number++;
275
- continue ;
276
- }
277
- } else if (p1.y () >= point.y () && p2.y () <= point.y ()) { // downward edge
278
- cross = (p2 - p1).cross (point - p1);
279
- if (cross < 0 ) { // point is to the left of edge
280
- winding_number--;
281
- continue ;
282
- }
283
- } else {
270
+ const auto is_upward_edge = p1.y () <= point.y () && p2.y () >= point.y ();
271
+ const auto is_downward_edge = p1.y () >= point.y () && p2.y () <= point.y ();
272
+
273
+ if (!is_upward_edge && !is_downward_edge) {
274
+ continue ;
275
+ }
276
+
277
+ const auto start_vec = point - p1;
278
+ const auto end_vec = point - p2;
279
+ const auto cross = start_vec.cross (end_vec);
280
+
281
+ if (is_upward_edge && cross > 0 ) { // point is to the left of edge
282
+ winding_number++;
283
+ continue ;
284
+ } else if (is_downward_edge && cross < 0 ) { // point is to the left of edge
285
+ winding_number--;
284
286
continue ;
285
287
}
286
288
287
- if (std::abs (cross) < epsilon) { // point is on edge
289
+ if (std::abs (cross) < epsilon && start_vec. dot (end_vec) <= 0 . ) { // point is on edge
288
290
return true ;
289
291
}
290
292
}
@@ -600,28 +602,30 @@ bool within(const alt::Point2d & point, const alt::ConvexPolygon2d & poly)
600
602
return false ;
601
603
}
602
604
603
- double cross;
604
605
for (auto it = vertices.cbegin (); it != std::prev (vertices.cend ()); ++it) {
605
606
const auto & p1 = *it;
606
607
const auto & p2 = *std::next (it);
607
608
608
- if (p1.y () < point.y () && p2.y () > point.y ()) { // upward edge
609
- cross = (p2 - p1).cross (point - p1);
610
- if (cross > 0 ) { // point is to the left of edge
611
- winding_number++;
612
- continue ;
613
- }
614
- } else if (p1.y () > point.y () && p2.y () < point.y ()) { // downward edge
615
- cross = (p2 - p1).cross (point - p1);
616
- if (cross < 0 ) { // point is to the left of edge
617
- winding_number--;
618
- continue ;
619
- }
620
- } else {
609
+ const auto is_upward_edge = p1.y () < point.y () && p2.y () > point.y ();
610
+ const auto is_downward_edge = p1.y () > point.y () && p2.y () < point.y ();
611
+
612
+ if (!is_upward_edge && !is_downward_edge) {
613
+ continue ;
614
+ }
615
+
616
+ const auto start_vec = point - p1;
617
+ const auto end_vec = point - p2;
618
+ const auto cross = start_vec.cross (end_vec);
619
+
620
+ if (is_upward_edge && cross > 0 ) { // point is to the left of edge
621
+ winding_number++;
622
+ continue ;
623
+ } else if (is_downward_edge && cross < 0 ) { // point is to the left of edge
624
+ winding_number--;
621
625
continue ;
622
626
}
623
627
624
- if (std::abs (cross) < epsilon) { // point is on edge
628
+ if (std::abs (cross) < epsilon && start_vec. dot (end_vec) <= 0 . ) { // point is on edge
625
629
return false ;
626
630
}
627
631
}
0 commit comments