我正在尝试计算图中以红色显示的值,即内角。
我有一个线相交点的数组,并尝试使用点积,但它只返回最小的角度。我需要完整的内角范围 (0-359),但似乎找不到符合此标准的太多。
最佳答案
假设您的角度是标准的逆时针格式,以下应该有效:
void angles(double points[][2], double angles[], int npoints){
for(int i = 0; i < npoints; i++){
int last = (i - 1 + npoints) % npoints;
int next = (i + 1) % npoints;
double x1 = points[i][0] - points[last][0];
double y1 = points[i][1] - points[last][1];
double x2 = points[next][0] - points[i][0];
double y2 = points[next][1] - points[i][1];
double theta1 = atan2(y1, x1)*180/3.1415926358979323;
double theta2 = atan2(y2, x2)*180/3.1415926358979323;
angles[i] = (180 + theta1 - theta2 + 360);
while(angles[i]>360)angles[i]-=360;
}
}
显然,如果您为您的点使用某种数据结构,您将希望将 double points[][2]
和对它的引用替换为对您的数据结构的引用。
关于c++ - 角度 > 180 的不规则多边形的内角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28821329/