quadtree subdivisões

This commit is contained in:
silva guimaraes 2023-08-18 13:37:58 -03:00
parent 82bd02f694
commit cb6130ef66

68
main.go
View file

@ -1,63 +1,70 @@
package main package main
// veja:
// The relationship between chaos, fractal and physics
// https://www.youtube.com/watch?v=C5Jkgvw-Z6E
import ( import (
// "fmt" // "fmt"
"fmt" "fmt"
"math" "math"
"math/rand" "math/rand"
"strconv" "strconv"
// "math/rand"
rl "github.com/gen2brain/raylib-go/raylib" rl "github.com/gen2brain/raylib-go/raylib"
) )
type quadnode struct { type quadnode struct {
topleft, bottomright rl.Vector2 // pos, size rl.Vector2
pos, size rl.Vector2
magnet *magnet magnet *magnet
parent *quadnode parent *quadnode
nodes []*quadnode nodes []*quadnode
debug string
} }
func newQuadTree() *quadnode { func newQuadTree() *quadnode {
return &quadnode{ return &quadnode{
topleft: rl.NewVector2(0, 0), pos: rl.NewVector2(0, 0),
bottomright: rl.NewVector2(windowWidth, windowHeight), size: rl.NewVector2(windowWidth, windowHeight),
nodes: nil, nodes: nil,
debug: "root",
} }
} }
func (q *quadnode) subdivide() { func (q *quadnode) subdivide() {
halfX := q.bottomright.X/2 halfX := q.size.X/2
halfY := q.bottomright.X/2 halfY := q.size.Y/2
halfSize := rl.NewVector2(halfX, halfY)
q.nodes = []*quadnode{ q.nodes = []*quadnode{
{ {
topleft: q.topleft, pos: q.pos,
bottomright: rl.Vector2{X: halfX, Y: halfY}, size: halfSize,
parent: q, parent: q,
debug: "0",
}, },
{ {
topleft: rl.Vector2{X: halfX, Y: q.topleft.Y}, pos: rl.NewVector2(q.pos.X + halfX, q.pos.Y),
bottomright: rl.Vector2{X: q.bottomright.X, Y: halfY}, size: halfSize,
parent: q, parent: q,
debug: "1",
}, },
{ {
topleft: rl.Vector2{X: q.topleft.X, Y: halfY}, pos: rl.NewVector2(q.pos.X, q.pos.Y + halfY),
bottomright: rl.Vector2{X: halfX, Y: q.bottomright.Y}, size: halfSize,
parent: q, parent: q,
debug: "2",
}, },
{ {
topleft: rl.Vector2{X: halfX, Y: halfY}, pos: rl.NewVector2(q.pos.X + halfX, q.pos.Y + halfY),
bottomright: q.bottomright, size: halfSize,
parent: q, parent: q,
debug: "3",
}, },
} }
fmt.Println(q.debug)
fmt.Println("halfX", halfX, "halfY", halfY)
for i, node := range q.nodes {
fmt.Println(i, node.pos, node.size)
}
// fmt.Println(q.nodes)
} }
func (q *quadnode) drawBoundaries() { func (q *quadnode) drawBoundaries() {
@ -65,14 +72,15 @@ func (q *quadnode) drawBoundaries() {
return return
} }
for _, node := range q.nodes { for _, node := range q.nodes {
node.drawBoundaries()
size := rl.Vector2Subtract(node.bottomright, node.topleft)
rl.DrawRectangleLines( rl.DrawRectangleLines(
int32(node.topleft.X), int32(node.topleft.Y), int32(node.pos.X), int32(node.pos.Y),
int32(size.X), int32(size.Y), int32(node.size.X), int32(node.size.Y),
rl.Green, rl.Green,
) )
} }
for _, node := range q.nodes {
node.drawBoundaries()
}
} }
type magnet struct { type magnet struct {
@ -83,14 +91,6 @@ type magnet struct {
radius float32 radius float32
} }
// type ball struct {
// pos rl.Vector2
// originGrid rl.Vector2
// radius float32
// speed rl.Vector2
// steps int
// }
// parâmetros // parâmetros
const initalMagnetCount = 10 const initalMagnetCount = 10
const windowWidth = 700 const windowWidth = 700
@ -197,6 +197,10 @@ func main() {
quadtree := newQuadTree() quadtree := newQuadTree()
quadtree.subdivide() quadtree.subdivide()
quadtree.nodes[0].subdivide() quadtree.nodes[0].subdivide()
quadtree.nodes[1].subdivide()
quadtree.nodes[2].subdivide()
quadtree.nodes[3].subdivide()
quadtree.nodes[2].nodes[0].subdivide()
pause := true pause := true