Skip to content

Commit 63126ba

Browse files
committed
extend point_in_polygon tolerance to horizontal edges
The on-horizontal-edge check compared y to the edge's y exactly, so points near a horizontal edge never reached the tolerant cross-product branch. Use the same |c| <= tol criterion there.
1 parent 666437c commit 63126ba

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

autotest/TestGeomUtil.f90

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,29 @@ subroutine test_point_in_polygon_tol(error)
351351
"clearly-outside point wrongly accepted with tolerance")
352352
if (allocated(error)) return
353353

354+
! the same checks, repeated for a point near a horizontal edge
355+
! (top edge, ya == yb) rather than a vertical one, since the
356+
! on-edge check is derived separately for horizontal edges
357+
xoff = xll + 5.0_DP
358+
yoff = yll + dy + 4.0_DP * spacing(yll + dy)
359+
360+
call check(error,.not. point_in_polygon(xoff, yoff, poly), &
361+
"near-horizontal-edge point wrongly accepted " &
362+
//"without tolerance")
363+
if (allocated(error)) return
364+
365+
call check(error, point_in_polygon(xoff, yoff, poly, tol), &
366+
"near-horizontal-edge point wrongly rejected " &
367+
//"with tolerance")
368+
if (allocated(error)) return
369+
370+
call check(error, &
371+
.not. point_in_polygon(xoff, yll + dy + 0.001_DP, &
372+
poly, tol), &
373+
"clearly-outside point wrongly accepted with tolerance " &
374+
//"(horizontal edge)")
375+
if (allocated(error)) return
376+
354377
deallocate (poly)
355378
end subroutine test_point_in_polygon_tol
356379

src/Utilities/GeomUtil.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ logical function point_in_polygon(x, y, poly, tol)
5757
point_in_polygon = .true.
5858
exit
5959
else if (ya == yb .and. &
60-
y == ya .and. &
60+
abs(y - ya) * abs(xb - xa) <= ltol .and. &
6161
between(x, xa, xb)) then
62-
! on horizontal edge
62+
! on (or within tol of) horizontal edge
6363
point_in_polygon = .true.
6464
exit
6565
else if (between(y, ya, yb)) then

0 commit comments

Comments
 (0)