Documentation
¶
Index ¶
- type BoundingBox
- type Node
- func (n Node) CalcAllForces(star Star2D, theta float64) Vec2
- func (n *Node) CalcCenterOfMass() Vec2
- func (n *Node) CalcTotalMass() float64
- func (n Node) DrawTreeLaTeX(outpath string)
- func (n Node) GenForestTree(node *Node) string
- func (n Node) GetAllStars() []Star2D
- func (n *Node) Insert(star Star2D) error
- func (n *Node) Subdivide()
- type Star2D
- type Stargalaxy
- type Vec2
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BoundingBox ¶
BoundingBox is a struct defining the spatial outreach of a box
func NewBoundingBox ¶
func NewBoundingBox(center Vec2, width float64) BoundingBox
NewBoundingBox returns a new Bounding Box using the centerpoint and the width given by the function parameters
type Node ¶
type Node struct { Boundary BoundingBox // Spatial outreach of the quadtree CenterOfMass Vec2 // Center of mass of the cell TotalMass float64 // Total mass of all the stars in the cell Depth int // Depth of the cell in the tree Star Star2D // The actual star // NW, NE, SW, SE Subtrees [4]*Node // The child subtrees }
Node defines a node in the tree storing the galaxy
func NewNode ¶
func NewNode(bounadry BoundingBox) *Node
NewNode creates a new new node using the given bounding box
Example ¶
The example below creates a new node using the given bounding box
newNode := NewNode(BoundingBox{ Center: Vec2{ X: 25, Y: 25, }, Width: 50, }) fmt.Printf("%v\n", newNode)
Output: &{{{25 25} 50} {0 0} 0 0 {{0 0} {0 0} 0} [<nil> <nil> <nil> <nil>]}
func NewRoot ¶
NewRoot returns a pointer to a node defined as a root node. It taks the with of the BoundingBox as an argument resulting in a node that should (in theory) fit the whole galaxy if defined correctly.
Example ¶
The example below creates a new root node at (0, 0) with the given width. The width given in this case is 100.
root := NewRoot(100) fmt.Printf("%v\n", root)
Output: &{{{0 0} 100} {0 0} 0 0 {{0 0} {0 0} 0} [<nil> <nil> <nil> <nil>]}
func (Node) CalcAllForces ¶
CalcAllForces calculates the force acting in between the given star and all the other stars using the given theta. It gets all the other stars from the root node it is called on
Example ¶
Output:
func (*Node) CalcCenterOfMass ¶
CalcCenterOfMass calculates the center of mass for every node in the tree
Example ¶
CalcCenterOfMass calculates the center of mass of the node it is called on. In the example below, the Tree contains two stars with equal mass (10): (20, 30) and (-20, -30).
root := NewRoot(100) root.Subdivide() star1 := NewStar2D(Vec2{0, 0}, Vec2{0, 0}, 10) star2 := NewStar2D(Vec2{3, 3}, Vec2{0, 0}, 10) // Insert the stars into the tree // There will be no error handling here, we'll assume that everything goes right.. _ = root.Insert(star1) _ = root.Insert(star2) root.CalcTotalMass() centerOfMass := root.CalcCenterOfMass() fmt.Println(centerOfMass)
Output:
func (*Node) CalcTotalMass ¶
CalcTotalMass calculates the total mass for every node in the tree
Example ¶
Output:
func (Node) DrawTreeLaTeX ¶
DrawTreeLaTeX writes the tree it is called on to a texfile defined by the outpath parameter and calls lualatex to build the tex-file
Example ¶
Draws the tree to a pdf using lualatex for building the pdf. (Luatex is used, because pdflatex apparently cannot handle such deep recursion depths)
// create a new root node root := NewRoot(100) // write the LaTeX to out.tex and build the tex using luatex root.DrawTreeLaTeX("out.tex")
Output:
func (Node) GenForestTree ¶
GenForestTree draws the subtree it is called on. If there is a star inside of the root node, the node is drawn The method returns a string depicting the tree in latex forest structure
Example ¶
Generate a tree using the LaTeX forest tree notation This is a minimal example using only a root node
{ // Create a new root root := NewRoot(100) root.Star = Star2D{ C: Vec2{ X: 10, Y: 20, }, V: Vec2{ X: 0, Y: 0, }, M: 20, } // generate the tree forestTree := root.GenForestTree(root) fmt.Println(forestTree)
Output: [10 20[][][][]]
Example (DeepTree) ¶
Generate a tree using the LaTeX forest tree notation. This is an example displaying a bigger tree.
// Create a new root root := NewRoot(100) // Subdivide the root multiple times root.Subdivide() root.Subtrees[1].Subdivide() // Insert a star into the tree root.Subtrees[1].Star = Star2D{ C: Vec2{ X: 20, Y: 30, }, V: Vec2{ X: 0, Y: 0, }, M: 20, } // Generate the tree forestTree := root.GenForestTree(root) fmt.Println(forestTree)
Output: [[[][][][]][20 30[[][][][]][[][][][]][[][][][]][[][][][]]][[][][][]][[][][][]]]
func (Node) GetAllStars ¶
GetAllStars returns all the stars in the tree it is called on in an array
Example ¶
GetAllStars gets all the stars from a selected tree. In the example below, an empty node is generated, subdivided and two stars are inserted into it. Then, a list of all stars (the two that were previously inserted) gets generated and printed.
// Define a new root node root := NewRoot(100) // Subdivide the root root.Subdivide() // Insert two stars into the tree root.Subtrees[1].Star = Star2D{ C: Vec2{ X: 10, Y: 20, }, V: Vec2{ X: 0, Y: 0, }, M: 0, } root.Subtrees[3].Star = Star2D{ C: Vec2{ X: 30, Y: 40, }, V: Vec2{ X: 0, Y: 0, }, M: 0, } // Get the stars from the tree starsList := root.GetAllStars() // Print all the stars in the list for _, star := range starsList { fmt.Println(star) }
Output: {{10 20} {0 0} 0} {{30 40} {0 0} 0}
func (*Node) Insert ¶
Insert inserts the given star into the Node or the tree it is called on
Example ¶
Insert a star into a tree. If the star cannot be inserted (e.g. recursion depth too deep), Insert() returns an error
// Initialize a tree and a star root := NewRoot(100) star := Star2D{ C: Vec2{ X: 12, Y: 34, }, V: Vec2{ X: 0, Y: 0, }, M: 0, } // insert the star into the tree err := root.Insert(star) // handle potential errors if err != nil { panic(err) } fmt.Printf("%v", root)
Output: Direct insert of (12.000000, 34.000000) &{{{0 0} 100} {0 0} 0 0 {{12 34} {0 0} 0} [<nil> <nil> <nil> <nil>]}
Example (Error) ¶
Insert two stars that are very close to each other into the tree. A problem arises: the tree has to be subdivided so often, that the insert function raises a recursion depth error
// Initialize a tree and two root := NewRoot(100) star1 := Star2D{ C: Vec2{ X: 5, Y: 5, }, V: Vec2{ X: 0, Y: 0, }, M: 0, } star2 := Star2D{ C: Vec2{ X: 5.000000000000001, Y: 5.000000000000001, }, V: Vec2{ X: 0, Y: 0, }, M: 0, } // insert the first star into the tree err := root.Insert(star1) // handle potential errors if err != nil { panic(err) } // insert the second star into the tree err = root.Insert(star2) // handle potential errors if err != nil { panic(err) }
Output: Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Direct insert of (5.000000, 5.000000) Could not insert star (5.000000, 5.000000) (recursion limit reached) Could not insert star (5.000000, 5.000000) (recursion limit reached)
func (*Node) Subdivide ¶
func (n *Node) Subdivide()
Subdivide the tree
Example ¶
The example below subdivides the node it is called on. The function inserts four pointers pointing to other nodes into the subtree array representing the quadrants.
The Code below prints the four subtrees that where generated:
root := NewRoot(100) root.Subdivide() for i := 0; i < 4; i++ { fmt.Printf("%v\n", root.Subtrees[i]) }
Output: &{{{-25 25} 50} {0 0} 0 0 {{0 0} {0 0} 0} [<nil> <nil> <nil> <nil>]} &{{{25 25} 50} {0 0} 0 0 {{0 0} {0 0} 0} [<nil> <nil> <nil> <nil>]} &{{{-25 -25} 50} {0 0} 0 0 {{0 0} {0 0} 0} [<nil> <nil> <nil> <nil>]} &{{{25 -25} 50} {0 0} 0 0 {{0 0} {0 0} 0} [<nil> <nil> <nil> <nil>]}
type Star2D ¶
type Star2D struct { C Vec2 `json:"C"` // coordinates of the star V Vec2 `json:"V"` // velocity of the star M float64 `json:"M"` // mass of the star }
Star2D defines a struct storing essential star information such as it's coordinate, velocity and mass
func NewStar2D ¶
NewStar2D returns a new star using the given arguments as values for the Star
func (*Star2D) Accelerate ¶
Accelerate and move the star with it's velocity and the acceleration a for the time t This changes the position and the velocity of the star.
func (*Star2D) AccelerateVelocity ¶
AccelerateVelocity accelerates the star with the acceleration a for the time t. This changes the velocity of the star.
func (*Star2D) CalcNewPos ¶
CalcNewPos calculates the new position of a star using the force acting on it
func (*Star2D) Copy ¶
Copy Return a copy of the star by returning a star struct with the same values.
func (Star2D) InsideOf ¶
func (star Star2D) InsideOf(boundary BoundingBox) bool
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.
type Stargalaxy ¶
Stargalaxy is a struct bundling the star and the galaxy index it comes from
type Vec2 ¶
Vec2 defines a vector
func CalcForce ¶
CalcForce calculates the force exerted on s1 by s2 and returns a vector representing that force
Example ¶
Output:
func NewVec2 ¶
NewVec2 returns a new Vec2 using the given coordinates
func (*Vec2) Add ¶
Add returns the sum of this vector and the vector v2