我已經構建了Java中的太陽系模型。爲了確定一個行星的位置,它確實做了很多計算,這些計算給出了一個非常精確的值。不過,我常常對大概的位置感到滿意,如果這可能會讓它更快。因爲我在模擬中使用它非常重要,因爲這個星球的位置將被要求數百萬次。一組點之間的快速插值
目前我嘗試緩存行星在整個軌道上的位置,然後反覆使用這些座標。如果請求兩個值之間的位置,則執行線性插值。這就是我如何存儲值:
for(int t=0; t<tp; t++) {
listCoordinates[t]=super.coordinates(ti+t);
}
interpolator = new PlanetOrbit(listCoordinates,tp);
PlanetOrbit
有插值代碼:
package cometsim;
import org.apache.commons.math3.util.FastMath;
public class PlanetOrbit {
final double[][] coordinates;
double tp;
public PlanetOrbit(double[][] coordinates, double tp) {
this.coordinates = coordinates;
this.tp = tp;
}
public double[] coordinates(double julian) {
double T = julian % FastMath.floor(tp);
if(coordinates.length == 1 || coordinates.length == 0) return coordinates[0];
if(FastMath.round(T) == T) return coordinates[(int) T];
int floor = (int) FastMath.floor(T);
if(floor>=coordinates.length) floor=coordinates.length-5;
double[] f = coordinates[floor];
double[] c = coordinates[floor+1];
double[] retval = f;
retval[0] += (T-FastMath.floor(T))*(c[0]-f[0]);
retval[1] += (T-FastMath.floor(T))*(c[1]-f[1]);
retval[2] += (T-FastMath.floor(T))*(c[2]-f[2]);
return retval;
}
}
你能想到的FastMath
爲Math
但速度更快。然而,這個代碼在每次計算精確值方面速度並沒有太大提高。你對如何加快速度有什麼想法嗎?
是我讀這正確的方法'座標'實際上改變字段內的值' coordinates'? –
@RichardTingle在代碼'coordinates'的第一個片段中,爲每個't'返回一個三個值的數組,這些數組存儲在'listCoordinates'中。 'listCoordinates'被賦予'PlanetOrbit'來存儲它。從此以後從未改變,但我使用該列表來獲取該位置。 'PlanetOrbit.coordinates'是我在模擬過程中要調用的方法來找出行星的位置。 –
好吧,我強烈建議不要調用所有的「座標」。我的意思是PlanetOrbit#協調的方法。在該方法中,f =座標[floor];'然後'double [] retval = f;'然後'retval'被修改 –