Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

任意多边形顺逆时针判断 #2

Open
liuxq opened this issue Oct 29, 2016 · 2 comments
Open

任意多边形顺逆时针判断 #2

liuxq opened this issue Oct 29, 2016 · 2 comments
Labels

Comments

@liuxq
Copy link
Owner

liuxq commented Oct 29, 2016

今天在工作上遇到了需要判断一个给定的任意多边形是逆时针还是顺时针的问题。
如果是凸多边形,那看任意两个边的向量做一个叉乘就知道方向了。考虑到有可能是凹多边形,所以我判断了所有相邻的边的向量叉乘,然后取大于一般的方向。具体代码如下:

//判断多边形是逆时针还是顺时针, true:逆时针

    bool isAntiClock(vector<Vec2d>& polygon)
    {
        int sz = polygon.size();
        int greaterC = 0, lessC = 0;
        for (int i = 0; i < polygon.size(); i++)
        {
            Vec2d a = polygon[i];
            Vec2d b = polygon[(i+1)%sz];
            Vec2d c = polygon[(i+2)%sz];
            if ((b-a)/(c-b) > 0)
                greaterC ++;
            else
                lessC ++;
        }
        if (greaterC > lessC)
        {
            return true;
        }
        else
            return false;
    }

之后,我查了一下网上的资料,用的方法更科学高效,是找到多边形最左下角的节点,然后看该节点相邻两个边的向量方向。因为是最左下角,所以一定是一个凸的节点,顿时觉得自己low爆儿了。

@liuxq liuxq added the blog label Oct 29, 2016
@PENGYESYSU
Copy link

是否一定要左下角呢?最左边的点似乎就可以了

@falconlulu
Copy link

代码测试了下好像没有办法监测出来呀,我用了另外的一种方法试了试 ok https://blog.csdn.net/QLU_minoz/article/details/89185726
多多指教

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants