Home >Web Front-end >JS Tutorial >How to determine whether a shape is convex in js (code)
The content of this article is about how js determines whether a shape is convex (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
//--------------判断是否是凸多边形函数------------------------- // p:顶点数组(数组对象) n:顶点个数;1:凸集;-1:凹集;0:曲线不符合要求无法计算 function convex(p,n) { var j,k,z; var flag = 0; if (n<3){ // console.log("不符合要求") return 0; } for (var i=0;i<n;i++) { j = (i + 1) % n; k = (i + 2) % n; z = (p[j].x - p[i].x) * (p[k].y - p[j].y); z -= (p[j].y - p[i].y) * (p[k].x - p[j].x); if (z < 0){ flag |= 1; } else if (z > 0){ flag |= 2; } if (flag == 3){ // console.log("凹多边形,不符合要求") return -1; //CONCAVE } } if (flag != 0){ // console.log("凸多边形") return 1; //CONVEX } else{ return 0; } }
Related recommendations:
How does js determine whether the email format is correct
How does js determine whether the iframe is fully loaded
The above is the detailed content of How to determine whether a shape is convex in js (code). For more information, please follow other related articles on the PHP Chinese website!