with the given coordinates it produces the following output#! /usr/local/bin/perl @coord = ( 6, 2, -4 ); $dim = scalar ( @coord ); @trans = map { $_ - 1 } @coord; for ( $i = 0; $i < 3**$dim; $i++ ) { @vector = vector ( $i ); @add = add_vector ( \@trans, \@vector ); print "[@add]\n" unless center ( @vector ); } sub vector { my $i = shift @_; my @vector; for ( my $j=0; $j < $dim; $j++ ) { push ( @vector, $i % 3 ); $i = int $i/3; } return @vector; } sub add_vector { my $ar_ref1 = shift @_; my $ar_ref2 = shift @_; my @vector; for ( my $j=0; $j < $dim; $j++ ) { push ( @vector, $$ar_ref1[$j] + $$ar_ref2[$j] ); } return @vector; } sub center { my @vector = @_; my $bool = 1; my $i = 0; while ( $bool and $i < $dim ) { $bool = ( $vector[$i++] == 1 ); } return $bool; }
I am not going to explain my code and the idea's I used to create it. (It would be rather lengthy). The variables and subroutines I use suggest the underlying mechanisme. But if my code is as obscure to you as the others solutions are to my, please comment on the reply and I will try to explain it![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] [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]
In reply to Re: getting my neighbours in an N-dimensional space
by Anonymous Monk
in thread getting my neighbours in an N-dimensional space
by dash2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |