From 2becc43f5888802695ca2c7857aa1bd0e30da9fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Czapli=C4=B9=E2=80=9Eski?= Date: Wed, 8 Oct 2014 23:12:19 +0200 Subject: [PATCH] added testcase showing issue #3 --- bugs_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 bugs_test.go diff --git a/bugs_test.go b/bugs_test.go new file mode 100644 index 0000000..1d1f727 --- /dev/null +++ b/bugs_test.go @@ -0,0 +1,73 @@ +package polyclip_test + +import ( + "fmt" + "github.com/akavel/polyclip-go" + "sort" + . "testing" +) + +type sorter polyclip.Polygon + +func (s sorter) Len() int { return len(s) } +func (s sorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] } +func (s sorter) Less(i, j int) bool { + if len(s[i]) != len(s[j]) { + return len(s[i]) < len(s[j]) + } + for k := range s[i] { + pi, pj := s[i][k], s[j][k] + if pi.X != pj.X { + return pi.X < pj.X + } + if pi.Y != pj.Y { + return pi.Y < pj.Y + } + } + return false +} + +// basic normalization just for tests; to be improved if needed +func normalize(poly polyclip.Polygon) polyclip.Polygon { + for i, c := range poly { + if len(c) == 0 { + continue + } + + // find bottom-most of leftmost points, to have fixed anchor + min := 0 + for j, p := range c { + if p.X < c[min].X || p.X == c[min].X && p.Y < c[min].Y { + min = j + } + } + + // rotate points to make sure min is first + poly[i] = append(c[min:], c[:min]...) + } + + sort.Sort(sorter(poly)) + return poly +} + +func dump(poly polyclip.Polygon) string { + return fmt.Sprintf("%v", normalize(poly)) +} + +func TestBug3(t *T) { + subject := polyclip.Polygon{{{1, 1}, {1, 2}, {2, 2}, {2, 1}}} + clipping := polyclip.Polygon{ + {{2, 1}, {2, 2}, {3, 2}, {3, 1}}, + {{1, 2}, {1, 3}, {2, 3}, {2, 2}}, + {{2, 2}, {2, 3}, {3, 3}, {3, 2}}} + result := dump(subject.Construct(polyclip.UNION, clipping)) + + exp := dump(polyclip.Polygon{{ + {1, 1}, {2, 1}, {3, 1}, + {3, 2}, {3, 3}, + {2, 3}, {1, 3}, + {1, 2}}}) + if result != exp { + t.Errorf("expected %s, got %s", exp, result) + } +}