简体   繁体   English

KineticJS获得所有交集

[英]KineticJS get all intersection

I was working not so long ago with KineticJS and I have a problem. 我不久前与KineticJS合作,但遇到了问题。

I can draw circles (white, blue and red), and then I'll draw a polygon along the path of the blue circles. 我可以绘制圆(白色,蓝色和红色),然后沿着蓝色圆的路径绘制多边形。

But how do I find the coordinates, or at least something about the red circles. 但是我如何找到坐标,或者至少是关于红色圆圈的坐标。 That is, about those who fall under the following polygons. 也就是说,关于那些属于以下多边形的对象。

I tried using getIntersection but I do not understand what it returns. 我尝试使用getIntersection但不了解它返回的内容。 Sorry for my bad english. 对不起,我的英语不好。 Thanks in advance!! 提前致谢!!

http://imageshack.us/scaled/thumb/42/7gok.jpg http://imageshack.us/scaled/thumb/42/7gok.jpg

I assume from your picture that all red and blue dots are positioned on a grid. 我从您的图片中假设所有红色和蓝色点都位于网格上。

This means that the x and y of any dot are clamped to that grid (axis aligned) 这意味着任何点的x和y都被夹在该网格上(轴对齐)

I further assume that the blue dots define the outside of your polygon. 我进一步假设蓝点定义了多边形的外部。

So you want to see if any particular red dot is inside that polygon? 因此,您想查看该多边形内是否有任何特定的红点?

Given these assumptions, any red dot is inside your polygon if it is surrounded by blue dots on all 4 vertical and horizontal sides 根据这些假设,如果多边形中所有垂直和水平的四个侧面都被蓝点包围,则任何红点都在多边形内

  • Blue dot to left of red dot 蓝点到红点的左边

     blueY==redY && blueX<redX 
  • Blue dot to right of red dot 红点右边的蓝点

     blueY==redY && blueX>redX 
  • Blue dot above red dot 红点上方的蓝点

     blueX==redX && blueY<redY 
  • Blue dot below red dot 红点下方的蓝点

     blueX==redX && blueY>redY 

First, you need to be able to get all blue dots, so when you're creating blue dots be sure to assign them a name property of blue 首先,您需要能够获取所有蓝点,因此在创建蓝点时,请务必为其分配一个蓝色的名称属性。

name:"blue",

Then you can test any red dot using this function that checks if that red dot is surrounded by blue dots: 然后,您可以使用此功能测试任何红点,该功能检查该红点是否被蓝点包围:

function isRedInPolygon(red){

    // get XY of the red circle
    var redX=red.getX();
    var redY=red.getY();

    // set up vars for results
    // these vars become true when blue dots are
    // directly up, down, left and right of the red dot
    var up=false;
    var down=false;
    var left=false;
    var right=false;

    // get all the blue dots into an array
    var blues=layer.get('.blue').toArray();

    for(var j=0;j<blues.length;j++){

        // get the XY of this blue
        var blue=blues[j];
        blueX=blues[j].getX();
        blueY=blues[j].getY();

        // test left/right
        if(blueX==redX){
            // left
            if(blueY<redY){ up=true; }
            // right
            if(blueY>redY){ down=true; }
        }

        // test up/down
        if(blue.Y==red.Y){
            if(blueX<redX){ left=true; }
            if(blueX>redX){ right=true; }
        }
    }

    // return true if this red dot is surrounded
    // by blue dots on all 4 sides
    return(up && down && left && right);
}

To get a list of all red objects inside the polygon 获取多边形内所有红色对象的列表

First, assign the name="red" to all red dots. 首先,将名称=“ red”分配给所有红点。

name:"red",

Then use this call to get an array of all red objects in the polygon. 然后使用此调用获取多边形中所有红色对象的数组。

var redsInside = listOfRedsInPolygon();

Using this function: 使用此功能:

function listOfRedsInPolygon(){

    var reds=layer.get('.red').toArray();

    var redsInsidePolygon=[];

    for(var i=0;i<reds.length;i++){

        if(isRedInPolygon(reds[i])){
            redsInsidePolygon.push(reds[i]);
        }
    }

    return(redsInsidePolygon);
}

Disclaimer: I have not tested this code...it might need some tweaking. 免责声明:我尚未测试此代码...可能需要进行一些调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM