diff --git a/flat.go b/flat.go index 995070c2..5081437d 100644 --- a/flat.go +++ b/flat.go @@ -265,13 +265,8 @@ func doubleArea1(flatCoords []float64, offset, end, stride int) float64 { func doubleArea2(flatCoords []float64, offset int, ends []int, stride int) float64 { var doubleArea float64 - for i, end := range ends { - da := doubleArea1(flatCoords, offset, end, stride) - if i == 0 { - doubleArea = da - } else { - doubleArea -= da - } + for _, end := range ends { + doubleArea += doubleArea1(flatCoords, offset, end, stride) offset = end } return doubleArea diff --git a/geom_test.go b/geom_test.go index cd714592..506358e0 100644 --- a/geom_test.go +++ b/geom_test.go @@ -38,7 +38,7 @@ func aliases(x, y []float64) bool { } func TestArea(t *testing.T) { - for _, tc := range []struct { + for i, tc := range []struct { g interface { Area() float64 } @@ -115,7 +115,7 @@ func TestArea(t *testing.T) { { g: NewPolygon(XY).MustSetCoords([][]Coord{ {{-3, -2}, {-1, 4}, {6, 1}, {3, 10}, {-4, 9}, {-3, -2}}, - {{0, 6}, {2, 6}, {2, 8}, {0, 8}, {0, 6}}, + {{0, 6}, {0, 8}, {2, 8}, {2, 6}, {0, 6}}, }), want: 56, }, @@ -151,7 +151,7 @@ func TestArea(t *testing.T) { g: NewMultiPolygon(XY).MustSetCoords([][][]Coord{ { {{-3, -2}, {-1, 4}, {6, 1}, {3, 10}, {-4, 9}, {-3, -2}}, - {{0, 6}, {2, 6}, {2, 8}, {0, 8}, {0, 6}}, + {{0, 6}, {0, 8}, {2, 8}, {2, 6}, {0, 6}}, }, }), want: 56, @@ -163,13 +163,28 @@ func TestArea(t *testing.T) { }, { {{-3, -2}, {-1, 4}, {6, 1}, {3, 10}, {-4, 9}, {-3, -2}}, - {{0, 6}, {2, 6}, {2, 8}, {0, 8}, {0, 6}}, + {{0, 6}, {0, 8}, {2, 8}, {2, 6}, {0, 6}}, }, }), want: 57, }, + { + g: NewPolygon(XY).MustSetCoords([][]Coord{ + {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}, + }), + want: 1, + }, + { + g: NewPolygon(XY).MustSetCoords([][]Coord{ + {{0, 0}, {3, 0}, {3, 3}, {0, 3}, {0, 0}}, + {{1, 1}, {1, 2}, {2, 2}, {2, 1}, {1, 1}}, + }), + want: 8, + }, } { - assert.Equal(t, tc.want, tc.g.Area()) + t.Run(strconv.Itoa(i), func(t *testing.T) { + assert.Equal(t, tc.want, tc.g.Area()) + }) } } diff --git a/polygon_test.go b/polygon_test.go index bba715e5..f00f0fc5 100644 --- a/polygon_test.go +++ b/polygon_test.go @@ -116,3 +116,26 @@ func TestPolygonSetSRID(t *testing.T) { assert.Equal(t, 4326, NewPolygon(NoLayout).SetSRID(4326).SRID()) assert.Equal(t, 4326, Must(SetSRID(NewPolygon(NoLayout), 4326)).SRID()) } + +func TestPolygonArea(t *testing.T) { + for i, tc := range []struct { + polygon *Polygon + expected float64 + }{ + { + polygon: NewPolygon(XY).MustSetCoords([][]Coord{{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}}), + expected: 1, + }, + { + polygon: NewPolygon(XY).MustSetCoords([][]Coord{ + {{0, 0}, {3, 0}, {3, 3}, {0, 3}, {0, 0}}, + {{1, 1}, {1, 2}, {2, 2}, {2, 1}, {1, 1}}, + }), + expected: 8, + }, + } { + t.Run(strconv.Itoa(i), func(t *testing.T) { + assert.Equal(t, tc.expected, tc.polygon.Area()) + }) + } +}