Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Sorting geometric coordinates based on priority

by johngg (Abbot)
on Feb 02, 2017 at 23:15 UTC ( #1180896=note: print w/replies, xml ) Need Help??


in reply to Sorting geometric coordinates based on priority

A solution using a Guttman Rosler Transform which might be more efficient if there are a large number of rectangles.

use strict; use warnings; use feature qw{ say }; my @x = ( 3, 2, 4, 0, 3, 0, 4, 0, 3, 2, 0, 4 ); my @y = ( 0, 0, 0, 1, 1, 0, 3, 3, 3, 3, 2, 2 ); my @rects = map { [ split m{:}, substr $_, 8 ] } sort map { my $packed = ~ ( pack q{N}, $x[ $_ ] ) . pack q{Na*}, $y[ $_ ], join( q{:}, $x[ $_ ], $y[ $_ ] ) } 0 .. $#x; my $n = 0; say qq{@{[ sprintf q{%2d}, $n ++ ] }: @$_} for @rects;

The output.

0: 4 0 1: 4 2 2: 4 3 3: 3 0 4: 3 1 5: 3 3 6: 2 0 7: 2 3 8: 0 0 9: 0 1 10: 0 2 11: 0 3

I hope this is of interest.

Update: Simplified decoration/de-decoration removing need for join and split.

use strict; use warnings; use feature qw{ say }; my @x = ( 3, 2, 4, 0, 3, 0, 4, 0, 3, 2, 0, 4 ); my @y = ( 0, 0, 0, 1, 1, 0, 3, 3, 3, 3, 2, 2 ); my @rects = map { [ unpack( q{N}, ~ $_ ), unpack( q{x4N}, $_ ) ] } sort map { my $packed = ~ ( pack q{N}, $x[ $_ ] ) . pack q{N}, $y[ $_ ] } 0 .. $#x; my $n = 0; say qq{@{[ sprintf q{%2d}, $n ++ ] }: @$_} for @rects;

Cheers,

JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1180896]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-05-26 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?