简体   繁体   中英

count the number of adjacent rectangles

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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