【问题标题】:How to determine the type of polygon如何确定多边形的类型
【发布时间】:2017-04-05 21:37:54
【问题描述】:

用户输入第 n 个点。我需要检查是否存在多边形,然后确定类型 - 凹多边形或凸多边形。我知道一个多边形是凸的,如果它的每个角度都在 180 度以下。所以问题归结为找到多边形的每个内角。我一直在寻找公式或算法,但没有成功。

示例:

输入:n = 4;

第 1 点:(5;6)

点2:(4;-5)

点3:(-5;4)

Point4: (-5;5)

预期输出:多边形是凸的

这是目前的代码:现在它只找到平面中点之间的最大和最小距离。

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    double a[15][2];
    int n;
    cin >> n;
    if (n <= 0 && n > 15)
        return 1;

    for (int i = 0; i < n; i++)
    {
        cout << "x" << i << " = , y" << i << " = ";
        cin >> a[i][0] >> a[i][1];
    }

    double maxDistance = 0.0;
    double minDistance = 0.0;
    double maxpoint1[2];
    double maxpoint2[2];
    double minpoint1[2];
    double minpoint2[2];

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (j != i)
            {
                double x1 = a[i][0];
                double x2 = a[j][0];
                double y1 = a[i][1];
                double y2 = a[j][1];
                double currentDistance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

                if (currentDistance > maxDistance)
                {
                    maxDistance = currentDistance;
                    maxpoint1[0] = x1;
                    maxpoint1[1] = y1;
                    maxpoint2[0] = x2;
                    maxpoint2[1] = y2;

                }

                if (minDistance > currentDistance)
                {
                    currentDistance = minDistance;
                    minpoint1[0] = x1;
                    minpoint1[1] = y1;
                    minpoint2[0] = x2;
                    minpoint2[1] = y2;
                }

                cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2;
                cout << endl << "Distance is " << currentDistance;
                cout << endl;
            }
        }
    }

    cout << "The max distance is: " << maxDistance << " between x1 = " << maxpoint1[0] << " y1 = " << maxpoint1[1] << " and x2 = " << maxpoint2[0] << " y2 = " << maxpoint2[1];
    cout << "The min distance is: " << minDistance << " between x1 = " << minpoint1[0] << " y1 = " << minpoint1[1] << " and x2 = " << minpoint2[0] << " y2 = " << minpoint2[1];


    return 0;
}

【问题讨论】:

    标签: c++ algorithm geometry polygon coordinate-systems


    【解决方案1】:

    要确定多边形是凸的还是凹的,只需检查所有连续点三元组CrossProduct(P[0], P[1], P[2]) etc 的叉积符号。举例

    CrossProductSign(A, B, C) = 
                   SignOf((B.X - A.X) * (C.Y - B.Y) - (B.Y - A.Y) * (C.X - B.X))
    

    对于凸一,所有叉积必须具有相同的符号(+ 或 -)。

    它是如何工作的:对于凸多边形,每个三元组都在同一侧转弯(或顺时针,或逆时针,具体取决于行走方向)。对于凹面,一些符号会有所不同(内角超过 180 度)。请注意,您不需要计算角度值。

    【讨论】:

      【解决方案2】:

      如果要求两条边之间的角度,请使用向量的叉积或点积。

      一个点 b = |a||b| cos(angle_between_vectors) = a[0]*b[0] + a[1]*b[1] + a[2]*b[2]

      内角为 (pi - angle_between_vectors)

      哦,顺便说一下,多边形可能会与自身相交,这在许多用例中也是有害的问题。您的定义将无法检测到这一点......例如复杂四边形的所有角度都小于 90 度。

      这不是确定多边形是否为凸面的唯一方法,而且可能是大多数计算丰富的方法之一? 点积的问题是它的符号会显示角度是否小于或大于 pi/2。确定多边形是否复杂或非凸的正确方法是检查转向方向是否发生变化。为此,您需要交叉产品。对于二维向量,它们叉积的结果只有 z 分量(垂直于平面),它的符号决定了旋转发生的方式。

      问题已经在这里了。

      How do determine if a polygon is complex/convex/nonconvex?

      【讨论】:

        猜你喜欢
        • 2014-04-05
        • 1970-01-01
        • 1970-01-01
        • 2015-07-26
        • 1970-01-01
        • 2013-03-11
        • 1970-01-01
        • 2016-10-03
        相关资源
        最近更新 更多