Detect if a set of points in an array that are the vertices of a complex polygon were defined in a clockwise or counterclockwise order?

前端 未结 5 1066
说谎
说谎 2021-02-07 05:57

EDIT: I updated the program with the answer and it works great!

I am making a program (feel free to try it out) that lets users draw polygons which it then triangulates.

5条回答
  •  耶瑟儿~
    2021-02-07 06:46

    This a function function that specialized for OpenLayers. As You Can See The Condition Of Clockwise Polygon Is area<0 This Reference Confirm It.

    function IsClockwise(feature)
    {
    if(feature.geometry==null)return -1;
    var vertices=feature.geometry.getVertices();
    var area=0;
    for (var i = 0; i < (vertices.length); i++) 
        {
        j = (i + 1) % vertices.length;
        area += vertices[i].x * vertices[j].y;
        area -= vertices[j].x * vertices[i].y;
        // console.log(area);
        }
    return (area < 0);
    }
    

提交回复
热议问题