Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
hello,

I'm looking for a fast sorting (or "reordering") algorithm. the situation is as follows: I have a bunch of segments, in the form of 4-elements arrays ( START_X, START_Y, END_X, END_Y ). these segments form a polygon, but unfortunately I don't have them in the right order.

to let you understand the problem, consider this polygon:

| 5 + F-----------E | | | 4 + | | | | | 3 + | C--------D | | | 2 + | | | | | 1 + A--B | +--+--+--+--+--+-- 1 2 3 4 5
I could have the segments, for example, in this order (letters are not there, they're just for your reference):
DE = [ 5, 3, 5, 5 ] AB = [ 1, 1, 2, 1 ] BC = [ 2, 1, 2, 3 ] EF = [ 5, 5, 1, 5 ] FA = [ 1, 5, 1, 1 ] CD = [ 2, 3, 5, 3 ]
and I need them in this order:
AB = [ 1, 1, 2, 1 ] BC = [ 2, 1, 2, 3 ] CD = [ 2, 3, 5, 3 ] DE = [ 5, 3, 5, 5 ] EF = [ 5, 5, 1, 5 ] FA = [ 1, 5, 1, 1 ]
in fact, what I need is just the order of the points to build the polygon, which is:
A = [ 1, 1 ] B = [ 2, 1 ] C = [ 2, 3 ] D = [ 5, 3 ] E = [ 5, 5 ] F = [ 1, 5 ]
that said, the algorithm I came up with is:
# segments are in @seg my($start_x, $start_y) = ($seg[0]->[0], $seg[0]->[1]); my($end_x, $end_y) = ($seg[0]->[2], $seg[0]->[3]); push(@poly, [$start_x, $start_y]); push(@poly, [$end_x, $end_y]); shift(@seg); while(@seg) { for(my $s = 0; $s <= $#seg; $s++) { if( $seg[$s]->[0] == $end_x and $seg[$s]->[1] == $end_y ) { ($end_x, $end_y) = ($seg[$s]->[2], $seg[$s]->[3]); push(@poly, [$end_x, $end_y]); splice(@seg, $s, 1); } } } # polygon points are in @poly
this actually works, but since I have several hundreds of thousands of points to reorder, I really need something more efficient. I'm pretty sure I have to use a hash somehow, but my brain, or at least part of it, seems to be gone in vacation :-)

well, if your brain is still around, can you help me?

cheers,
Aldo

King of Laziness, Wizard of Impatience, Lord of Hubris


In reply to reordering segments to form a polygon by dada

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-11-26 12:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?