Skip to content

Commit

Permalink
add some more planar area tests and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Nov 21, 2018
1 parent f3ee61f commit c77a26f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 9 additions & 0 deletions planar/area.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func Area(g orb.Geometry) float64 {

// CentroidArea returns both the centroid and the area in the 2d plane.
// Since the area is need for the centroid, return both.
// Polygon area will always be >= zero. Ring area my be negative if it has
// a clockwise winding orider.
func CentroidArea(g orb.Geometry) (orb.Point, float64) {
if g == nil {
return orb.Point{}, 0
Expand Down Expand Up @@ -188,6 +190,13 @@ func polygonCentroidArea(p orb.Polygon) (orb.Point, float64) {

centroid, area := ringCentroidArea(p[0])
area = math.Abs(area)
if len(p) == 1 {
if area == 0 {
c, _ := lineStringCentroidDist(orb.LineString(p[0]))
return c, 0
}
return centroid, area
}

holeArea := 0.0
weightedHoleCentroid := orb.Point{}
Expand Down
12 changes: 9 additions & 3 deletions planar/area_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,15 @@ func TestCentroid_RingAdv(t *testing.T) {
// | |
// +-----+

centroid, area := CentroidArea(ring)

expected := orb.Point{1.5, 0.45}
if c, _ := CentroidArea(ring); !c.Equal(expected) {
t.Errorf("incorrect centroid: %v != %v", c, expected)
if !centroid.Equal(expected) {
t.Errorf("incorrect centroid: %v != %v", centroid, expected)
}

if area != -2.5 {
t.Errorf("incorrect area: %v != 2.5", area)
}
}

Expand All @@ -249,7 +255,7 @@ func TestCentroidArea_Polygon(t *testing.T) {
}
})

t.Run("no hole", func(t *testing.T) {
t.Run("collapsed", func(t *testing.T) {
e := orb.Point{0.5, 1}
c, _ := CentroidArea(orb.Polygon{{{0, 1}, {1, 1}, {0, 1}}})
if !c.Equal(e) {
Expand Down

0 comments on commit c77a26f

Please sign in to comment.