gpt4 book ai didi

java - 计算n角的角度(加上比较)

转载 作者:行者123 更新时间:2023-12-01 09:34:24 24 4
gpt4 key购买 nike

我正在尝试计算 n 角形状的点之间的角度。有 2 个数组保存 x 和 y 坐标。我一直收到 P13 错误,但我不明白为什么。有谁知道原因吗?

public class Ugao {

public static int nizovi(double[]a , double[]b , double alfa) {
int BrStr = 0;
for (int i = 0; i < b.length; i++) {
double P12,P13, P23;
P12 = duzina(a[i+1], b[i+1], a[i], b[i]);
P13 = duzina(a[i+1], b[i+1], a[i+2], b[i+2]); // pisemo prvo i+1 jer je to vertex tacka, ona koja je u centru ugla, zato nije od i
P23 = duzina(a[i], b[i], a[i+2], b[i+2]);

double x = ugao(P12, P13, P23);
if(x > alfa) {
System.out.print( "Stranica : " + "( " + a[i] + "," + b[i] + " )" + "( " + a[i+1] + "," + b[i+1] + " )" + " & ");
System.out.println("( " + a[i+1] + "," + b[i+1] + " )" + "( " + a[i+2] + "," + b[i+2] + " )");
System.out.println();
BrStr++;
}
}
return BrStr;
}


public static double ugao (double P12, double P13, double P23) {
return Math.acos((Math.pow(P12, 2) + Math.pow(P13, 2) - Math.pow(P23, 2))/(2 * P12 * P13));
}

public static double duzina (double vertexX1, double vertexY1, double x2, double y2) {
return Math.sqrt(Math.pow((vertexX1-x2),2)+ Math.pow((vertexY1-y2), 2));
}

public static void main(String[] args) {
// TODO Auto-generated method stub

double a[] = {12,25,3};
double b[] = {11,20,5};

nizovi(a, b, 45);

}

}

最佳答案

您超出了数组范围。

例如,如果length = 4数组包含索引0..3,但在最后一个周期使用i=3表达式运行a[i+2] 想要元素 a[5]

您可以使用模运算来获取下一个顶点索引

 current: i
next: (i+1)%length
nextnext: (i+2)%length

关于java - 计算n角的角度(加上比较),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39129995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com