c++ - 角度 > 180 的不规则多边形的内角

标签 c++ math vector-graphics

我正在尝试计算图中以红色显示的值,即内角。

我有一个线相交点的数组,并尝试使用点积,但它只返回最小的角度。我需要完整的内角范围 (0-359),但似乎找不到符合此标准的太多。

arrow

最佳答案

假设您的角度是标准的逆时针格式,以下应该有效:

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/

相关文章:

c++ - 模板层次结构中的可选虚函数取决于参数

python - 如何在约束规划中限制零的个数

javascript - 将 .ai 或 .svg 文件放到网络上,不使用 Flash

ios - 如何将矢量图像导入 xcode 8 中的图像资源

java - 处理:弧码只创建圆。如何正确识别起始值和终止值

c++ - 如何从特定位置开始读取文件 C++

c++ - 在 C++ 中定义包含 TTF_Font 变量的 vector 时出现 "Undefined size"错误

c++ - 将输入读入 C 风格的字符串

javascript - 反转 Exp 函数。对数刻度

Ruby - 数学运算