diff options
-rw-r--r-- | boundingBox.go | 24 | ||||
-rw-r--r-- | quadtree.go | 60 | ||||
-rw-r--r-- | star.go | 20 |
3 files changed, 41 insertions, 63 deletions
diff --git a/boundingBox.go b/boundingBox.go index ad02eab..4e9bf7d 100644 --- a/boundingBox.go +++ b/boundingBox.go @@ -2,26 +2,10 @@ package structs // BoundingBox is a struct defining the spatial outreach of a box type BoundingBox struct { - center Vec2 // Center of the box - width float64 // width of the box + Center Vec2 // Center of the box + Width float64 // Width of the box } -func (b *BoundingBox) Width() float64 { - return b.width -} - -func (b *BoundingBox) SetWidth(width float64) { - b.width = width -} - -func (b *BoundingBox) Center() Vec2 { - return b.center -} - -func (b *BoundingBox) SetCenter(center Vec2) { - b.center = center -} - -func NewBoundingBox(center Vec2, halfDim float64) *BoundingBox { - return &BoundingBox{center: center, width: halfDim} +func NewBoundingBox(center Vec2, width float64) *BoundingBox { + return &BoundingBox{Center: center, Width: width} } 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} +} diff --git a/star.go b/star.go index d2bb124..2255a69 100644 --- a/star.go +++ b/star.go @@ -13,10 +13,10 @@ func (s Star2D) InsideOf(boundary BoundingBox) bool { // Test if the star is inside or outside of the bounding box. // Abort testing if one of the conditions is not met - if s.C.X < boundary.center.X+boundary.width/2 { - if s.C.X > boundary.center.X-boundary.width/2 { - if s.C.Y < boundary.center.Y+boundary.width/2 { - if s.C.Y > boundary.center.Y-boundary.width/2 { + if s.C.X < boundary.Center.X+boundary.Width/2 { + if s.C.X > boundary.Center.X-boundary.Width/2 { + if s.C.Y < boundary.Center.Y+boundary.Width/2 { + if s.C.Y > boundary.Center.Y-boundary.Width/2 { return true } else { return false @@ -36,23 +36,23 @@ func (s Star2D) InsideOf(boundary BoundingBox) bool { // is applied on is. // This methods presumes that the point is inside of the boundingBox func (s Star2D) Quadrant(starsQuadtree *Quadtree) string { - centerX := starsQuadtree.boundary.center.X - centerY := starsQuadtree.boundary.center.Y + centerX := starsQuadtree.Boundary.Center.X + centerY := starsQuadtree.Boundary.Center.Y - // test if the point is left the the center or not + // test if the point is left the the Center or not if s.C.X < centerX { - // Test if the point is above or below of the center + // Test if the point is above or below of the Center if s.C.Y > centerY { return "northwest" } else { return "southwest" } - // The point is right of the center + // The point is right of the Center } else { - // Test if the point is above or below of the center + // Test if the point is above or below of the Center if s.C.Y > centerY { return "northeast" } else { |