Polygon_closest_distance_line()'s fallback loop (used whenever the
query line does not cross the polygon) iterated i=0..N-2 and never
tested the closing edge V[N-1]->V[0]. Every other polygon helper in
this file (Polygon_outside, Polygon_intersects) and the sibling
Polygon_closest_distance_point() already wrap around to include this
edge; this function was the one left behind.
The only caller, AP_OABendyRuler::calc_margin_from_inclusion_and_exclusion_polygons(),
passes in AC_Fence polygons in their raw unclosed form (no duplicated
closing vertex), so in real fences this is a genuine edge that was
silently skipped, causing the reported clearance/margin to the fence
boundary to be overestimated near that edge.
Fix mirrors the existing wrap-around pattern from Polygon_intersects()
a few lines above in the same file. Added a regression test using a
simple square fence to confirm the closing edge is now considered and
that behaviour near unaffected edges is unchanged.
Assisted-by: Claude:claude-sonnet-5
This routine was entemplatificated from the integer version, which was
designed to be perfect across representable ranges of points. The
floating point version suffered from a rounding problem.
using sign checking and 64 bit integer math only when needed results
in an algorithm that is just as fast as the floating point version,
but has perfect results for any representable lat/lng