Orbital elements: Difference between revisions
Content added Content deleted
(Added Prolog) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 116: | Line 116: | ||
Speed : (-0.55277084096044382, 0.95742708317976177, 0) |
Speed : (-0.55277084096044382, 0.95742708317976177, 0) |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
|||
{{trans|D}} |
|||
<lang csharp>using System; |
|||
namespace OrbitalElements { |
|||
class Vector { |
|||
public Vector(double x, double y, double z) { |
|||
X = x; |
|||
Y = y; |
|||
Z = z; |
|||
} |
|||
public double X { get; set; } |
|||
public double Y { get; set; } |
|||
public double Z { get; set; } |
|||
public double Abs() { |
|||
return Math.Sqrt(X * X + Y * Y + Z * Z); |
|||
} |
|||
public static Vector operator +(Vector lhs, Vector rhs) { |
|||
return new Vector(lhs.X + rhs.X, lhs.Y + rhs.Y, lhs.Z + rhs.Z); |
|||
} |
|||
public static Vector operator *(Vector self, double m) { |
|||
return new Vector(self.X * m, self.Y * m, self.Z * m); |
|||
} |
|||
public static Vector operator /(Vector self, double m) { |
|||
return new Vector(self.X / m, self.Y / m, self.Z / m); |
|||
} |
|||
public override string ToString() { |
|||
return string.Format("({0}, {1}, {2})", X, Y, Z); |
|||
} |
|||
} |
|||
class Program { |
|||
static Tuple<Vector, Vector> OrbitalStateVectors( |
|||
double semiMajorAxis, |
|||
double eccentricity, |
|||
double inclination, |
|||
double longitudeOfAscendingNode, |
|||
double argumentOfPeriapsis, |
|||
double trueAnomaly |
|||
) { |
|||
Vector mulAdd(Vector v1, double x1, Vector v2, double x2) { |
|||
return v1 * x1 + v2 * x2; |
|||
} |
|||
Tuple<Vector, Vector> rotate(Vector iv, Vector jv, double alpha) { |
|||
return new Tuple<Vector, Vector>( |
|||
mulAdd(iv, +Math.Cos(alpha), jv, Math.Sin(alpha)), |
|||
mulAdd(iv, -Math.Sin(alpha), jv, Math.Cos(alpha)) |
|||
); |
|||
} |
|||
var i = new Vector(1, 0, 0); |
|||
var j = new Vector(0, 1, 0); |
|||
var k = new Vector(0, 0, 1); |
|||
var p = rotate(i, j, longitudeOfAscendingNode); |
|||
i = p.Item1; j = p.Item2; |
|||
p = rotate(j, k, inclination); |
|||
j = p.Item1; |
|||
p = rotate(i, j, argumentOfPeriapsis); |
|||
i = p.Item1; j = p.Item2; |
|||
var l = semiMajorAxis * ((eccentricity == 1.0) ? 2.0 : (1.0 - eccentricity * eccentricity)); |
|||
var c = Math.Cos(trueAnomaly); |
|||
var s = Math.Sin(trueAnomaly); |
|||
var r = l / (1.0 + eccentricity * c); |
|||
var rprime = s * r * r / l; |
|||
var position = mulAdd(i, c, j, s) * r; |
|||
var speed = mulAdd(i, rprime * c - r * s, j, rprime * s + r * c); |
|||
speed /= speed.Abs(); |
|||
speed *= Math.Sqrt(2.0 / r - 1.0 / semiMajorAxis); |
|||
return new Tuple<Vector, Vector>(position, speed); |
|||
} |
|||
static void Main(string[] args) { |
|||
var res = OrbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0); |
|||
Console.WriteLine("Position : {0}", res.Item1); |
|||
Console.WriteLine("Speed : {0}", res.Item2); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Position : (0.77942284339868, 0.450000034653684, 0) |
|||
Speed : (-0.552770840960444, 0.957427083179762, 0)</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 219: | Line 311: | ||
<pre>Position : (0.779423, 0.45, 0) |
<pre>Position : (0.779423, 0.45, 0) |
||
Speed : (-0.552771, 0.957427, 0)</pre> |
Speed : (-0.552771, 0.957427, 0)</pre> |
||
=={{header|C#}}== |
|||
{{trans|D}} |
|||
<lang csharp>using System; |
|||
namespace OrbitalElements { |
|||
class Vector { |
|||
public Vector(double x, double y, double z) { |
|||
X = x; |
|||
Y = y; |
|||
Z = z; |
|||
} |
|||
public double X { get; set; } |
|||
public double Y { get; set; } |
|||
public double Z { get; set; } |
|||
public double Abs() { |
|||
return Math.Sqrt(X * X + Y * Y + Z * Z); |
|||
} |
|||
public static Vector operator +(Vector lhs, Vector rhs) { |
|||
return new Vector(lhs.X + rhs.X, lhs.Y + rhs.Y, lhs.Z + rhs.Z); |
|||
} |
|||
public static Vector operator *(Vector self, double m) { |
|||
return new Vector(self.X * m, self.Y * m, self.Z * m); |
|||
} |
|||
public static Vector operator /(Vector self, double m) { |
|||
return new Vector(self.X / m, self.Y / m, self.Z / m); |
|||
} |
|||
public override string ToString() { |
|||
return string.Format("({0}, {1}, {2})", X, Y, Z); |
|||
} |
|||
} |
|||
class Program { |
|||
static Tuple<Vector, Vector> OrbitalStateVectors( |
|||
double semiMajorAxis, |
|||
double eccentricity, |
|||
double inclination, |
|||
double longitudeOfAscendingNode, |
|||
double argumentOfPeriapsis, |
|||
double trueAnomaly |
|||
) { |
|||
Vector mulAdd(Vector v1, double x1, Vector v2, double x2) { |
|||
return v1 * x1 + v2 * x2; |
|||
} |
|||
Tuple<Vector, Vector> rotate(Vector iv, Vector jv, double alpha) { |
|||
return new Tuple<Vector, Vector>( |
|||
mulAdd(iv, +Math.Cos(alpha), jv, Math.Sin(alpha)), |
|||
mulAdd(iv, -Math.Sin(alpha), jv, Math.Cos(alpha)) |
|||
); |
|||
} |
|||
var i = new Vector(1, 0, 0); |
|||
var j = new Vector(0, 1, 0); |
|||
var k = new Vector(0, 0, 1); |
|||
var p = rotate(i, j, longitudeOfAscendingNode); |
|||
i = p.Item1; j = p.Item2; |
|||
p = rotate(j, k, inclination); |
|||
j = p.Item1; |
|||
p = rotate(i, j, argumentOfPeriapsis); |
|||
i = p.Item1; j = p.Item2; |
|||
var l = semiMajorAxis * ((eccentricity == 1.0) ? 2.0 : (1.0 - eccentricity * eccentricity)); |
|||
var c = Math.Cos(trueAnomaly); |
|||
var s = Math.Sin(trueAnomaly); |
|||
var r = l / (1.0 + eccentricity * c); |
|||
var rprime = s * r * r / l; |
|||
var position = mulAdd(i, c, j, s) * r; |
|||
var speed = mulAdd(i, rprime * c - r * s, j, rprime * s + r * c); |
|||
speed /= speed.Abs(); |
|||
speed *= Math.Sqrt(2.0 / r - 1.0 / semiMajorAxis); |
|||
return new Tuple<Vector, Vector>(position, speed); |
|||
} |
|||
static void Main(string[] args) { |
|||
var res = OrbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0); |
|||
Console.WriteLine("Position : {0}", res.Item1); |
|||
Console.WriteLine("Speed : {0}", res.Item2); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Position : (0.77942284339868, 0.450000034653684, 0) |
|||
Speed : (-0.552770840960444, 0.957427083179762, 0)</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 618: | Line 618: | ||
Speed : (-0.5527708409604438, 0.9574270831797618, 0.0) |
Speed : (-0.5527708409604438, 0.9574270831797618, 0.0) |
||
</pre> |
</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 883: | Line 882: | ||
};</pre> |
};</pre> |
||
=={{header|Perl 6}}== |
|||
We'll use the [https://github.com/grondilu/clifford Clifford geometric algebra library] but only for the vector operations. |
|||
<lang perl6>sub orbital-state-vectors( |
|||
Real :$semimajor-axis where * >= 0, |
|||
Real :$eccentricity where * >= 0, |
|||
Real :$inclination, |
|||
Real :$longitude-of-ascending-node, |
|||
Real :$argument-of-periapsis, |
|||
Real :$true-anomaly |
|||
) { |
|||
use Clifford; |
|||
my ($i, $j, $k) = @e[^3]; |
|||
sub rotate($a is rw, $b is rw, Real \α) { |
|||
($a, $b) = cos(α)*$a + sin(α)*$b, -sin(α)*$a + cos(α)*$b; |
|||
} |
|||
rotate($i, $j, $longitude-of-ascending-node); |
|||
rotate($j, $k, $inclination); |
|||
rotate($i, $j, $argument-of-periapsis); |
|||
my \l = $eccentricity == 1 ?? # PARABOLIC CASE |
|||
2*$semimajor-axis !! |
|||
$semimajor-axis*(1 - $eccentricity**2); |
|||
my ($c, $s) = .cos, .sin given $true-anomaly; |
|||
my \r = l/(1 + $eccentricity*$c); |
|||
my \rprime = $s*r**2/l; |
|||
my $position = r*($c*$i + $s*$j); |
|||
my $speed = |
|||
(rprime*$c - r*$s)*$i + (rprime*$s + r*$c)*$j; |
|||
$speed /= sqrt($speed**2); |
|||
$speed *= sqrt(2/r - 1/$semimajor-axis); |
|||
{ :$position, :$speed } |
|||
} |
|||
say orbital-state-vectors |
|||
semimajor-axis => 1, |
|||
eccentricity => 0.1, |
|||
inclination => pi/18, |
|||
longitude-of-ascending-node => pi/6, |
|||
argument-of-periapsis => pi/4, |
|||
true-anomaly => 0;</lang> |
|||
{{out}} |
|||
<pre>{position => 0.237771283982207*e0+0.860960261697716*e1+0.110509023572076*e2, speed => -1.06193301748006*e0+0.27585002056925*e1+0.135747024865598*e2}</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
Line 1,049: | Line 1,000: | ||
?-</pre> |
?-</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 1,114: | Line 1,064: | ||
<pre>Position : (0.787295801413, 0.454545489549, 0.0) |
<pre>Position : (0.787295801413, 0.454545489549, 0.0) |
||
Speed : (-0.547722599684, 0.948683273698, 0.0)</pre> |
Speed : (-0.547722599684, 0.948683273698, 0.0)</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
We'll use the [https://github.com/grondilu/clifford Clifford geometric algebra library] but only for the vector operations. |
|||
<lang perl6>sub orbital-state-vectors( |
|||
Real :$semimajor-axis where * >= 0, |
|||
Real :$eccentricity where * >= 0, |
|||
Real :$inclination, |
|||
Real :$longitude-of-ascending-node, |
|||
Real :$argument-of-periapsis, |
|||
Real :$true-anomaly |
|||
) { |
|||
use Clifford; |
|||
my ($i, $j, $k) = @e[^3]; |
|||
sub rotate($a is rw, $b is rw, Real \α) { |
|||
($a, $b) = cos(α)*$a + sin(α)*$b, -sin(α)*$a + cos(α)*$b; |
|||
} |
|||
rotate($i, $j, $longitude-of-ascending-node); |
|||
rotate($j, $k, $inclination); |
|||
rotate($i, $j, $argument-of-periapsis); |
|||
my \l = $eccentricity == 1 ?? # PARABOLIC CASE |
|||
2*$semimajor-axis !! |
|||
$semimajor-axis*(1 - $eccentricity**2); |
|||
my ($c, $s) = .cos, .sin given $true-anomaly; |
|||
my \r = l/(1 + $eccentricity*$c); |
|||
my \rprime = $s*r**2/l; |
|||
my $position = r*($c*$i + $s*$j); |
|||
my $speed = |
|||
(rprime*$c - r*$s)*$i + (rprime*$s + r*$c)*$j; |
|||
$speed /= sqrt($speed**2); |
|||
$speed *= sqrt(2/r - 1/$semimajor-axis); |
|||
{ :$position, :$speed } |
|||
} |
|||
say orbital-state-vectors |
|||
semimajor-axis => 1, |
|||
eccentricity => 0.1, |
|||
inclination => pi/18, |
|||
longitude-of-ascending-node => pi/6, |
|||
argument-of-periapsis => pi/4, |
|||
true-anomaly => 0;</lang> |
|||
{{out}} |
|||
<pre>{position => 0.237771283982207*e0+0.860960261697716*e1+0.110509023572076*e2, speed => -1.06193301748006*e0+0.27585002056925*e1+0.135747024865598*e2}</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |