- Notifications
You must be signed in to change notification settings - Fork1
s0rg/grid
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Generic 2D grid
- DDA RayCasting
- A-Star pathfinding
- Ray-based line of sight
- Recursive ShadowCasting
- Dijkstra maps
- Bresenham's lines
- 100% test cover
import ("image""github.com/s0rg/grid")constmapW,mapH=100,100funcvalueExample() {// working with value-types is straightforwardg:= grid.New[int](image.Rect(0,0,mapW,mapH))// now grid is filled with nil-value for your type// you still can re-fill it with some other values:g.Fill(func()int {return1 })}funcpointerExample() {// working with pointer-types is same, but you now you must to pre-fill themtypemycellstruct {}g:= grid.New[*mycell](image.Rect(0,0,mapW,mapH))// now grid is filled with nil's, so you need pre-fill it with some values,// otherwise you will access those nil's with Get / MustGet methods.g.Fill(func()*mycell {return&mycell{} })}funcusageExample() {typemycellstruct {wallbool }g:= grid.New[*mycell](image.Rect(0,0,mapW,mapH))g.Fill(func()*mycell {return&mycell{} })pt:=image.Pt(10,10)// set new valueg.Set(pt,&mycell{wall:true})// update existing valueifv,ok:=g.Get(pt);ok {v.wall=false }// shorthand, for above, will panic on out-of-bounds accessg.MustGet(pt).wall=true// iterate itemsg.Iter(func(p image.Point,c*mycell) (nextbool) {ifc.wall {// wall found }returntrue })}
Here is a full example.
You can run it withgo run _example/main.go
to see results.
run:
make bench
results:
goos: linuxgoarch: amd64pkg: github.com/s0rg/gridcpu: AMD Ryzen 5 5500U with Radeon GraphicsBenchmarkGrid/Set-12 1000000000 0.8108 ns/op 0 B/op 0 allocs/opBenchmarkGrid/Get-12 641611768 1.764 ns/op 0 B/op 0 allocs/opBenchmarkGrid/Neighbours-12 52243890 23.41 ns/op 0 B/op 0 allocs/opBenchmarkGrid/LineBresenham-12 4416172 269.0 ns/op 0 B/op 0 allocs/opBenchmarkGrid/CastRay-12 3829839 321.1 ns/op 0 B/op 0 allocs/opBenchmarkGrid/CastShadow-12 32648 36950 ns/op 0 B/op 0 allocs/opBenchmarkGrid/LineOfSight-12 9897 114576 ns/op 0 B/op 0 allocs/opBenchmarkGrid/DijkstraMap-12 1029 1190195 ns/op 20656 B/op 3 allocs/opBenchmarkGrid/Path-12 372 3225325 ns/op 997588 B/op 13643 allocs/opPASSok github.com/s0rg/grid12.098s
About
Generic 2D grid
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.