Count the number of adjacent boxes

前端 未结 3 1334
情书的邮戳
情书的邮戳 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:22

    In the scenario you describe boxes share corners, so if you computed all of the box corners then you could see which ones were touching

    // O(n)
    foreach box b {
       // compute b's other 2 corners and save them
    }
    
    // 16 * O(n^2) = O(n^2) 
    foreach box b {
       foreach box other {
          // match if any of b's corners match any of others points
       }
    }
    

    There is probably a much more efficient/elegant solution, this one is somewhat naive.

提交回复
热议问题