blob: 3a7f66ecc27f49faa7bcad14176001fdad2a207c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package structs
// Define a struct storing essential star information such as it's coordinate, velocity and mass
type Star2D struct {
C Vec2 // coordinates of the star
V Vec2 // velocity of the star
M float64 // mass of the star
}
func NewStar2D(c Vec2, v Vec2, m float64) Star2D {
return Star2D{C: c, V: v, M: m}
}
// 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}
}
// Accelerate the star with the acceleration a for the time t.
// This changes the velocity of the star.
func (s *Star2D) AccelerateVelocity(a Vec2, t float64) {
s.V = s.V.Add(a.Multiply(t))
}
// Move the star with it's velocity for the time t.
// This changes the Position of the star.
func (s *Star2D) Move(t float64) {
s.C = s.C.Add(s.V.Multiply(t))
}
// 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 (s *Star2D) Accelerate(a Vec2, t float64) {
s.AccelerateVelocity(a, t)
s.Move(t)
}
|