Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Perly way of handling coordinates?

by loris (Hermit)
on Sep 12, 2007 at 07:18 UTC ( #638497=perlquestion: print w/replies, xml ) Need Help??
loris has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,

I am working with 2D coordinates (actually representing cells in an Excel sheet) and need to add and subtract various offsets to get at the coordinates I need. Currently I am mainly using references to arrays to hold the coordinates, but I was wondering whether there was some clever perly way via which I would be able to do something like the obviously incorrect:

@origin = (2,3); @offset = (4,5); @interesting_cell = @origin + @offset; # = (4) not (6,8) Doh!

Any ideas?



"It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ." (from "Slow Loris" by Alexis Deacon)

Replies are listed 'Best First'.
Re: Perly way of handling coordinates?
by moritz (Cardinal) on Sep 12, 2007 at 07:33 UTC
    One way to do it is to write a class and overload '+' (untested):

    package Coord; use overload '+' => \&add; sub new { my ($class, $x, $y) = @_; return bless { x => $x, y => $y}, $class; } sub add { my ($self, $other) = @_; return Coord->new($self->{x} + $other->{x}, $self->{y} + $other->{y}); }

    Of course this code is missing all kinds of sanity checks. You might even consider to bless an array ref instead of a hash ref, but then it's much harder to extend.

Re: Perly way of handling coordinates?
by moklevat (Priest) on Sep 12, 2007 at 16:14 UTC
    Hi loris,

    PDL treats coordinates in the intuitive way you are seeking.

    #!/usr/bin/perl use strict; use warnings; use PDL; my $origin = pdl (2,3); my $offset = pdl (4,5); my $interesting_cell = $origin + $offset; print $interesting_cell; # [6 8]
Re: Perly way of handling coordinates?
by jettero (Monsignor) on Sep 12, 2007 at 10:48 UTC

    Perhaps Math::Vector? It'd still do the addition correctly without a third dimension.

    Though, it's really just:

    my $a1 = [1,2]; bless $a1, "test"; my $a2 = [2,4]; my $a3 = $a1+$a2; print "@$a3\n"; package test; use strict; use overload '+' => sub { bless [ $_[0][0]+$_[1][0], $_[0][1]+$_[1][1] ], ref $_[0] }; # NOTE: I'm ignoring an important third argument that you'd need to ch +eck # if you wanted to support subtraction 1;

    UPDATE: I didn't notice that moritz wrote basically the exact same thing as the above, sorry. I'm leaving it because it's a slightly different style.


Re: Perly way of handling coordinates?
by jbert (Priest) on Sep 12, 2007 at 07:59 UTC
    Could you use Math::Complex? Then you can add and substract them and pull out the real or imaginary parts for your X and Y.

    They've done the overloading for you already.

Re: Perly way of handling coordinates?
by artist (Parson) on Sep 12, 2007 at 15:40 UTC
    Here is the 'clever' way, you asked for.
    use List::MoreUtils qw(pairwise); @origin = (2,3,4); @offset = (4,5,6); @interesting_cell = pairwise { $a + $b } @origin,@offset; #gets(6,8,10 +)
Re: Perly way of handling coordinates? (*max+)
by tye (Sage) on Sep 12, 2007 at 15:25 UTC

    I'm not sure if it applies with your situations, but I've often found situations where I've got a defined "max width" and I use coordinates that simulate a 2-D matrix using a 1-D array. If the max width is 999 then instead of (2,3) I'd use 2003. And 2003+4005 is 6008.

    - tye        

Re: Perly way of handling coordinates?
by ww (Archbishop) on Sep 12, 2007 at 14:20 UTC
    TIMTOWTDI (easy to follow, but not very Perl-ish); assumes a constant offset, but adapt as required:
    my @origin = (2, 3, 22, 17); my @offset = (4,5); my $n = 0; my $p = 0; for my $origin(@origin) { push(my @interesting_cell, $origin[$n] + $offset[$p]); $n++; $p++; if ( $p > 1 ) { $p = 0 }; for my $interesting(@interesting_cell) { print "$interesting \n"; } }

    Afterthought: OP clearly doesn't want the output formatted this way, but can use the values as desired.

Re: Perly way of handling coordinates?
by spectre9 (Beadle) on Sep 13, 2007 at 01:26 UTC
    For a truly Perly way to solve this issue, perhaps you should implement your references as... cell references. The spreadsheet-perl CPAN module has a pure perl spreadsheet implementation. Storing coordinates as A2 is probably what would be most intuitive. Either extract the parser or maybe the module would work as-is. CPAN: Spreadsheet-Perl-0.08 -- Patrick

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://638497]
Approved by moritz
Front-paged by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2018-04-25 03:49 GMT
Find Nodes?
    Voting Booth?