Skip to content

Commit 8a663b7

Browse files
Zsolt Palotaidsymonds
Zsolt Palotai
authored andcommitted
s2: Add ContainsLatLng method to Rect.
Signed-off-by: David Symonds <[email protected]>
1 parent d233c21 commit 8a663b7

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

s2/rect.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ func (r Rect) Area() float64 {
9595
return r.Lng.Length() * capDiff
9696
}
9797

98+
// ContainsLatLng reports whether the given LatLng is within the Rect.
99+
func (r Rect) ContainsLatLng(ll LatLng) bool {
100+
if !ll.IsValid() {
101+
return false
102+
}
103+
return r.Lat.Contains(ll.Lat.Radians()) && r.Lng.Contains(ll.Lng.Radians())
104+
}
105+
98106
// AddPoint increases the size of the rectangle to include the given point.
99107
func (r Rect) AddPoint(ll LatLng) Rect {
100108
if !ll.IsValid() {

s2/rect_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,40 @@ func TestAddPoint(t *testing.T) {
157157
}
158158
}
159159

160+
func TestContainsLatLng(t *testing.T) {
161+
tests := []struct {
162+
input Rect
163+
ll LatLng
164+
want bool
165+
}{
166+
{
167+
rectFromDegrees(0, -180, 90, 0),
168+
LatLngFromDegrees(30, -45),
169+
true,
170+
},
171+
{
172+
rectFromDegrees(0, -180, 90, 0),
173+
LatLngFromDegrees(30, 45),
174+
false,
175+
},
176+
{
177+
rectFromDegrees(0, -180, 90, 0),
178+
LatLngFromDegrees(0, -180),
179+
true,
180+
},
181+
{
182+
rectFromDegrees(0, -180, 90, 0),
183+
LatLngFromDegrees(90, 0),
184+
true,
185+
},
186+
}
187+
for _, test := range tests {
188+
if got, want := test.input.ContainsLatLng(test.ll), test.want; got != want {
189+
t.Errorf("%v.ContainsLatLng(%v) was %v, want %v", test.input, test.ll, got, want)
190+
}
191+
}
192+
}
193+
160194
func TestExpanded(t *testing.T) {
161195
empty := Rect{FullRect().Lat, s1.EmptyInterval()}
162196
tests := []struct {

0 commit comments

Comments
 (0)