javascript - find convex an concave corners in a polygon - Stack Overflow

I am trying to detect if a corner is concave or convex in an arbitrary polygon.I made the function bel

I am trying to detect if a corner is concave or convex in an arbitrary polygon. I made the function below that putes the angle between all edge-pairs. however one never knows if the if it is the inner or the outer corner angle that it returns. I have no idea how to go about this. Any help appreciated!!!!

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);
}

I am trying to detect if a corner is concave or convex in an arbitrary polygon. I made the function below that putes the angle between all edge-pairs. however one never knows if the if it is the inner or the outer corner angle that it returns. I have no idea how to go about this. Any help appreciated!!!!

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);
}

Share Improve this question asked Nov 17, 2012 at 0:23 timkadotimkado 2,0622 gold badges20 silver badges30 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Here's some code to work out concave vs convex corners:

// 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';
}

An easy way to do this is by assessing vector determinants.

First, we make sure that the polygon is clockwise/anti-clockwise (using shoelace method).

Let's go with clockwise. That means all your interior angles can be considered to be drawn anti-clockwise between respective adjacent sides.

Say for a particular angle ABC between sides AB and BC, we can calculate the determinant between vectors BA and BC (ad - bc formula).

If the determinant is <= 0, you go for the concave angle (ie. 360 - angle between vectors). If det is greater than 0, we take the convex angle. Hope this helps

A bit more detail about what your trying to do might be helpful. That being said, seems like an algorithm for generating the convex hull might be useful. Such as the following, which is probably the best balance of efficiency and ease of implementation:

http://en.wikibooks/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain

Once you know which points are part of the convex hull, the rest should be a bit more straight forward.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745204270a4616509.html

相关推荐

  • javascript - find convex an concave corners in a polygon - Stack Overflow

    I am trying to detect if a corner is concave or convex in an arbitrary polygon.I made the function bel

    16小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信