我试图在一个 20x20x10 英里的假想盒子内并在 10 分钟内寻找可能的碰撞。所有平面都有一组位置(x,y,z)和速度。我每秒都在测试碰撞并打印出结果,但似乎我有一个无限循环,而且我从 predictX、predictY、predictZ 方法得到的结果都是一样的。任何关于我如何解决问题的建议将不胜感激!
import java.util.Random;
public class Plane {
double[] p=new double[3];
double[] v=new double[3];
public Plane(double[] p, double[] v)
{
this.p=p;
this.v=v;
}
public static void main(String[] args) {
int NUMBER_OF_PLANE = 100;
Random r=new Random(); //initialize random seed
int i;
double[] p={.1,.1,.1}; double[] v={.1,.1,.1};
Plane[] ac= new Plane [100];
for (i=0; i<NUMBER_OF_PLANE;i++){
ac[i] = new Plane (p, v);
ac[i].p[0]=20*r.nextDouble();
ac[i].p[1]=20*r.nextDouble();
ac[i].p[2]=10*r.nextDouble();
ac[i].v[0]=100+500*r.nextDouble();
if (r.nextBoolean()) v[0] = -v[0];
ac[i].v[1]=100+500*r.nextDouble();
if (r.nextBoolean()) v[1] = -v[1];
ac[i].v[2]=40*r.nextDouble()-20;
ac[i] = new Plane (p,v);
}
collision(ac);
}
public double predictX(double t) //predicts the position(x) of plane at time t
{
double x;
x = this.p[0] + (t * v[0]);
return x;
}
public double predictY(double t) //predicts the position(y) of plane at time t
{
double y;
y = this.p[1] + (t * v[1]);
return y;
}
public double predictZ(double t) //predicts the height of plane at time t
{
double z;
z = this.p[2] + (t * v[2]);
return z;
}
public static void collision(Plane[] list)
{
double time=0;
while (time<=0.166667){//timer. 0.166667 hours = 10 minutes
for (int i=0; i<list.length; i++){
for (int j=i+1; j<list.length; j++)
{
double iX = list[i].predictX(time);
double iY = list[i].predictY(time);
double iZ = list[i].predictZ(time);
double jX = list[j].predictX(time);
double jY = list[j].predictY(time);
double jZ = list[j].predictZ(time);
if (iX==jX && iY==jY && iZ==jZ){
System.out.println("Warning to Aircraft #"+i+": collision in "+time+" seconds with Aircraft #"+j);
System.out.println("Position "+iX+", "+iY+" Altitude "+iZ);}
}
}
time+=0.000277778; //1 second
}
}
}