Count the number of adjacent boxes

前端 未结 3 1420
情书的邮戳
情书的邮戳 2021-01-18 06:46

Suppose I have a set of (X,Y) coordinates of 1000 boxes.

         (    x1,    y1)    (    x2,    y2)      Area

         (0.0000,0.0000)    (0.3412,0.4175)           


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-18 07:30

    Let's say you're interested in box B for which the (x0, y0, x1, y1) coordinates are: (B.x0, B.y0, B.x1, B.y1)

    Then with:

    ax0 = min(x0,x1);
    ax1 = max(x0,x1);
    ay0 = min(y0,y1);
    ay1 = max(y0,y1);
    bx0 = min(B.x0,B.x1);
    bx1 = max(B.x0,B.x1);
    by0 = min(B.y0,B.y1);
    by1 = max(B.y0,B.y1);
    

    you go through the whole list of boxes (with a for loop) and you select the ones for which:

    (((ay0 <= by1 && ay1 >= by0) && (ax1 == bx0 || ax0 == bx1) || // touch on x axis
     ((ax0 <= bx1 && ax1 >= bx0) && (ay1 == by0 || ay0 == by1))   // touch on y axis
    

提交回复
热议问题