Пример #1
0
func (p *particle) treeForce(tree node) vect.V {
	if tree == nil {
		return vect.V{}
	}
	switch n := tree.(type) {
	case *quad:
		dist := p.position.Dst(n.position)
		size := n.Size()
		if size/dist < THETA {
			return p.force(n)
		} else {
			force := vect.V{}
			for _, child := range n.children {
				force = force.Add(p.treeForce(child))
			}
			return force
		}
	case *particle:
		if n == p {
			return vect.V{}
		}
		return p.force(n)
	default:
		panic(fmt.Errorf("treeForce: bad argument type - %T", tree))
	}
}
Пример #2
0
func (q *quad) recalculate() {
	var (
		pos  vect.V
		mass float64
	)
	for _, n := range q.children {
		if n == nil {
			continue
		}
		pos = pos.Add(n.Position().Mul(n.Mass()))
		mass += n.Mass()
	}
	q.position = pos.Div(mass)
	q.mass = mass
}