我在3D中有以下6点。您可以在下面的代码的pointXYZ
变量中看到每个点的XYZ坐标。每个点也有一个id
,存储在pointIDs
变量中。 pointXYZ[0]
对应于pointIDs[0]
。
问题:如何以一致的方式从以下位置订购积分:
pointIDs = [463, 478, 464, 1169, 477, 462]
首字母
到
pointIDs = [463, 464, 478, 477, 1169, 462]
顺时针
或
pointIDs = [463, 464, 462, 1169, 477, 478]
逆时针
注意:我了解3D中的cw或ccw取决于从哪一侧看。因此,cw或ccw对我来说无关紧要,应该保持一致,也不要紧要从哪里开始,464只是一个示例。第一个id
是总是中心。
现在,在calculate normal和this answer的帮助下,我想到了下面的代码。但是,我很难从那里对其进行排序。非常感谢您的帮助。
function vectorAB(a, b) {
return [b[0] - a[0], b[1] - a[1], b[2] - a[2]];
}
var pointIDs = [463, 478, 464, 1169, 477, 462];
//console.log(pointIDs);
var pointXYZ = [
[5.951940485039203, 0, -2.2214890192281618],
[5.876896688975295, -0.13547705169471963, -2.4092198479124574],
[5.9742796273835745, 0.22293478422146445, -2.1491607701178843],
[5.9742796273835745, -0.22293478422146445, -2.1491607701178843],
[5.876896688975295, 0.13547705169471963, -2.4092198479124574],
[6.034465603999274, 0, -1.9864123122226367]
];
//console.log(pointXYZ);
var vectors = [];
for (var i = 1; i < pointIDs.length; i++) {
vectors.push(vectorAB(pointXYZ[i], pointXYZ[0]));
}
//console.log(vectors);
var normal = math.cross(vectors[0], vectors[1]);
console.log("id", pointIDs[1], "and", pointIDs[2],
math.dot(normal, math.cross(vectors[0], vectors[1])));
console.log("id", pointIDs[2], "and", pointIDs[3],
math.dot(normal, math.cross(vectors[1], vectors[2])));
console.log("id", pointIDs[3], "and", pointIDs[4],
math.dot(normal, math.cross(vectors[2], vectors[3])));
console.log("id", pointIDs[4], "and", pointIDs[5],
math.dot(normal, math.cross(vectors[3], vectors[4])));
console.log("id", pointIDs[5], "and", pointIDs[4],
math.dot(normal, math.cross(vectors[4], vectors[3])));
console.log("id", pointIDs[4], "and", pointIDs[2],
math.dot(normal, math.cross(vectors[3], vectors[1])));
console.log("id", pointIDs[4], "and", pointIDs[3],
math.dot(normal, math.cross(vectors[3], vectors[2])));
console.log("id", pointIDs[1], "and", pointIDs[5],
math.dot(normal, math.cross(vectors[0], vectors[4])));
<script src="https://unpkg.com/mathjs@5.4.0/dist/math.min.js"></script>