Skip to content

Commit

Permalink
Improve intersection precision to reduce false positives. (akavel#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanful authored May 17, 2019
1 parent 584bdbb commit b96f58c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
49 changes: 49 additions & 0 deletions bugs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,55 @@ func TestResweepingIntersectingEndpoints(t *T) {
}.verify(t)
}

func TestIntersectionFalsePositives(t *T) {
testCases{
{
op: polyclip.INTERSECTION,
subject: polyclip.Polygon{{
{100.0000001, 100},
{98.48077539970159, 117.36481778405785},
{98.48077539970159, 82.63518221594214},
}},
clipping: polyclip.Polygon{{
{100.0000001, 100},
{100, 99.99999885699484},
{99.9999999, 100.00000000000001},
{100, 100.00000114300516},
{100.0000001, 100},
}},
result: polyclip.Polygon{{
{100.0000001, 100},
{100, 99.99999885699484},
{99.9999999, 100.00000000000001},
{100, 100.00000114300516},
{100.0000001, 100},
}},
},
{
op: polyclip.INTERSECTION,
subject: polyclip.Polygon{{
{100.00000001, 100},
{98.48077531106888, 117.36481776842952},
{98.48077531106888, 82.63518223157048},
}},
clipping: polyclip.Polygon{{
{100.00000001, 100},
{100, 99.99999988569955},
{99.99999999, 100.00000000000001},
{100, 100.00000011430046},
{100.00000001, 100},
}},
result: polyclip.Polygon{{
{100.00000001, 100},
{100, 99.99999988569955},
{99.99999999, 100.00000000000001},
{100, 100.00000011430046},
{100.00000001, 100},
}},
},
}.verify(t)
}

func TestCorruptionResistanceFromFloatingPointImprecision(t *T) {
testCases{
{
Expand Down
2 changes: 1 addition & 1 deletion clipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func findIntersection(seg0, seg1 segment, tryBothDirections bool) (int, Point, P
d0 := Point{seg0.end.X - p0.X, seg0.end.Y - p0.Y}
p1 := seg1.start
d1 := Point{seg1.end.X - p1.X, seg1.end.Y - p1.Y}
sqrEpsilon := 1e-15 // was originally 1e-3, which is very prone to false positives
sqrEpsilon := 1e-21 // was originally 1e-3, which is very prone to false positives
E := Point{p1.X - p0.X, p1.Y - p0.Y}
kross := d0.X*d1.Y - d0.Y*d1.X
sqrKross := kross * kross
Expand Down

0 comments on commit b96f58c

Please sign in to comment.