Skip to content

Commit

Permalink
Fix calculation of "inout" fields for overlapping segments (#12) (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanful authored Oct 16, 2020
1 parent 2989974 commit a2ddce8
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 3 deletions.
141 changes: 139 additions & 2 deletions bugs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ func TestIntersectionFalsePositives(t *T) {
clipping: polyclip.Polygon{{
{100.00000001, 100},
{100, 99.99999988569955},
{99.99999999, 100.00000000000001},
{99.99999998, 100.00000000000001},
{100, 100.00000011430046},
{100.00000001, 100},
}},
result: polyclip.Polygon{{
{100.00000001, 100},
{100, 99.99999988569955},
{99.99999999, 100.00000000000001},
{99.99999998, 100.00000000000001},
{100, 100.00000011430046},
{100.00000001, 100},
}},
Expand Down Expand Up @@ -573,6 +573,143 @@ func TestSelfIntersectionAvoidance(t *T) {
}.verify(t)
}

func TestOverlappingSegments(t *T) {
testCases{
{
// ----------
// | /
// | /
// | /
// | /
// | /|
// | /_|
// | /
// | /
// |/
op: polyclip.UNION,
subject: polyclip.Polygon{{
{0, 0},
{10, 0},
{0, 10},
}},
clipping: polyclip.Polygon{{
{6, 5},
{6, 4},
{5, 5},
}},
result: polyclip.Polygon{{
{0, 0},
{10, 0},
{6, 4},
{6, 5},
{5, 5},
{0, 10},
}},
},
{
// ----------
// | /
// | /
// | /
// | __/
// | | /
// | |/
// | /
// | /
// |/
op: polyclip.UNION,
subject: polyclip.Polygon{{
{0, 0},
{10, 0},
{0, 10},
}},
clipping: polyclip.Polygon{{
{6, 4},
{4, 5},
{5, 5},
}},
result: polyclip.Polygon{{
{0, 0},
{10, 0},
{6, 4},
{5, 5},
{0, 10},
}},
},
{
// |\
// | \
// | \__
// | \ |
// | \|
// | \
// | \
// | \
// ----------
op: polyclip.UNION,
subject: polyclip.Polygon{{
{0, 10},
{10, 10},
{0, 0},
}},
clipping: polyclip.Polygon{{
{6, 5},
{6, 6},
{5, 5},
}},
result: polyclip.Polygon{{
{0, 0},
{5, 5},
{6, 5},
{6, 6},
{10, 10},
{0, 10},
}},
},
{
// |\
// | \
// | \
// | |\
// | |_\
// | \
// | \
// | \
// ----------
op: polyclip.UNION,
subject: polyclip.Polygon{{
{0, 10},
{10, 10},
{0, 0},
}},
clipping: polyclip.Polygon{{
{5, 5},
{5, 6},
{6, 6},
}},
result: polyclip.Polygon{{
{0, 0},
{5, 5},
{6, 6},
{10, 10},
{0, 10},
}},
},
{
op: polyclip.UNION,
subject: polyclip.Polygon{{
{40131, 8372}, {40127, 8374}, {40126, 8375}, {40125, 8375}, {40122, 8377}, {40143, 8369},
}},
clipping: polyclip.Polygon{{
{40106, 8376}, {40128, 8373}, {40126, 8375},
}},
result: polyclip.Polygon{{
{40106, 8376}, {40128, 8373}, {40127, 8374}, {40131, 8372}, {40143, 8369}, {40122, 8377}, {40124.91891891892, 8375.054054054053},
}},
},
}.verify(t)
}

func TestNonReductiveSegmentDivisions(t *T) {
if Short() {
return
Expand Down
10 changes: 9 additions & 1 deletion clipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,15 @@ func (c *clipper) compute(operation Op) Polygon {
} else {
e.inout = true
}
} else { // the previous two line segments in S are overlapping line segments
} else if e.segmentsEqual(prev) {
// The second of two overlapping line segments. Determine the flags from the edge types.
if e.edgeType == _EDGE_SAME_TRANSITION || prev.edgeType == _EDGE_SAME_TRANSITION {
e.inout = prev.inout
} else {
e.inout = !prev.inout
}
} else {
// The previous two line segments in S are overlapping line segments
prevTwo := S[pos-2]
if prev.polygonType == e.polygonType {
e.inout = !prev.inout
Expand Down
4 changes: 4 additions & 0 deletions endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func (se *endpoint) segment() segment {
return segment{se.p, se.other.p}
}

func (e1 *endpoint) segmentsEqual(e2 *endpoint) bool {
return e1.segment() == e2.segment()
}

func signedArea(p0, p1, p2 Point) float64 {
return (p0.X-p2.X)*(p1.Y-p2.Y) -
(p1.X-p2.X)*(p0.Y-p2.Y)
Expand Down

0 comments on commit a2ddce8

Please sign in to comment.