Axis Aligned? Rectangles

Source: Internet
Author: User

Describe? Algorithm? takes? unsorted. Array? axis‐aligned? rectangles? Returns? rectangles?? Overlaps, huh? What if? There? Is such? A? pair.? ? Axis‐aligned? Means? That? All? Rectangle? Sides? is either? parallel? Or? Perpendicular? to? The? X‐? and? Y‐axis. ?? You? Can? Assume? each? Rectangle? Object? Has? Variables?in? It: the. x‐y? Coordinates? Upper‐left Corner and the bottom‐right? corner.

good? Answer: ? Create? A. Sorted? array? The? X? Coordinates? Left? and? Right? Edges?? The rectangles. ?? Then, the use? A? "Scanline"? Move? From? Left, right? through? The? Rectangles. ?? Keep? A? Binary? Search? The tree? containing? Y. Coordinates? Top? and? Bottom? Edges? Rectangles? That? Overlap Scanline. ?? For? Element? The?? Array,? Check? Whether? Is it? A? Left? or? Edge.? ? If? Is it? A? right? Edge,? remove? The? Corresponding? Top? and? Bottom? Edges? Bst.?? What If? It is A?left edge,? Search? The? Bst? For? rectangles? That? Overlap? Current? Rectangle, what if? There? Is One,?return? Overlap.?? Then,? Add? the?y? Coordinates?? The? Top? and? Bottom?edges? The. Rectangle? To the? Bst.?? The? Search? Takes? O (n?log?n)? time,? Since? It? takes? O (n?log?n)? To? Sort? The? Rectangles? and? each? The?? 2n? Iteration S?takes? O (log?n)? Time.

C + + Sample Code (Find all pairs of rectangles-overlap):

1#include <iostream>2#include <vector>3#include <map>4#include <algorithm>5 using namespacestd;6 7 structRect {8     int_x1, _x2, _y1, _y2;9RectintX1,intX2,intY1,inty2):Ten _x1 (x1), _x2 (x2), _y1 (y1), _y2 (y2) {} One }; A  - structXedge { -     int_x; the     int_is_left; -     int_i_rect; -Xedge (intXintIs_left,inti_rect): - _x (x), _is_left (Is_left), _i_rect (I_rect) {} + }; -  + structYedge { A     int_y; at     int_is_top; -     int_i_rect; -Yedge (intYintIs_top,inti_rect): - _y (y), _is_top (Is_top), _i_rect (I_rect) {} - }; -  in intMain () { -Vector<rect> RCTs;//vector that stores rectangles toVector<xedge> xes;//Left and right edges of the rectangles +     intx1, x2, y1, y2; -     inti =0; the      while(CIN >> x1 >> x2 >> y1 >>y2) { * rcts.push_back (Rect (x1, x2, y1, y2)); $Xes.push_back (Xedge (x1,1, i));Panax NotoginsengXes.push_back (Xedge (x2,0, i)); -i++; the     } +  A     //Sort left and right rectangles according to their x-coordinates the sort (Xes.begin (), Xes.end (), +[](Constxedge& E1,Constxedge&E2) { -             if(e1._x! = e2._x)returnE1._x <e2._x; $             Else if(E1._is_left! =e2._is_left) $                 returnE1._is_left >E2._is_left; -             Else returnE1._i_rect <E2._i_rect; -         }); the  -     //A binary search tree for top and bottom edges that overlap the "scanline"Wuyimultimap<int, yedge>Edge_map; the      for(auto& xe:xes) {//XE: "Scanline" -         if(!xe._is_left) { WuAuto Range =Edge_map.equal_range (rcts[xe._i_rect]._y1); -              for(Auto iter = Range.first; iter! = Range.second; + +ITER) { About                 if(Iter->second._i_rect = =xe._i_rect) { $ Edge_map.erase (ITER); -                      Break; -                 } -             } ARange =Edge_map.equal_range (rcts[xe._i_rect]._y2); +              for(Auto iter = Range.first; iter! = Range.second; + +ITER) { the                 if(Iter->second._i_rect = =xe._i_rect) { - Edge_map.erase (ITER); $                      Break; the                 } the             } the}Else { the             intY1 =rcts[xe._i_rect]._y1; -             inty2 =Rcts[xe._i_rect]._y2; inAuto Iter1 =edge_map.lower_bound (y1); theAuto Iter2 =edge_map.upper_bound (y2); the              for(Auto iter = iter1; iter! = Iter2; + +ITER) { Aboutcout << Xe._i_rect <<" "<< Iter->second._i_rect <<Endl; the             } theEdge_map.insert (Make_pair (Y1, Yedge (Y1,0, Xe._i_rect))); theEdge_map.insert (Make_pair (y2, Yedge (y2,1, Xe._i_rect))); +         } -     } the Bayi     return 0; the}

Axis Aligned? Rectangles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.