Marching squares: Difference between revisions
Content added Content deleted
(→{{header|J}}: yes, an oval, but...) |
(added Raku programming solution) |
||
Line 317: | Line 317: | ||
[ (3.0, 0.0), (2.0, 1.0), (2.0, 1.0), (1.0, 2.0), (1.0, 3.0), (2.0, 4.0), (3.0, 4.0), (4.0, 3.0), (4.0, 2.0), (4.0, 1.0), (3.0, 0.0) ] |
[ (3.0, 0.0), (2.0, 1.0), (2.0, 1.0), (1.0, 2.0), (1.0, 3.0), (2.0, 4.0), (3.0, 4.0), (4.0, 3.0), (4.0, 2.0), (4.0, 1.0), (3.0, 0.0) ] |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
{{trans|Phix}} |
|||
<lang perl6># 20220708 Raku programming solution |
|||
enum <E N W S>; |
|||
my (@dx,@dy) := (1,0,-1,0), (0,-1,0,1); |
|||
my \example = ((0, 0, 0, 0, 0), |
|||
(0, 0, 0, 0, 0), |
|||
(0, 0, 1, 1, 0), |
|||
(0, 0, 1, 1, 0), |
|||
(0, 0, 0, 1, 0), |
|||
(0, 0, 0, 0, 0)); |
|||
printf("X: %d, Y: %d, Path: %s\n", identifyPerimeter(example)); |
|||
sub identifyPerimeter(\data) { |
|||
my (\height,\width) = { .elems, .first.elems }(data); |
|||
for ^width X ^height -> (\x,\y) { |
|||
unless data[y;x] == 0 { |
|||
my \directions = $ = ''; |
|||
my ($cx,$cy,$direction,$previous) = x, y; |
|||
loop { |
|||
my $mask = 0; |
|||
for (0,0,1),(1,0,2),(0,1,4),(1,1,8) -> (\dx,\dy,\b) { |
|||
my ($mx,$my) = $cx+dx,$cy+dy; |
|||
$mask += b if so all ( $mx>1, $my>1, data[$my-1;$mx-1] != 0) |
|||
} |
|||
$direction = do given $mask { |
|||
when * ∈ ( 1, 5, 13 ) { N } |
|||
when * ∈ ( 2, 3, 7 ) { E } |
|||
when * ∈ ( 4, 12, 14 ) { W } |
|||
when * ∈ ( 8, 10, 11 ) { S } |
|||
when * ∈ ( 6 ) { $previous == N ?? W !! E } |
|||
when * ∈ ( 9 ) { $previous == E ?? N !! S } |
|||
} |
|||
given $direction { directions ~= $_ ; |
|||
$previous = $_ ; |
|||
($cx,$cy) <<+=<< |(@dx[.value], @dy[.value]) } |
|||
last if $cx==x and $cy==y ; |
|||
} |
|||
return x, -y, directions |
|||
} |
|||
} |
|||
return -1, -1, "Not found!" |
|||
}</lang> |
|||
Output is the same as the Phix entry. |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |