Skip to content

Commit

Permalink
Go: added some vector and angle functions.
Browse files Browse the repository at this point in the history
Added functions:
    DegreesFromRadians
    RadiansFromDegrees
    AngleBetween
    StateVector.Position
    StateVector.Velocity

Changed existing functions to pass structs by value,
just like we do in the C code. This is a safety feature
to signal value semantics and a contract not to mutate the
argument state.

Run Go tests with the -v option to log more info about what's happening.
  • Loading branch information
cosinekitty committed Oct 6, 2023
1 parent 4941fbb commit d9cb9d6
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 26 deletions.
47 changes: 46 additions & 1 deletion generate/template/astronomy.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TimeFromTerrestrialDays(tt float64) AstroTime {
return makeTime(universalTime(tt), tt)
}

func (time *AstroTime) AddDays(days float64) AstroTime {
func (time AstroTime) AddDays(days float64) AstroTime {
return TimeFromUniversalDays(time.Ut + days)
}

Expand All @@ -260,6 +260,11 @@ type AstroVector struct {
T AstroTime
}

// Returns the length of vec expressed in the same distance units as vec's components.
func (vec AstroVector) Length() float64 {
return math.Sqrt(vec.X*vec.X + vec.Y*vec.Y + vec.Z*vec.Z)
}

type StateVector struct {
X float64
Y float64
Expand All @@ -270,6 +275,16 @@ type StateVector struct {
T AstroTime
}

// Position returns the position vector inside a state vector.
func (state StateVector) Position() AstroVector {
return AstroVector{state.X, state.Y, state.Z, state.T}
}

// Position returns the velocity vector inside a state vector.
func (state StateVector) Velocity() AstroVector {
return AstroVector{state.Vx, state.Vy, state.Vz, state.T}
}

type Spherical struct {
Lat float64
Lon float64
Expand Down Expand Up @@ -433,4 +448,34 @@ type constelBoundary struct {
decLo float64
}

// DegreesFromRadians converts an angle expressed in radians to an angle expressed in degrees.
func DegreesFromRadians(radians float64) float64 {
return radians * (180.0 / math.Pi)
}

// RadiansFromDegrees converts an angle expressed in degrees to an angle expressed in radians.
func RadiansFromDegrees(degrees float64) float64 {
return degrees * (math.Pi / 180.0)
}

// AngleBetween calculates the angle in degrees between two vectors.
// Given a pair of vectors avec and bvec, this function returns the
// angle in degrees between the vectors in 3D space.
// The angle is measured in the plane that contains both vectors.
// The returned value is in the closed range [0, 180].
func AngleBetween(avec AstroVector, bvec AstroVector) float64 {
r := avec.Length() * bvec.Length()
if r < 1.0e-8 {
panic("Cannot find angle between vectors because they are too short.")
}
dot := (avec.X*bvec.X + avec.Y*bvec.Y + avec.Z*bvec.Z) / r
if dot <= -1.0 {
return 180.0
}
if dot >= +1.0 {
return 0.0
}
return DegreesFromRadians(math.Acos(dot))
}

//$ASTRO_CONSTEL()
2 changes: 1 addition & 1 deletion generate/unit_test_golang
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Fail()
go version || Fail "Cannot find Go compiler."

cd ../source/golang
go test || Fail "Failure in Go unit tests."
go test -v || Fail "Failure in Go unit tests."
cd ../../generate
echo "unit_test_golang: success"
exit 0
106 changes: 83 additions & 23 deletions source/golang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ It provides a suite of well\-tested functions for calculating positions of the S
## Index

- [Constants](<#constants>)
- [func AngleBetween\(avec AstroVector, bvec AstroVector\) float64](<#AngleBetween>)
- [func DegreesFromRadians\(radians float64\) float64](<#DegreesFromRadians>)
- [func RadiansFromDegrees\(degrees float64\) float64](<#RadiansFromDegrees>)
- [type AstroMoonQuarter](<#AstroMoonQuarter>)
- [type AstroSearchFunc](<#AstroSearchFunc>)
- [type AstroTime](<#AstroTime>)
- [func TimeFromTerrestrialDays\(tt float64\) AstroTime](<#TimeFromTerrestrialDays>)
- [func TimeFromUniversalDays\(ut float64\) AstroTime](<#TimeFromUniversalDays>)
- [func \(time \*AstroTime\) AddDays\(days float64\) AstroTime](<#AstroTime.AddDays>)
- [func \(time AstroTime\) AddDays\(days float64\) AstroTime](<#AstroTime.AddDays>)
- [type AstroVector](<#AstroVector>)
- [func \(vec AstroVector\) Length\(\) float64](<#AstroVector.Length>)
- [type AtmosphereInfo](<#AtmosphereInfo>)
- [type AxisInfo](<#AxisInfo>)
- [type Body](<#Body>)
Expand All @@ -37,6 +41,8 @@ It provides a suite of well\-tested functions for calculating positions of the S
- [type SeasonsInfo](<#SeasonsInfo>)
- [type Spherical](<#Spherical>)
- [type StateVector](<#StateVector>)
- [func \(state StateVector\) Position\(\) AstroVector](<#StateVector.Position>)
- [func \(state StateVector\) Velocity\(\) AstroVector](<#StateVector.Velocity>)
- [type TimeFormat](<#TimeFormat>)
- [type Topocentric](<#Topocentric>)

Expand Down Expand Up @@ -118,8 +124,35 @@ const (
)
```

<a name="AngleBetween"></a>
## func [AngleBetween](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L466>)

```go
func AngleBetween(avec AstroVector, bvec AstroVector) float64
```

AngleBetween calculates the angle in degrees between two vectors. Given a pair of vectors avec and bvec, this function returns the angle in degrees between the vectors in 3D space. The angle is measured in the plane that contains both vectors. The returned value is in the closed range \[0, 180\].

<a name="DegreesFromRadians"></a>
## func [DegreesFromRadians](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L452>)

```go
func DegreesFromRadians(radians float64) float64
```

DegreesFromRadians converts an angle expressed in radians to an angle expressed in degrees.

<a name="RadiansFromDegrees"></a>
## func [RadiansFromDegrees](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L457>)

```go
func RadiansFromDegrees(degrees float64) float64
```

RadiansFromDegrees converts an angle expressed in degrees to an angle expressed in radians.

<a name="AstroMoonQuarter"></a>
## type [AstroMoonQuarter](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L357-L360>)
## type [AstroMoonQuarter](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L372-L375>)



Expand All @@ -131,7 +164,7 @@ type AstroMoonQuarter struct {
```

<a name="AstroSearchFunc"></a>
## type [AstroSearchFunc](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L371>)
## type [AstroSearchFunc](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L386>)



Expand Down Expand Up @@ -206,10 +239,10 @@ func TimeFromUniversalDays(ut float64) AstroTime


<a name="AstroTime.AddDays"></a>
### func \(\*AstroTime\) [AddDays](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L243>)
### func \(AstroTime\) [AddDays](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L243>)

```go
func (time *AstroTime) AddDays(days float64) AstroTime
func (time AstroTime) AddDays(days float64) AstroTime
```


Expand All @@ -228,8 +261,17 @@ type AstroVector struct {
}
```

<a name="AstroVector.Length"></a>
### func \(AstroVector\) [Length](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L264>)

```go
func (vec AstroVector) Length() float64
```

Returns the length of vec expressed in the same distance units as vec's components.

<a name="AtmosphereInfo"></a>
## type [AtmosphereInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L344-L348>)
## type [AtmosphereInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L359-L363>)



Expand All @@ -242,7 +284,7 @@ type AtmosphereInfo struct {
```

<a name="AxisInfo"></a>
## type [AxisInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L384-L389>)
## type [AxisInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L399-L404>)



Expand All @@ -256,7 +298,7 @@ type AxisInfo struct {
```

<a name="Body"></a>
## type [Body](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L279>)
## type [Body](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L294>)



Expand All @@ -281,7 +323,7 @@ type CalendarDateTime struct {
```

<a name="DeltaTimeFunc"></a>
## type [DeltaTimeFunc](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L373>)
## type [DeltaTimeFunc](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L388>)



Expand All @@ -290,7 +332,7 @@ type DeltaTimeFunc func(ut float64) float64
```

<a name="Ecliptic"></a>
## type [Ecliptic](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L319-L323>)
## type [Ecliptic](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L334-L338>)



Expand All @@ -303,7 +345,7 @@ type Ecliptic struct {
```

<a name="Equatorial"></a>
## type [Equatorial](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L312-L317>)
## type [Equatorial](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L327-L332>)



Expand All @@ -317,7 +359,7 @@ type Equatorial struct {
```

<a name="JupiterMoonsInfo"></a>
## type [JupiterMoonsInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L417-L422>)
## type [JupiterMoonsInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L432-L437>)



Expand All @@ -331,7 +373,7 @@ type JupiterMoonsInfo struct {
```

<a name="LibrationInfo"></a>
## type [LibrationInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L375-L382>)
## type [LibrationInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L390-L397>)



Expand All @@ -347,7 +389,7 @@ type LibrationInfo struct {
```

<a name="NodeEventInfo"></a>
## type [NodeEventInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L399-L402>)
## type [NodeEventInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L414-L417>)



Expand All @@ -359,7 +401,7 @@ type NodeEventInfo struct {
```

<a name="NodeEventKind"></a>
## type [NodeEventKind](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L391>)
## type [NodeEventKind](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L406>)



Expand All @@ -368,7 +410,7 @@ type NodeEventKind int
```

<a name="Observer"></a>
## type [Observer](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L306-L310>)
## type [Observer](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L321-L325>)



Expand All @@ -381,7 +423,7 @@ type Observer struct {
```

<a name="Refraction"></a>
## type [Refraction](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L336>)
## type [Refraction](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L351>)



Expand All @@ -400,7 +442,7 @@ const (
```

<a name="RotationMatrix"></a>
## type [RotationMatrix](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L332-L334>)
## type [RotationMatrix](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L347-L349>)



Expand All @@ -411,7 +453,7 @@ type RotationMatrix struct {
```

<a name="SeasonsInfo"></a>
## type [SeasonsInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L350-L355>)
## type [SeasonsInfo](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L365-L370>)



Expand All @@ -425,7 +467,7 @@ type SeasonsInfo struct {
```

<a name="Spherical"></a>
## type [Spherical](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L273-L277>)
## type [Spherical](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L288-L292>)



Expand All @@ -438,7 +480,7 @@ type Spherical struct {
```

<a name="StateVector"></a>
## type [StateVector](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L263-L271>)
## type [StateVector](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L268-L276>)



Expand All @@ -454,8 +496,26 @@ type StateVector struct {
}
```

<a name="StateVector.Position"></a>
### func \(StateVector\) [Position](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L279>)

```go
func (state StateVector) Position() AstroVector
```

Position returns the position vector inside a state vector.

<a name="StateVector.Velocity"></a>
### func \(StateVector\) [Velocity](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L284>)

```go
func (state StateVector) Velocity() AstroVector
```

Position returns the velocity vector inside a state vector.

<a name="TimeFormat"></a>
## type [TimeFormat](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L362>)
## type [TimeFormat](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L377>)



Expand All @@ -475,7 +535,7 @@ const (
```

<a name="Topocentric"></a>
## type [Topocentric](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L325-L330>)
## type [Topocentric](<https://github.com/cosinekitty/astronomy/blob/golang/source/golang/astronomy.go#L340-L345>)



Expand Down
Loading

0 comments on commit d9cb9d6

Please sign in to comment.