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 1068
说谎
说谎 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:37

    In case someone is using three.js the ShapeUtils comes with an inbuilt isClockWise method which internally uses the area method to determine the sign of the calculated area.

    isClockWise: function ( pts ) {
    
        return ShapeUtils.area( pts ) < 0;
    
    }
    

    The ShapeUtils.isClockWise Method can be found here.

    area: function ( contour ) {
    
        var n = contour.length;
        var a = 0.0;
    
        for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
    
            a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
    
        }
    
        return a * 0.5;
    
    },
    

    The ShapeUtils.area Method can be found here.

提交回复
热议问题