Skip to content

Commit

Permalink
Merge 14b7ca8 into 7c7cb33
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Dec 1, 2021
2 parents 7c7cb33 + 14b7ca8 commit 81b5571
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Eliminate redundant calls to the optional user supplied transform func during
area calculations.

## v0.35.0

2021-11-23
Expand Down
12 changes: 12 additions & 0 deletions geom/attr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,18 @@ func TestTransformedArea(t *testing.T) {
}
}

func TestTransformedAreaInvocationCount(t *testing.T) {
g := geomFromWKT(t, "POLYGON((0 0,0 1,1 0,0 0))")
var count int
g.Area(WithTransform(func(xy XY) XY {
count++
return xy
}))

// Each of the 4 points making up the polygon get transformed once each.
expectIntEq(t, count, 4)
}

func TestCentroid(t *testing.T) {
for i, tt := range []struct {
input string
Expand Down
19 changes: 14 additions & 5 deletions geom/type_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,22 @@ func signedAreaOfLinearRing(lr LineString, transform func(XY) XY) float64 {
var sum float64
seq := lr.Coordinates()
n := seq.Length()
for i := 0; i < n; i++ {
pt0 := seq.GetXY(i)
pt1 := seq.GetXY((i + 1) % n)
if n == 0 {
return 0
}

nthPt := func(i int) XY {
pt := seq.GetXY(i)
if transform != nil {
pt0 = transform(pt0)
pt1 = transform(pt1)
pt = transform(pt)
}
return pt
}

pt1 := nthPt(0)
for i := 0; i < n-1; i++ {
pt0 := pt1
pt1 = nthPt(i + 1)
sum += (pt1.X + pt0.X) * (pt1.Y - pt0.Y)
}
return sum / 2
Expand Down

0 comments on commit 81b5571

Please sign in to comment.