Skip to content

Commit 335b722

Browse files
nes1983dsymonds
authored andcommitted
s2: Add Rect.ApproxEqual.
Signed-off-by: David Symonds <dsymonds@golang.org>
1 parent 644846b commit 335b722

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

s2/rect.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,12 @@ func (r Rect) HausdorffDistance(other Rect) s1.Angle {
506506
other.DirectedHausdorffDistance(r))
507507
}
508508

509+
// ApproxEqual reports whether the latitude and longitude intervals of the two rectangles
510+
// are the same up to a small tolerance.
511+
func (r Rect) ApproxEqual(other Rect) bool {
512+
return r.Lat.ApproxEqual(other.Lat) && r.Lng.ApproxEqual(other.Lng)
513+
}
514+
509515
// directedHausdorffDistance returns the directed Hausdorff distance
510516
// from one longitudinal edge spanning latitude range 'a' to the other
511517
// longitudinal edge spanning latitude range 'b', with their longitudinal

s2/rect_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,3 +1107,25 @@ func TestDirectedHausdorffDistanceRectToRectDegenerateCases(t *testing.T) {
11071107
verifyDirectedHausdorffDistance(t,
11081108
rectFromDegrees(-20, 95, 20, 105), rectFromDegrees(-30, 5, 30, 15))
11091109
}
1110+
1111+
func TestApproxEqual(t *testing.T) {
1112+
// s1.Interval and r1.Interval have additional testing.
1113+
1114+
const ε = epsilon / 10
1115+
tests := []struct {
1116+
a, b Rect
1117+
want bool
1118+
}{
1119+
{EmptyRect(), rectFromDegrees(1, 5, 1, 5), true},
1120+
{rectFromDegrees(1, 5, 1, 5), EmptyRect(), true},
1121+
1122+
{rectFromDegrees(1, 5, 1, 5), rectFromDegrees(2, 7, 2, 7), false},
1123+
{rectFromDegrees(1, 5, 1, 5), rectFromDegrees(1+ε, 5+ε, 1+ε, 5+ε), true},
1124+
}
1125+
1126+
for _, test := range tests {
1127+
if got := test.a.ApproxEqual(test.b); got != test.want {
1128+
t.Errorf("%v.ApproxEquals(%v) = %t, want %t", test.a, test.b, got, test.want)
1129+
}
1130+
}
1131+
}

0 commit comments

Comments
 (0)