gpt4 book ai didi

javascript - 在多边形中找到凸 Angular 和凹 Angular

转载 作者:行者123 更新时间:2023-11-29 10:20:23 26 4
gpt4 key购买 nike

我正在尝试检测任意多边形中的 Angular 是凹的还是凸的。我创建了下面的函数来计算所有边对之间的 Angular 。然而,人们永远不知道它返回的是内 Angular 还是外 Angular 。我不知道该怎么做。任何帮助表示赞赏!!!!

function findConvexCorner (pt){
var isCornerConvex = [];
for (var i =0; i < pt.length ;i++)
{
var lastPt = pt.length -1;
if (i==0){
var vec1 = vec3.createXYZ( pt[lastPt].x - pt[i].x , pt[lastPt].y - pt[i].y ,0.0);
var vec2 = vec3.createXYZ( pt[i].x - pt[i+1].x , pt[i].y - pt[i+1].y ,0.0);
vec3.normalize(vec1);vec3.normalize(vec2);
isCornerConvex.push(Math.acos(vec3.dot(vec1,vec2))*180/Math.PI);}
else if(i == lastPt){
var vec2 = vec3.createXYZ( pt[i-1].x - pt[i].x , pt[i-1].y - pt[i].y ,0.0);
var vec1 = vec3.createXYZ( pt[0].x - pt[i].x , pt[0].y - pt[i].y ,0.0);
vec3.normalize(vec1);vec3.normalize(vec2);
isCornerConvex.push(Math.acos(vec3.dot(vec1,vec2))*180/Math.PI);}
else{
var vec1 = vec3.createXYZ( pt[i-1].x - pt[i].x , pt[i-1].y - pt[i].y ,0.0);
var vec2 = vec3.createXYZ( pt[i+1].x - pt[i].x , pt[i+1].y - pt[i].y ,0.0);
vec3.normalize(vec1);vec3.normalize(vec2);
isCornerConvex.push(Math.acos(vec3.dot(vec1,vec2))*180/Math.PI);}
}
console.log("Angle: "+ isCornerConvex);
}

problem

最佳答案

下面是一些计算凹 Angular 和凸 Angular 的代码:

// this assumes nextEdge and previousEdge are vectors pointing out of a vertex and to the next one
var angle = ((Math.atan2(nextEdge.x, nextEdge.y) - Math.atan2(previousEdge.x, previousEdge.y) + Math.PI * 2) % (Math.PI * 2)) - Math.PI;

if (angle > 0) {
corner.type = 'convex';
} else if (angle < 0) {
corner.type = 'concave';
} else {
corner.type = 'straight';
}

关于javascript - 在多边形中找到凸 Angular 和凹 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com