quadtree subdivisões
This commit is contained in:
parent
82bd02f694
commit
cb6130ef66
1 changed files with 36 additions and 32 deletions
68
main.go
68
main.go
|
|
@ -1,63 +1,70 @@
|
|||
package main
|
||||
|
||||
// veja:
|
||||
// The relationship between chaos, fractal and physics
|
||||
// https://www.youtube.com/watch?v=C5Jkgvw-Z6E
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
|
||||
// "math/rand"
|
||||
|
||||
rl "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
|
||||
type quadnode struct {
|
||||
topleft, bottomright rl.Vector2
|
||||
// pos, size rl.Vector2
|
||||
pos, size rl.Vector2
|
||||
magnet *magnet
|
||||
parent *quadnode
|
||||
nodes []*quadnode
|
||||
debug string
|
||||
}
|
||||
|
||||
func newQuadTree() *quadnode {
|
||||
return &quadnode{
|
||||
topleft: rl.NewVector2(0, 0),
|
||||
bottomright: rl.NewVector2(windowWidth, windowHeight),
|
||||
pos: rl.NewVector2(0, 0),
|
||||
size: rl.NewVector2(windowWidth, windowHeight),
|
||||
nodes: nil,
|
||||
debug: "root",
|
||||
}
|
||||
}
|
||||
|
||||
func (q *quadnode) subdivide() {
|
||||
halfX := q.bottomright.X/2
|
||||
halfY := q.bottomright.X/2
|
||||
halfX := q.size.X/2
|
||||
halfY := q.size.Y/2
|
||||
halfSize := rl.NewVector2(halfX, halfY)
|
||||
|
||||
q.nodes = []*quadnode{
|
||||
{
|
||||
topleft: q.topleft,
|
||||
bottomright: rl.Vector2{X: halfX, Y: halfY},
|
||||
pos: q.pos,
|
||||
size: halfSize,
|
||||
parent: q,
|
||||
debug: "0",
|
||||
},
|
||||
{
|
||||
topleft: rl.Vector2{X: halfX, Y: q.topleft.Y},
|
||||
bottomright: rl.Vector2{X: q.bottomright.X, Y: halfY},
|
||||
pos: rl.NewVector2(q.pos.X + halfX, q.pos.Y),
|
||||
size: halfSize,
|
||||
parent: q,
|
||||
debug: "1",
|
||||
},
|
||||
{
|
||||
topleft: rl.Vector2{X: q.topleft.X, Y: halfY},
|
||||
bottomright: rl.Vector2{X: halfX, Y: q.bottomright.Y},
|
||||
pos: rl.NewVector2(q.pos.X, q.pos.Y + halfY),
|
||||
size: halfSize,
|
||||
parent: q,
|
||||
debug: "2",
|
||||
},
|
||||
{
|
||||
topleft: rl.Vector2{X: halfX, Y: halfY},
|
||||
bottomright: q.bottomright,
|
||||
pos: rl.NewVector2(q.pos.X + halfX, q.pos.Y + halfY),
|
||||
size: halfSize,
|
||||
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() {
|
||||
|
|
@ -65,14 +72,15 @@ func (q *quadnode) drawBoundaries() {
|
|||
return
|
||||
}
|
||||
for _, node := range q.nodes {
|
||||
node.drawBoundaries()
|
||||
size := rl.Vector2Subtract(node.bottomright, node.topleft)
|
||||
rl.DrawRectangleLines(
|
||||
int32(node.topleft.X), int32(node.topleft.Y),
|
||||
int32(size.X), int32(size.Y),
|
||||
int32(node.pos.X), int32(node.pos.Y),
|
||||
int32(node.size.X), int32(node.size.Y),
|
||||
rl.Green,
|
||||
)
|
||||
}
|
||||
for _, node := range q.nodes {
|
||||
node.drawBoundaries()
|
||||
}
|
||||
}
|
||||
|
||||
type magnet struct {
|
||||
|
|
@ -83,14 +91,6 @@ type magnet struct {
|
|||
radius float32
|
||||
}
|
||||
|
||||
// type ball struct {
|
||||
// pos rl.Vector2
|
||||
// originGrid rl.Vector2
|
||||
// radius float32
|
||||
// speed rl.Vector2
|
||||
// steps int
|
||||
// }
|
||||
|
||||
// parâmetros
|
||||
const initalMagnetCount = 10
|
||||
const windowWidth = 700
|
||||
|
|
@ -197,6 +197,10 @@ func main() {
|
|||
quadtree := newQuadTree()
|
||||
quadtree.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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue