in reply to getting my neighbours in an N-dimensional space
This version creates a closure that counts in base 3 using "digits" -1, 0, and 1, returning the next neighbor each time it is called. It returns "us" as one of our neighbors.
produces:#!/usr/bin/perl -w use strict; use mapcar; sub neighbors { my @coords= @_; my @offset= (-1) x @coords; return sub { my $i= 0; return if ! @coords; my @return= mapcar { $_[0] + $_[1] } \@coords, \@offset; while( 1 < ++$offset[$i] ) { $offset[$i]= -1; if( $#offset < ++$i ) { @offset= @coords= (); last; } } return @return; }; } my $next= neighbors( 6, 2, -4 ); my @coords; while( @coords= $next->() ) { print "( @coords )\n"; }
( 5 1 -5 ) ( 6 1 -5 ) ( 7 1 -5 ) ( 5 2 -5 ) ( 6 2 -5 ) ( 7 2 -5 ) ( 5 3 -5 ) ( 6 3 -5 ) ( 7 3 -5 ) ( 5 1 -4 ) ( 6 1 -4 ) ( 7 1 -4 ) ( 5 2 -4 ) ( 6 2 -4 ) ( 7 2 -4 ) ( 5 3 -4 ) ( 6 3 -4 ) ( 7 3 -4 ) ( 5 1 -3 ) ( 6 1 -3 ) ( 7 1 -3 ) ( 5 2 -3 ) ( 6 2 -3 ) ( 7 2 -3 ) ( 5 3 -3 ) ( 6 3 -3 ) ( 7 3 -3 )
This was based on (tye)Re: Finding all Combinations and requires mapcar.
- tye (but my friends call me "Tye")
|
---|
Replies are listed 'Best First'. | |
---|---|
Re:x2 getting my neighbours in an N-dimensional space
by grinder (Bishop) on Jan 21, 2002 at 20:58 UTC | |
by tye (Sage) on Jan 21, 2002 at 21:04 UTC |
In Section
Seekers of Perl Wisdom