Skip to content

Commit 31e3227

Browse files
authored
Merge pull request #70 from missinglink/ring_closed
ring: require >=4 points to return true when calling Closed()
2 parents ba1cf20 + 896e5d8 commit 31e3227

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

ring.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ func (r Ring) Dimensions() int {
1717
// ie. 4+ points and the first and last points match.
1818
// NOTE: this will not check for self-intersection.
1919
func (r Ring) Closed() bool {
20-
// first must equal last
21-
return r[0] == r[len(r)-1]
20+
return (len(r) >= 4) && (r[0] == r[len(r)-1])
2221
}
2322

2423
// Reverse changes the direction of the ring.

ring_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,28 @@ func TestRing_Closed(t *testing.T) {
1717
},
1818
{
1919
name: "not closed if last point does not match",
20-
ring: Ring{{0, 0}, {3, 0}, {3, 4}},
20+
ring: Ring{{0, 0}, {3, 0}, {3, 3}, {3, 4}},
2121
closed: false,
2222
},
2323
{
24-
name: "length of ring doesn't matter",
24+
name: "empty ring",
25+
ring: Ring{},
26+
closed: false,
27+
},
28+
{
29+
name: "one vertex ring",
30+
ring: Ring{{3, 0}},
31+
closed: false,
32+
},
33+
{
34+
name: "two vertex ring",
2535
ring: Ring{{3, 0}, {3, 0}},
26-
closed: true,
36+
closed: false,
37+
},
38+
{
39+
name: "three vertex ring",
40+
ring: Ring{{3, 0}, {0, 0}, {3, 0}},
41+
closed: false,
2742
},
2843
}
2944

0 commit comments

Comments
 (0)