-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconv.jule
More file actions
132 lines (117 loc) · 3.84 KB
/
conv.jule
File metadata and controls
132 lines (117 loc) · 3.84 KB
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
// Copyright 2025 mertcandav.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.
use "std/math"
// Converts radians to degrees.
fn RadiansToDegrees(rad: f64): f64 {
// deg = rad * (180 / π)
const Div180Pi = 57.2957795130823208767981548141051703324054724665643215491602438 // https://oeis.org/A072097
ret rad * Div180Pi
}
// Converts degrees to radians.
fn DegreesToRadians(deg: f64): f64 {
// rad = deg * (π / 180)
const DivPi180 = 0.017453292519943295769236907684886127134428718885417254560971914 // https://oeis.org/A019685
ret deg * DivPi180
}
// Converts degrees to gradians.
fn DegreesToGradians(deg: f64): f64 {
// 1 degree = 10/9 gradians.
ret deg * (10 / 9)
}
// Converts gradians to degrees.
fn GradiansToDegrees(grad: f64): f64 {
// 1 gradian = 0.9 degrees.
ret grad * 0.9
}
// Converts radians to gradians.
fn RadiansToGradians(rad: f64): f64 {
// 1 radian = 200 / π gradians.
ret rad * (200 / Pi)
}
// Converts gradians to radians.
fn GradiansToRadians(grad: f64): f64 {
// 1 gradian = π / 200 radians.
ret grad * (Pi / 200)
}
// Converts an angle given in hours, minutes, and seconds to degrees (used in RA conversion).
fn HMSToDegrees(hours: f64, minutes: f64, seconds: f64): f64 {
// deg = 15 * (h + m/60 + s/3600)
ret 15 * (hours + minutes/60 + seconds/3600)
}
// Converts decimal degrees to hours, minutes, and seconds (used in RA).
fn DegreesToHMS(deg: f64): (hours: f64, minutes: f64, seconds: f64) {
totalHours := math::Abs(deg / 15)
hours = math::Floor(totalHours)
remainder := (totalHours - hours) * 60
minutes = math::Floor(remainder)
seconds = (remainder - f64(minutes)) * 60
if deg < 0 {
hours = -hours
}
ret hours, minutes, seconds
}
// Converts an angle in degrees, arcminutes, and arcseconds to decimal degrees.
// Supports negative input for southern or western directions.
fn DMSToDegrees(degrees: f64, minutes: f64, seconds: f64): f64 {
// decimal = sign * (|degrees| + minutes/60 + seconds/3600)
sign := math::Copysign(1, degrees) // Preserve the sign of the degrees.
absDeg := math::Abs(degrees)
ret sign * (absDeg + minutes/60 + seconds/3600)
}
// Converts decimal degrees to degrees, minutes, and seconds.
fn DegreesToDMS(deg: f64): (degrees: f64, minutes: f64, seconds: f64) {
sign := math::Copysign(1, deg) // Preserve the sign of the degrees
absDeg := math::Abs(deg)
degrees = math::Floor(absDeg)
remainder := (absDeg - degrees) * 60
minutes = math::Floor(remainder)
seconds = (remainder - minutes) * 60
ret degrees * sign, minutes, seconds
}
// Normalizes an angle in degrees to the range [0, 360).
fn NormalizeAngle(deg: f64): f64 {
mut normalized := math::Mod(deg, 360)
if normalized < 0 {
normalized += 360
}
ret normalized
}
// Normalizes RA hours to the range [0, 24).
fn NormalizeRAHours(hours: f64): f64 {
mut normalized := math::Mod(hours, 24)
if normalized < 0 {
normalized += 24
}
ret normalized
}
// Converts Cartesian coordinates (x, y, z) to spherical coordinates (r, theta, phi).
//
// r: radius (distance from origin)
// theta: polar angle (0 ≤ theta ≤ π), angle from +z axis
// phi: azimuthal angle (0 ≤ phi < 2π), angle from +x axis in xy-plane
fn CartesianToSpherical(x: f64, y: f64, z: f64): (r: f64, theta: f64, phi: f64) {
r = math::Sqrt(x*x + y*y + z*z)
if r == 0 {
theta = 0
phi = 0
ret
}
theta = math::Acos(z / r) // polar angle
phi = math::Atan2(y, x) // azimuthal angle
if phi < 0 {
phi += 2 * Pi
}
ret
}
// Converts spherical coordinates (r, theta, phi) in radians to Cartesian coordinates (x, y, z).
//
// theta: polar angle in radians (0 ≤ θ ≤ π)
// phi: azimuthal angle in radians (0 ≤ φ < 2π)
fn SphericalToCartesian(r: f64, theta: f64, phi: f64): (x: f64, y: f64, z: f64) {
sinTheta := math::Sin(theta)
x = r * sinTheta * math::Cos(phi)
y = r * sinTheta * math::Sin(phi)
z = r * math::Cos(theta)
ret
}