-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspatial.go
More file actions
35 lines (28 loc) · 828 Bytes
/
spatial.go
File metadata and controls
35 lines (28 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"github.com/dhconnelly/rtreego"
"github.com/gravestench/mathlib"
)
type Spatial struct {
tree *rtreego.Rtree
}
func (i *Spatial) search(position *mathlib.Vector2, sideLength float64) []rtreego.Spatial {
return i.tree.SearchIntersect(rtreego.Point{position.X, position.Y}.ToRect(sideLength))
}
func (i *Spatial) GetNeighbours(position *mathlib.Vector2, sideLength float64, boidId int) ([]Boid, int) {
var neighbours []Boid
spatials := i.search(position, sideLength)
for _, spatial := range spatials {
neighbour := spatial.(Boid)
if boidId == neighbour.Id {
continue
}
neighbours = append(neighbours, neighbour)
}
return neighbours, len(neighbours)
}
func newIndex(points ...rtreego.Spatial) *Spatial {
return &Spatial{
tree: rtreego.NewTree(2, 0, len(points), points...),
}
}