I wanted to see what an edge walker looked like
in the "strings/regex" domain...
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1204060
use strict;
use warnings;
my @points = points();
my ($minx, $miny, $maxx, $maxy) = (
$points[0][0], $points[0][1], $points[0][0], $points[0][1]);
for (@points)
{
my ($x, $y) = @$_;
$minx > $x and $minx = $x;
$miny > $y and $miny = $y;
$maxx < $x and $maxx = $x;
$maxy < $y and $maxy = $y;
}
my $height = $maxy - $miny + 3; # some useful stuff
my $width = $maxx - $minx + 3;
my $gw = $width + 1; # grid width (distance to down one row)
my $gwm3 = $gw - 3;
my @letters = ('A' .. 'Z', 'a' .. 'z'); # label points by letter seque
+nce
my @steps = (1, $gw+1, $gw, $gw-1, -1, -$gw-1, -$gw, -$gw+1); # by dir
+ection
my $grid = ( ' ' x $width . "\n" ) x $height;
my @answerpoints; # coordinates around edge
for (@points) # put points on grid (multiline string :)
{
my ($x, $y) = @$_;
substr $grid, (($y - $miny + 1)) * $gw + ($x - $minx + 1), 1, '#';
}
my $direction = 0; # start facing east (increases clockwise)
my $start = my $at = $grid =~ /(?<= )#/ ? $-[0] : die "empty"; # at to
+pleft
do # clockwise walk around edge
{
push @answerpoints, [ $at % $gw - 1 + $minx, int $at / $gw - 1 + $mi
+ny ];
pos($grid) = $at - $gw - 1; # northwest corner neighbor
$grid =~ /\G(.)(.)(.).{$gwm3}(.).(.).{$gwm3}(.)(.)(.)/s; # all neigh
+bors
("$1$2$3$5$8$7$6$4" x 2) =~ /^.{$direction} *(#)/ and # rotate & sca
+n
$at += $steps[ $direction = ( $-[1] - 3 ) % 8 ]; # step in new dir
+ection
}
until( $at == $start );
my $i = 0;
for (@answerpoints) # add letters for display if wanted
{
my ($x, $y) = @$_;
pos($grid) = $x - $minx + 1 + ($y - $miny + 1) * $gw;
$grid =~ s/\G#/$letters[$i++ % @letters]/; # label point if not
}
print $grid;
#use Data::Dump 'pp'; pp \@answerpoints; # the real answer :)
sub points
{
[527,83],[527,84],[526,84],[525,84],[524,84],[523,84],[522,84],
... some points deleted to shorten listing ...
[534,113],[533,113],[532,113],[531,113],[530,113],
}
Outputs:
A
MNOPQRSB
KL#######C
J########D
I########E
k H##########FG O
j#l G#############HIJKLMNP
i#m F#####################Q
h#no E#####################R
g##pq D#####################S
f###rs C#####################T
e#####tuvw B#####################U
d#########xyzA######################V
c###################################W
baZ#################################X
Y################################Y
X################################Z
W################################a
V###############################b
U###############################c
T###############################d
S##############################e
R##############################f
Q##############################g
P############################h
ONMLK#######################i
JIHGFED################j
CBAz############k
yxwvut######l
s####m
rqpon
Sure saves a lot of that ugly subscripting :)
UPDATE: New and Improved!! Now with Tk!! see Re^2: Polygon Creation -- Request for Algorithm Suggestions
-
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.