Orbital elements: Difference between revisions

Content added Content deleted
(Added Go)
m (→‎{{header|Java}}: Fixed 'long lines' problem.)
Line 210: Line 210:


=={{header|Java}}==
=={{header|Java}}==
{{lines too long|Java}}
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Java>public class OrbitalElements {
<lang Java>public class OrbitalElements {
private static class Vector {
private static class Vector {
private double x, y, z;
private double x, y, z;

public Vector(double x, double y, double z) {
public Vector(double x, double y, double z) {
this.x = x;
this.x = x;
Line 221: Line 220:
this.z = z;
this.z = z;
}
}

public Vector plus(Vector rhs) {
public Vector plus(Vector rhs) {
return new Vector(x + rhs.x, y + rhs.y, z + rhs.z);
return new Vector(x + rhs.x, y + rhs.y, z + rhs.z);
}
}

public Vector times(double s) {
public Vector times(double s) {
return new Vector(s * x, s * y, s * z);
return new Vector(s * x, s * y, s * z);
}
}

public Vector div(double d) {
public Vector div(double d) {
return new Vector(x / d, y / d, z / d);
return new Vector(x / d, y / d, z / d);
}
}

public double abs() {
public double abs() {
return Math.sqrt(x * x + y * y + z * z);
return Math.sqrt(x * x + y * y + z * z);
}
}

@Override
@Override
public String toString() {
public String toString() {
Line 243: Line 242:
}
}
}
}

private static Vector mulAdd(Vector v1, Double x1, Vector v2, Double x2) {
private static Vector mulAdd(Vector v1, Double x1, Vector v2, Double x2) {
return v1.times(x1).plus(v2.times(x2));
return v1.times(x1).plus(v2.times(x2));
}
}

private static Vector[] rotate(Vector i, Vector j, double alpha) {
private static Vector[] rotate(Vector i, Vector j, double alpha) {
return new Vector[]{
return new Vector[]{
Line 254: Line 253:
};
};
}
}

private static Vector[] orbitalStateVectors(double semimajorAxis, double eccentricity, double inclination, double longitudeOfAscendingNode, double argumentOfPeriapsis, double trueAnomaly) {
private static Vector[] orbitalStateVectors(
double semimajorAxis, double eccentricity,
double inclination, double longitudeOfAscendingNode,
double argumentOfPeriapsis, double trueAnomaly
) {
Vector i = new Vector(1, 0, 0);
Vector i = new Vector(1, 0, 0);
Vector j = new Vector(0, 1, 0);
Vector j = new Vector(0, 1, 0);
Vector k = new Vector(0, 0, 1);
Vector k = new Vector(0, 0, 1);

Vector[] p = rotate(i, j, longitudeOfAscendingNode);
Vector[] p = rotate(i, j, longitudeOfAscendingNode);
i = p[0];
i = p[0];
Line 268: Line 271:
i = p[0];
i = p[0];
j = p[1];
j = p[1];

double l = semimajorAxis * ((eccentricity == 1.0) ? 2.0 : (1.0 - eccentricity * eccentricity));
double l = (eccentricity == 1.0) ? 2.0 : 1.0 - eccentricity * eccentricity;
l *= semimajorAxis;
double c = Math.cos(trueAnomaly);
double c = Math.cos(trueAnomaly);
double s = Math.sin(trueAnomaly);
double s = Math.sin(trueAnomaly);
Line 278: Line 282:
speed = speed.div(speed.abs());
speed = speed.div(speed.abs());
speed = speed.times(Math.sqrt(2.0 / r - 1.0 / semimajorAxis));
speed = speed.times(Math.sqrt(2.0 / r - 1.0 / semimajorAxis));

return new Vector[]{position, speed};
return new Vector[]{position, speed};
}
}

public static void main(String[] args) {
public static void main(String[] args) {
Vector[] ps = orbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0);
Vector[] ps = orbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0);
Line 288: Line 292:
}
}
}</lang>
}</lang>

{{out}}
{{out}}
<pre>Position : (0.7794228433986797, 0.4500000346536842, 0.0000000000000000)
<pre>Position : (0.7794228433986797, 0.4500000346536842, 0.0000000000000000)