RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 528 | Replies: 11 | Thread Tools  Display Modes
Reply
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

plane sweepin

  #1  
Oct 20th, 2008
heya... ok, so i was browsing the net to find out if there's a good text on this algorithm, but i don't seem to be able to find one. people are obviously using it, but, hey, there's no info on it.
the part i get is the theory on how it works... moving the line through the plane and stuff of that kind, but i've never, ever seen a simple, decent c++ implementation of it. i see people talking about starting events, stopping events, doing stuff like that, but i don't have any idea how i would implement that. could someone please help me?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,687
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 32
Solved Threads: 529
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: plane sweepin

  #2  
Oct 20th, 2008
>i've never, ever seen a simple, decent c++ implementation of it
Write one. That's what I do when I can't find a satisfactory implementation to pinch. Just find a good description of the algorithm and construct the code yourself; be careful not to get stuck on descriptions of an implementation, because you'll probably end up confusing yourself if you don't see the code (case in point, your confusion over the events).
I'm here to prove you wrong.
Reply With Quote  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: plane sweepin

  #3  
Oct 20th, 2008
i would, but i'm really not talented in implementing something from scratch, my first mergesort took 10 secs to sort 100 integers, and even then, they weren't sorted. with msort it was easy cause i was able to look at other people's code, but this way, i'll never know is it good, or have i raised the complexity to...like a 10th power..?
Reply With Quote  
Join Date: Sep 2004
Posts: 6,687
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 32
Solved Threads: 529
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: plane sweepin

  #4  
Oct 20th, 2008
>i'm really not talented in implementing something from scratch
There's no such thing as talent in programming, only experience. And you can only gain experience by doing it. My first merge sort was crappy too, but now I can crank out high quality implementations without batting an eyelash.
Last edited by Narue : Oct 20th, 2008 at 4:39 pm.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: plane sweepin

  #5  
Oct 22nd, 2008
okay, so i've tried...hard, seriously. and i'm stuck. i've implemented the events, and an imaginary set to hold them in, but i'm stuck at the actual sweeping part. the problem i'm trying to solve is box union, you're given a set of rectangles ( two diagonal points ) and gotta print the area of their union. this is the code of my sweep:
  1. for( int i = 0; i < X.size(); ++i ) {
  2. int y_len = 0; // init for the y length for the inner sweep
  3.  
  4. for( int j = 0; j < Y.size(); ++j ) { // inner sweep to get the y length
  5. if( !A.cont( Y[j] ) ) continue;
  6.  
  7. if( !B.empty() ) y_len += Y[j].value - Y[j-1].value;
  8. if( B.cont( Y[j] ) ) B.remove( Y[j] );
  9. else B.insert( Y[j] );
  10. }
  11.  
  12. if( !A.empty() ) sol += y_len * ( X[i].value - X[i-1].value ); // outer sweep, adding up the area
  13. if( A.cont( X[i] ) ) A.remove( X[i] );
  14. else A.insert( X[i] );
  15. }

and.. i think i should mention: cont() is my memb. function for the set type, insert and remove also. A and B are set of events (segments in this example), X and Y are sorted sequences of events... can someone help me fix this part?
if you need the rest of the code, i can post it too! thanks!
Last edited by gregorynoob : Oct 22nd, 2008 at 11:47 am.
Reply With Quote  
Join Date: Jul 2005
Posts: 1,395
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 10
Solved Threads: 196
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: plane sweepin

  #6  
Oct 22nd, 2008
I can see getting the area of union of two rectangles defined by a series of three points. Is that good enough? I can also do it for a series of four points, if there isn't a common vertex.
Last edited by Lerner : Oct 22nd, 2008 at 3:00 pm.
Klatu Barada Nikto
Reply With Quote  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: plane sweepin

  #7  
Oct 22nd, 2008
hmm, union of n rectangles, each defined by only two points, the segments are parallel with the lines of the system (x and y).
Reply With Quote  
Join Date: Jul 2005
Posts: 1,395
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 10
Solved Threads: 196
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: plane sweepin

  #8  
Oct 22nd, 2008
I assume if you can do it for two arbitrary rectangles then you can do it for any given pair of rectangles in a group of n rectangles.

In general, if a rectangle is defined by two non adjacent points like p1(x1, y1) and p2(x2, y2), then, assuming x1 < x2 by definition, the length parallel to the x axis (call it width) is x2 - x1 and the length parallel to the y axix (call it height) is y2 - y1, assuming y1 < y2 by definition. The area is width times height.

The union of two rectangles means area of overlap of the two rectangles. That means if rectangle 1 is defined by (p1, p2) and rectangle 2 is defined by (p3, p4) where x1 < x2 and x3 < x4 by definition, then in order for there to be overlap if you sort x1, x2, x3 and x4 in ascending order. If the lowest two are x1 and x2 or x3 and x4 then there isn't overlap. If there is overlap then width of the overlap is difference between the second and third values in the sorted sequence. A similar procedure can be done with the y components to get the height of the overlap. This then should allow you to calculate the area of the overlap of any two rectangles oriented in with sides parallel to the x and y axis and each with x1 < x2 and y1 < y2.

Exactly how you plan to use this algorithm in a plane sweep algorithm I don't know.
Klatu Barada Nikto
Reply With Quote  
Join Date: Jun 2008
Posts: 86
Reputation: gregorynoob is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
gregorynoob gregorynoob is offline Offline
Junior Poster in Training

Re: plane sweepin

  #9  
Oct 23rd, 2008
you're assuming too much, nobody guarantees any of those cases you mentioned. x1 < x2? why? it can be the opposite! also.. it isn't true that union is the area of the overlap.. it's the total area the rectangles cover together!
Reply With Quote  
Join Date: Jul 2005
Posts: 1,395
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 10
Solved Threads: 196
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: plane sweepin

  #10  
Oct 23rd, 2008
Was my geometry class so long ago that I've forgotten the difference between union and intersection? I guess so! (sorry)

But don't despair. All that work hasn't gone for naught, at least in the scenario of two rectangles. I suspect the definition of the area of the union of two rectangles would be the total area of the two rectangles if they don't overlap and the total area of the two rectangles minus the area of overlap if they do overlap. Therefore, in the two rectangle scenario, knowing how to calculate the intersection of the two rectangles could be very helpful!

Determining the union of three (or more) rectangles does increase the complexity dramatically; and I confess I don't know how to do it using the computer. Manually I might find the perimeter of what I would call the irregular polygon formed by the external edges of all the overlapping rectangles and then chop up the irregular polygon into individual rectangles and add up the the total area of all the resultant rectangles. Alternatively, I might find the area of a bounding rectangle followed by the area of all the rectangular spaces between the bounding rectangle and the perimeter of the irregular polygon and then subtract the area of the spaces from the aread of the bounding rectangle. I seem to remember something about an algorithm for determining the external boundaries of irregular polygons somewhere. I'll see what I can find since it seems like an intriguing problem.

My reason for requiring x1 < x2 was to give some order to things so they aren't completely random. After all, the rectangle ((4, 3), (1, 2)) is the same as ((1, 2), (4,3)). So I elected to consider ordering the pair of points by the x component.

Sorry about the misdirection. No harm intended. Good luck.
Klatu Barada Nikto
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:24 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC