interior angles of irregular polygon with angles > 180

时光总嘲笑我的痴心妄想 提交于 2020-01-13 14:56:07

问题


I'm trying to calculate the values shown in the picture in red i.e. the interior angles.

I've got an array of the points where lines intersect and have tried using the dot-product but it only returns the smallest angles. I need the full range of internal angles (0-359) but can't seem to find much that meets this criteria.


回答1:


Assuming your angles are in standard counterclockwise format, the following should work:

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;
    }
}

Obviously, if you are using some sort of data structure for your points, you will want to replace double points[][2] and references to it with references to your data structure.




回答2:


You can obtain full angle range (-Pi..Pi) with atan2 function:

atan2(crossproduct, dotproduct)


来源:https://stackoverflow.com/questions/28821329/interior-angles-of-irregular-polygon-with-angles-180

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!