about summary refs log tree commit diff
path: root/star.go
blob: 11aea06a966b9074ba0b5c802d46a459e2ec8337 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// main.go purpose is to build the interaction layer in between the http endpoints and the http server
// Copyright (C) 2019 Emile Hansmaennel
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package structs

// Star2D defines a struct storing essential star information such as it's coordinate, velocity and mass
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
}

// NewStar2D returns a new star using the given arguments as values for 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 (star 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 star.C.X < boundary.Center.X+boundary.Width/2 {
		if star.C.X > boundary.Center.X-boundary.Width/2 {
			if star.C.Y < boundary.Center.Y+boundary.Width/2 {
				if star.C.Y > boundary.Center.Y-boundary.Width/2 {
					return true
				}
			}
		}
	}

	return false
}

// CalcNewPos calculates the new position of a star using the force acting on it
func (star *Star2D) CalcNewPos(force Vec2, timestep float64) {
	acceleration := NewVec2(force.X/star.M, force.Y/star.M)
	star.Accelerate(acceleration, timestep)
}

// Copy Return a copy of the star by returning a star struct with the same values.
func (star *Star2D) Copy() Star2D {
	return Star2D{star.C.Copy(), star.V.Copy(), star.M}
}

// AccelerateVelocity accelerates the star with the acceleration a for the time t.
// This changes the velocity of the star.
func (star *Star2D) AccelerateVelocity(a Vec2, t float64) {
	star.V = star.V.Add(a.Multiply(t))
}

// Move the star with it's velocity for the time t.
// This changes the Position of the star.
func (star *Star2D) Move(t float64) {
	star.C = star.C.Add(star.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 (star *Star2D) Accelerate(a Vec2, t float64) {
	star.AccelerateVelocity(a, t)
	star.Move(t)
}

// posX determines if the star is the positive x region of the given boundary. If it is,
// the method returns true, if not, it returns false
func (star Star2D) posX(boundary BoundingBox) bool {

	// define shortcuts
	bx := boundary.Center.X
	bw := boundary.Width / 2

	if star.C.X > bx && star.C.X < bx+bw {
		return true
	}
	return false
}

// posY determines if the star is the positive y region of the given boundary. If it is,
// the method returns true, if not, it returns false
func (star Star2D) posY(boundary BoundingBox) bool {

	// define shortcuts
	by := boundary.Center.Y
	bw := boundary.Width / 2

	if star.C.Y > by && star.C.Y < by+bw {
		return true
	}
	return false
}

// getRelativePosition returns the relative position of a star relative to the bounding
// bounding box it is in. It returns the integer that is mapped to a cell in the Node
// definition
func (star Star2D) getRelativePosition(boundary BoundingBox) string {
	if star.posX(boundary) == true {
		if star.posY(boundary) == true {
			return "NE"
		}
		return "SE"
	}
	if star.posY(boundary) == true {
		return "NW"
	}
	return "SW"
}

func (star Star2D) getRelativePositionInt(boundary BoundingBox) int {
	quadrantMap := make(map[string]int)
	quadrantMap["NW"] = 0
	quadrantMap["NE"] = 1
	quadrantMap["SW"] = 2
	quadrantMap["SE"] = 3

	QuadrantMapString := star.getRelativePosition(boundary)
	return quadrantMap[QuadrantMapString]
}