about summary refs log tree commit diff
path: root/structs
diff options
context:
space:
mode:
Diffstat (limited to 'structs')
-rw-r--r--structs/boundingBox.go27
-rw-r--r--structs/quadtree.go84
-rw-r--r--structs/star.go56
3 files changed, 165 insertions, 2 deletions
diff --git a/structs/boundingBox.go b/structs/boundingBox.go
new file mode 100644
index 0000000..ad02eab
--- /dev/null
+++ b/structs/boundingBox.go
@@ -0,0 +1,27 @@
+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
+}
+
+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}
+}
diff --git a/structs/quadtree.go b/structs/quadtree.go
new file mode 100644
index 0000000..7f82a2a
--- /dev/null
+++ b/structs/quadtree.go
@@ -0,0 +1,84 @@
+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
+
+	// NW, NE, SW, SE
+	quadrants []*Quadtree // List of quadtrees representing individual quadrants
+
+	// Quadrants
+	//northWest *Quadtree
+	//northEast *Quadtree
+	//southWest *Quadtree
+	//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
+func (q *Quadtree) SetCenterOfMass(centerOfMass Vec2) {
+	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.
+func (q *Quadtree) CalcCenterOfMass() (Vec2, float64) {
+	var totalMass float64 = 0
+	var x float64 = 0
+	var y float64 = 0
+
+	// 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
+
+		return (Vec2{x, y}, totalMass)
+
+	} else {
+
+		// Iterate over all the quadrants
+		for _, element := range q.quadrants {
+
+			// 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
+		}
+	}
+
+	// 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 {
+		if element == nil {
+			return true
+		}
+	}
+	return false
+}
+
+// NewQuadtree generates a new root node.
+func NewQuadtree(boundary BoundingBox) *Quadtree {
+	return &Quadtree{boundary: boundary}
+}
diff --git a/structs/star.go b/structs/star.go
index f495d99..d2bb124 100644
--- a/structs/star.go
+++ b/structs/star.go
@@ -1,7 +1,5 @@
 package structs
 
-import "C"
-
 // Define a struct storing essential star information such as it's coordinate, velocity and mass
 type Star2D struct {
 	C Vec2    // coordinates of the star
@@ -9,6 +7,60 @@ type Star2D struct {
 	M float64 // mass        of the star
 }
 
+// InsideOf is a method that tests if the star it is applied on is in or outside of the given
+// BoundingBox. It returns true if the star is inside of the BoundingBox and false if it isn't.
+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 {
+					return true
+				} else {
+					return false
+				}
+			} else {
+				return false
+			}
+		} else {
+			return false
+		}
+	} else {
+		return false
+	}
+}
+
+// Quadrant returns a string indicating in which quadrant of the given quadtree the point the method
+// 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
+
+	// 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
+		if s.C.Y > centerY {
+			return "northwest"
+		} else {
+			return "southwest"
+		}
+
+		// The point is right of the center
+	} else {
+
+		// Test if the point is above or below of the center
+		if s.C.Y > centerY {
+			return "northeast"
+		} else {
+			return "southeast"
+		}
+	}
+}
+
 // Return a copy of the star by returning a star struct with the same values.
 func (s *Star2D) Copy() Star2D {
 	return Star2D{s.C.Copy(), s.V.Copy(), s.M}