本文主要是介绍JavaScript 凹凸多边形判断,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// 凸多边形示例点
var points = [{ "x": 121.13461725174781, "y": 28.85972342614326, "z": 6.5083743913337795 },{ "x": 121.13514790352059, "y": 28.85893044194414, "z": 5.239703380752392 },{ "x": 121.13631312370927, "y": 28.85945345020736, "z": 5.098438720064432 },{ "x": 121.1359027717907, "y": 28.86041176963575, "z": 5.352710221996978 },{ "x": 121.13461725174781, "y": 28.85972342614326, "z": 6.5083743913337795 }
]
// 凹多边形示例点
var points = [{ "x": 121.1347274517314, "y": 28.859676273631692, "z": 6.0256031727820805 },{ "x": 121.13506353979167, "y": 28.858930341787225, "z": 5.169401984643744 },{ "x": 121.13610244166651, "y": 28.85937573746748, "z": 4.970347026883167 },{ "x": 121.13660444154766, "y": 28.859446699269476, "z": 5.103525544574725 },{ "x": 121.13605631779419, "y": 28.8600846928507, "z": 5.080870114806739 },{ "x": 121.1347274517314, "y": 28.859676273631692, "z": 6.0256031727820805 }
]
/*** @param {*} p 顶点数组(数组对象)* @returns 1:凸集;-1:凹集;0:曲线不符合要求无法计算*/
function concave(p) {var n = p.length;//顶点个数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;}
}
这篇关于JavaScript 凹凸多边形判断的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!