Marching squares: Difference between revisions

m
syntax highlighting fixup automation
(Added Perl)
m (syntax highlighting fixup automation)
Line 11:
This is a partial implementation, see the talk page for some discussion of the untouched issues.
 
<langsyntaxhighlight Jlang="j">step1=: {{2 2 #.@(0 1 3 2&{)@,;._3 ,&0^:2@|:@|.^:4 y}}
step2=: {{(($y)#:i.$y) {{<x step2a y{::LUT}}"1 0 y}}
step2a=: {{ if. #y do. x+"1 y else. y end. }}
Line 49:
end.
r,<~.c
}}</langsyntaxhighlight>
 
Task example:
 
<langsyntaxhighlight Jlang="j"> img=: 4~:i.3 2
img
1 1
Line 69:
│2 4│
│1 3│
└───┘</langsyntaxhighlight>
 
Here, <code>img</code> is a bitmap. We pad the bitmap by surrounding it with zeros during processing. The box at the end contains a contour corresponding to the bitmap. Here, the first column represents row number (row 0 at the top) and the second column represents column number (column 0 at the left). Each contour represents a closed loop (so the final coordinate pair would connect with the first coordinate pair).
Line 75:
While the above implementation is incomplete, it seems to adequately handle an oval of cassini with focal points at X=1, -1 and Y=0:
 
<langsyntaxhighlight Jlang="j">a=: 1
X=: |:Y=:201#"0]0.02*i:100
Z0=: (*:(*:X)+(*:Y)) + (_2*(*:a)*X -&*: Y) + *:a
Z=: (Z0>:0.8)*Z0<:1.2
C=: unwind step2 step1 Z</langsyntaxhighlight>
 
Here, Z is a bitmap, and C is a list of three contours (one with 336 distinct coordinate pairs, and two with 134 distinct coordinate pairs) which surround that bitmap. These can be inspected (after <code>require'viewmat'</code>) with <code>viewmat Z</code> and <code>viewmat 1 (<"1;C)} 200 200$0</code>, and look plausible.
Line 88:
Uses the marching squares algorithm: see github.com/JuliaGeometry/Contour.jl/blob/master/src/Contour.jl
See the discussion page for the Oval of Cassini example
<langsyntaxhighlight lang="ruby">using Contour
import GLMakie as GM # GLMakie also defines Contour so import, not using
 
Line 124:
GM.lines!(ovalxs3, ovalys3, color = :lightgreen, linewidth = 4)
GM.display(oplot)
</langsyntaxhighlight>{{out}}
<pre>
points = [(3, 4), (4, 3), (4, 2), (4, 1), (3, 0), (2, 1), (2, 1), (1, 2), (1, 3), (2, 4), (3, 4)]
Line 131:
=={{header|Lua}}==
Based on the Phix and Wren solutions.
<langsyntaxhighlight Lualang="lua">-- positive directions: right, down, clockwise
local Directions = { -- clockwise from North
N = {x= 0, y=-1},
Line 227:
 
msMain (1, example)
</langsyntaxhighlight>{{out}}
<pre>
root: x=1 y=2
Line 237:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use v5.36;
no warnings 'experimental::for_list';
use List::Util 'any';
Line 278:
[0, 0, 0, 0, 0]);
 
printf "X: %d, Y: %d, Path: %s\n", identify_perimeter(@M);</langsyntaxhighlight>
{{out}}
<pre>X: 2, Y: -2, Path: SSESENNNWW</pre>
Line 284:
=={{header|Phix}}==
Based on the same code as the Wren example.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">E</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">W</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">S</span>
Line 338:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"X: %d, Y: %d, Path: %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">identifyPerimeter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">example</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 345:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from numpy import array, round
from skimage import measure
 
Line 360:
contours = round(measure.find_contours(example, 0.1))[0]
print('[', ', '.join([str((p[1], 5 - p[0])) for p in contours]), ']')
</langsyntaxhighlight>{{out}}
<pre>
[ (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) ]
Line 367:
=={{header|Raku}}==
{{trans|Phix}}
<syntaxhighlight lang="raku" perl6line># 20220708 Raku programming solution
 
enum <E N W S>;
Line 409:
}
return -1, -1, "Not found!"
}</langsyntaxhighlight>
Output is the same as the Phix entry.
 
Line 415:
{{libheader|Wren-seq}}
This is a translation of the [http://www.tomgibara.com/computer-vision/marching-squares public domain Java code], written by Tom Gibara, which is linked to from the Wikipedia article. It also uses his example to test the code.
<langsyntaxhighlight lang="ecmascript">import "./seq" for Lst, FrozenList
 
/* A direction in the plane. */
Line 632:
var ms = MarchingSquares.new(5, 6, example)
var path = ms.identifyPerimeter()
System.print(path)</langsyntaxhighlight>
 
{{out}}
10,327

edits