java 中的 X 和 Y 坐标循环

X and Y coordinate loop in java

我正在尝试制作一个 for 循环,它将通过 9 个不同的点集,并在我的主驱动程序中与另外两个 类 一起执行它们。我现在很困惑,无法弄清楚如何在 for 循环再次运行之前设置第二个 (pointC2)。

坐标是:

X:{{86.92, 70.93, 97.74, 30.90, 51.66, 0.83, 55.91, 32.92, 65.26, 83.90},

Y:{2.47, 27.81, 34.36, 35.14, 31.70,21.77, 66.62, 75.23, 72.53, 4.710}};  

(86.92, 2.47) = (x1,y1)

(70.93, 27.81) = (x2,y2)

我尝试将其设置为多维循环,但无法获得正确的计数器。

赋值的基础是将笛卡尔坐标转为极坐标,然后求(x1,y1)(x2,y2)(x3,y3)的距离,等等

这是我需要在 for 循环之后执行的代码:

      //calls point1 from Cartesian
        Cartesian pointC1 = new Cartesian(x1, y1);

      //calls point2 from Cartesian
        Cartesian pointC2 = new Cartesian(x2, y2);

        double answer1 = Cartesian.distance(pointC1, pointC2);

      //prints out point1 and point2
        System.out.println("Point 1 = " + pointC2 + " Point 2 = " + pointC1);

      //prints out sum 
        System.out.println("Distance: " + answer1);

then goes again but this time from (x2,y2) and (x3, y3)

我可以告诉你一个程序而不是编码,但你必须先研究一下arrays

定义,

array X: {{86.92, 70.93, 97.74, 30.90, 51.66, 0.83, 55.91, 32.92, 65.26, 83.90},

array Y: {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710}};

然后,

begin for loop from i = 0 to array size -1
x1 = array X [i];
y1 = array Y [i];
// rest of your code goes here
// ...
end for loop
double[] x = {86.92, 70.93, 97.74, 30.90, 51.66, 0.83, 55.91, 32.92, 65.26, 83.90};
double[] y = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710};
Cartesian[] pointC = new Cartesian[Math.min(x.length, y.length)];
double[] distance  = new double[pointC.length - 1];
for(int i = 0; i < pointC.length; i++){
    pointC[i] = new Cartesian(x[i], y[i]);
}
for(int i = 0; i < distance.length; i++){
    distance[i] = Cartesian.distance(pointC[i], pointC[i+1]);
}