count the number of adjacent rectangles

喜夏-厌秋 提交于 2019-12-23 02:56:19

问题


My code prints a sets of (X,Y) coordinates in 2D space in the range [0,1].

void Rect_Print() {
    cout << "In counter-clockwise fashion" << endl;
    cout << "#Rectangle    (   x0,   y0)   (   x1,   y1) " << endl;

    for (int b=0; b<Rect_Count; b++) {
       double Area = (Rect[b].x0 - Rect[b].x1) * (Rect[b].y0 - Rect[b].y1);

       cout << fixed << setprecision(4) << (b+1) <<
               "  (" << Rect[b].x0 << "," << Rect[b].y0 <<
             ")   (" << Rect[b].x1 << "," << Rect[b].y1 << ")" << endl;
    }

    cout << "Number of divisions (N = 3j-2) = " << Rect_Count << endl;
}

These points divide a unit square in a (3j-2) sub-rectangles (not uniform). For each of the specific rectangle, I would like to count the total number of rectangle adjacent to it.

Example

  1. Suppose the first coordinate divide the unit square into four rectangles like:

    In this picture you can see, there are three rectangles adjacent with rectangle-3.

  2. If I proceed that way, after my sixth step the unit square divide into 19 rectangles. So it's look like:

    Now there are five rectangles adjacent to rectangle-3. six rectangles adjacent to rectangle-11.

Suppose I have a sets of ten thousand coordinate and they sub-divide the square into small sub-rectangles. I would like to use c++ to count the number of rectangles adjacent to each of them. How can I do it?

After searching on Internet it seems the Flann may help me to do the trick. I read the user manual but could not understand how can I do that.

Can anyone help me?


回答1:


I would suggest that you structure this using something similar a quad tree (see Wikipedia Quadtree article). The difference here is that you don't evenly split the sub-nodes of the quad tree - you split at the points that get inserted.

You can then traverse the tree, searching for intersecting edges.

If a node does not have an edge that intersects your query rectangle then you do not need to recurse into its children which will save CPU time when doing this for 10,000's of rectangles, since the complexity will be logarithmic rather than linear.

The leaf tiles of the tree are your list of rectangles so you should only count rectangles that are leaves of the tree and intersect with the query rectangle.

It is also a convenient way to handle the subdivision of the rectangles - when you insert a point, you can recurse into the tree to find the rectangle to split quickly.




回答2:


You can look for a r-tree or kd-tree. The treemap algorithm works similar. A good start is to sort the boxes and put them in the tree by recursively split it at the 2 axis and look where the next box fits.



来源:https://stackoverflow.com/questions/17274934/count-the-number-of-adjacent-rectangles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!