diff options
author | emile <hanemile@protonmail.com> | 2018-10-15 16:07:22 +0200 |
---|---|---|
committer | emile <hanemile@protonmail.com> | 2018-10-15 16:07:22 +0200 |
commit | 0525bc463f53487a7de8409697fabec28471a0bd (patch) | |
tree | 5cad6a659bdfc471e0865797bf1e19fbf8f81f02 /structs | |
parent | 088372b31882dd0217d8cc36d7abeac8b6268382 (diff) |
for detailed information about the changes, see https://git.darknebu.la/GalaxySimulator/Source/pulls/1
Diffstat (limited to 'structs')
-rw-r--r-- | structs/star.go | 29 | ||||
-rw-r--r-- | structs/structs.go | 23 | ||||
-rw-r--r-- | structs/vector2D.go | 63 |
3 files changed, 92 insertions, 23 deletions
diff --git a/structs/star.go b/structs/star.go new file mode 100644 index 0000000..72afc00 --- /dev/null +++ b/structs/star.go @@ -0,0 +1,29 @@ +package structs + +import "C" + +type Star2D struct { + C Vec2 // coordinates of the star + V Vec2 // velocity of the star + M float64 // mass of the star +} + +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 +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 +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 +func (s *Star2D) Accelerate(a Vec2, t float64) { + s.AccelerateVelocity(a, t) + s.Move(t) +} diff --git a/structs/structs.go b/structs/structs.go deleted file mode 100644 index ed70a84..0000000 --- a/structs/structs.go +++ /dev/null @@ -1,23 +0,0 @@ -package structs - -/* - The structs package defines structs that are used to store information that is used for processing - star related data. -*/ - -// Force struct soring a force vector -type Force struct { - X, Y float64 -} - -// Coord struct storing coordinates -type Coord struct { - X, Y float64 -} - -// Star struct storing all necessary star information -type Star2D struct { - C Coord - F Force - Mass float64 -} diff --git a/structs/vector2D.go b/structs/vector2D.go new file mode 100644 index 0000000..47e457f --- /dev/null +++ b/structs/vector2D.go @@ -0,0 +1,63 @@ +package structs + +import ( + "math" +) + +type Vec2 struct { + X, Y float64 +} + +// creates a copy of the vector +func (v *Vec2) Copy() Vec2 { + return Vec2{v.X, v.Y} +} + +func (v *Vec2) Split() (x float64, y float64) { + return v.X, v.Y +} + +// changes the length of the vector to the length l +func (v *Vec2) SetLength(l float64) { + var k = l / v.GetLength() + var newV = v.Multiply(k) + // v = newV + v.X, v.Y = newV.Split() +} + +// changes the length of the vector to the length 1 +func (v *Vec2) SetLengthOne() { + v.SetLength(1) +} + +// returns the direction Vector of this vector. This means a copy of this vector with a length of 1 +func (v *Vec2) GetDirVector() Vec2 { + var dirV = v.Copy() + dirV.SetLengthOne() + return dirV +} + +// returns the length of the vector +func (v *Vec2) GetLength() float64 { + return math.Sqrt(math.Pow(v.X, 2) + math.Pow(v.Y, 2)) +} + +// returns the product of the vector and a scalar s +func (v *Vec2) Multiply(s float64) Vec2 { + return Vec2{v.X * s, v.Y * s} +} + +// returns the quotient of the vector and a scalar s +func (v *Vec2) Divide(s float64) Vec2 { + return Vec2{v.X / s, v.Y / s} +} + +// returns the sum of this vector and the vector v2 +func (v1 *Vec2) Add(v2 Vec2) Vec2 { + return Vec2{v1.X + v2.X, v1.Y + v2.Y} +} + +// returns the difference of this vector minus the vector v2 +func (v1 *Vec2) Subtract(v2 Vec2) Vec2 { + return Vec2{v1.X - v2.X, v1.Y - v2.Y} +} |