diff options
Diffstat (limited to 'quadtree.go')
-rw-r--r-- | quadtree.go | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/quadtree.go b/quadtree.go index 103a666..0be4079 100644 --- a/quadtree.go +++ b/quadtree.go @@ -2,14 +2,14 @@ package structs // Definition of a quadtree and it's nodes recursively type Quadtree struct { - boundary BoundingBox // Spatial outreach of the quadtree - centerOfMass Vec2 // Center of mass of the cell - totalMass float64 // Total mass of the cell - depth int // Depth of the cell in the quadtree - star Star2D // Star inside the cell + Boundary BoundingBox `json:"boundary"` // Spatial outreach of the quadtree + CenterOfMass Vec2 `json:"CenterOfMass"` // Center of mass of the cell + TotalMass float64 `json:"totalMass"` // Total mass of the cell + Depth int `json:"depth"` // Depth of the cell in the quadtree + Star Star2D `json:"star"` // Star inside the cell // NW, NE, SW, SE - quadrants []*Quadtree // List of quadtrees representing individual quadrants + Quadrants []*Quadtree `json:"Quadrants"` // List of quadtrees representing individual Quadrants // Quadrants //northWest *Quadtree @@ -18,21 +18,15 @@ type Quadtree struct { //southEast *Quadtree } -// CenterOfMass is a getter method for quadtrees. -// It returns the Center of mass of the quadtree it is applied on -func (q *Quadtree) CenterOfMass() Vec2 { - return q.centerOfMass -} - // SetCenterOfMass is a setter method for quadtrees. -// It sets the centerOfMass of the quadtree to the given value +// It sets the CenterOfMass of the quadtree to the given value func (q *Quadtree) SetCenterOfMass(centerOfMass Vec2) { - q.centerOfMass = centerOfMass + q.CenterOfMass = centerOfMass } // CalcCenterOfMass is a calculator method for quadtrees. -// It recursively walks through the quadtree and calculates it's center of mass. -// The calculated center of mass is then inserted into the centerOfMass variable. +// It recursively walks through the quadtree and calculates it's Center of mass. +// The calculated Center of mass is then inserted into the CenterOfMass variable. func (q *Quadtree) CalcCenterOfMass() (Vec2, float64) { var totalMass float64 = 0 var x float64 = 0 @@ -41,36 +35,36 @@ func (q *Quadtree) CalcCenterOfMass() (Vec2, float64) { // If the Node is a leaf if q.IsLeaf() == true { - // update the values needed to calculate the center of mass - totalMass += q.star.M - x += q.star.C.X * q.star.M - y += q.star.C.X * q.star.M + // update the values needed to calculate the Center of mass + totalMass += q.Star.M + x += q.Star.C.X * q.Star.M + y += q.Star.C.X * q.Star.M return Vec2{x, y}, totalMass } else { - // Iterate over all the quadrants - for _, element := range q.quadrants { + // Iterate over all the Quadrants + for _, element := range q.Quadrants { - // Calculate the center of mass for each quadrant + // Calculate the Center of mass for each quadrant centerOfMass, totalMass := element.CalcCenterOfMass() - // Update the overall centerOfMass for the individual quadtree - q.centerOfMass.X += centerOfMass.X - q.centerOfMass.Y += centerOfMass.Y - q.totalMass += totalMass + // Update the overall CenterOfMass for the individual quadtree + q.CenterOfMass.X += centerOfMass.X + q.CenterOfMass.Y += centerOfMass.Y + q.TotalMass += totalMass } } - // Return the original centerOfMass and totalMass - return q.centerOfMass, q.totalMass + // Return the original CenterOfMass and totalMass + return q.CenterOfMass, q.TotalMass } // IsLeaf is a method for quadtrees returning true if the node is a leaf (has no children) // or returning false if the node is nor a leaf (has children). func (q *Quadtree) IsLeaf() bool { - for _, element := range q.quadrants { + for _, element := range q.Quadrants { if element == nil { return true } @@ -79,6 +73,6 @@ func (q *Quadtree) IsLeaf() bool { } // NewQuadtree generates a new root node. -//func NewQuadtree(boundary BoundingBox) *Quadtree { -// return &Quadtree{boundary: boundary} -//} +func NewQuadtree(boundary BoundingBox) *Quadtree { + return &Quadtree{Boundary: boundary} +} |