Orbital elements: Difference between revisions

Added Wren
m (→‎version 2: aligned a PARSE statement's parameters.)
(Added Wren)
Line 1,655:
 
<pre>Position: 0.779423 0.450000 0.000000; Speed: -0.552771 0.957427 0.000000</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<lang ecmascript>class Vector {
construct new(x, y, z) {
_x = x
_y = y
_z = z
}
 
x { _x }
y { _y }
z { _z }
 
+(other) { Vector.new(_x + other.x, _y + other.y, _z + other.z) }
*(m) { Vector.new(_x * m, _y * m, _z * m) }
 
/(d) { this * (1/d) }
 
abs { (_x *_x + _y *_y + _z * _z).sqrt }
 
toString { "(%(_x), %(_y), %(_z))" }
}
 
var orbitalStateVectors = Fn.new { |semimajorAxis, eccentricity, inclination,
longitudeOfAscendingNode, argumentOfPeriapsis, trueAnomaly|
var i = Vector.new(1, 0, 0)
var j = Vector.new(0, 1, 0)
var k = Vector.new(0, 0, 1)
 
var mulAdd = Fn.new { |v1, x1, v2, x2| v1 * x1 + v2 * x2 }
 
var rotate = Fn.new { |i, j, alpha|
return [mulAdd.call(i, alpha.cos, j, alpha.sin),
mulAdd.call(i, -alpha.sin, j, alpha.cos)]
}
 
var p = rotate.call(i, j, longitudeOfAscendingNode)
i = p[0]
j = p[1]
p = rotate.call(j, k, inclination)
j = p[0]
p = rotate.call(i, j, argumentOfPeriapsis)
i = p[0]
j = p[1]
 
var l = semimajorAxis * ((eccentricity == 1) ? 2 : (1 - eccentricity * eccentricity))
var c = trueAnomaly.cos
var s = trueAnomaly.sin
var r = l / (1 + eccentricity * c)
var rprime = s * r * r / l
var position = mulAdd.call(i, c, j, s) * r
var speed = mulAdd.call(i, rprime * c - r * s, j, rprime * s + r * c)
speed = speed / speed.abs
speed = speed * (2 / r - 1 / semimajorAxis).sqrt
return [position, speed]
}
 
var ps = orbitalStateVectors.call(1, 0.1, 0, 355 / (113 * 6), 0, 0)
System.print("Position : %(ps[0])")
System.print("Speed : %(ps[1])")</lang>
 
{{out}}
<pre>
Position : (0.77942284339868, 0.45000003465368, 0)
Speed : (-0.55277084096044, 0.95742708317976, 0)
</pre>
 
=={{header|zkl}}==
9,490

edits