Skip to content

Commit

Permalink
fix a bug in Contour.Contains
Browse files Browse the repository at this point in the history
  • Loading branch information
akavel committed Apr 20, 2015
1 parent 9e6ed32 commit 55bd550
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,15 @@ func (c Contour) Contains(p Point) bool {
}
next := c[ii]

if (p.Y >= next.Y || p.Y <= curr.Y) &&
(p.Y >= curr.Y || p.Y <= next.Y) {
// Is the point out of the edge's bounding box?
// bottom vertex is inclusive (belongs to edge), top vertex is
// exclusive (not part of edge) -- i.e. p lies "slightly above
// the ray"
bottom, top := curr, next
if bottom.Y > top.Y {
bottom, top = top, bottom
}
if p.Y < bottom.Y || p.Y >= top.Y {
continue
}
// Edge is from curr to next.
Expand Down
21 changes: 21 additions & 0 deletions geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,27 @@ func TestContourContains(t *T) {
}
}

func TestContourContains2(t *T) {
c1 := Contour{{55, 35}, {25, 35}, {25, 119}, {55, 119}}
c2 := Contour{{145, 35}, {145, 77}, {105, 77}, {105, 119}, {55, 119}, {55, 35}}
cases := []struct {
Contour
Point
Result bool
}{
{c1, Point{54.95, 77}, true},
{c1, Point{55.05, 77}, false},
{c2, Point{54.95, 77}, false},
{c2, Point{55.05, 77}, true},
}
for _, c := range cases {
result := c.Contour.Contains(c.Point)
if result != c.Result {
t.Errorf("case %v expected %v, got %v", c, c.Result, result)
}
}
}

func ExamplePolygon_Construct() {
subject := Polygon{{{1, 1}, {1, 2}, {2, 2}, {2, 1}}} // small square
clipping := Polygon{{{0, 0}, {0, 3}, {3, 0}}} // overlapping triangle
Expand Down

0 comments on commit 55bd550

Please sign in to comment.