Spiral matrix: Difference between revisions

m
No edit summary
 
(9 intermediate revisions by 4 users not shown)
Line 1,607:
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls, Windows}}
This code actually creates a thematrix requiredin memory and stores values in the matrix, instead of just simulating one by drawing the pattern. It can also create matriciesmatrices of any size and the matriciesmatrices don't have to be square. It works by creating a rectangle of the same size as the matrix,. thenIt enteringenters the values alongin the linesmatrix along circumference of the rectanglematrix. It then uses the Windows library routine "InflateRect" to decrease the size of the rectangle until the whole matrix is filled with spiraling values. Since a rectangle can be any size and doesn't have to be square, it works with any size matrix, including non-square matrices.
 
<syntaxhighlight lang="Delphi">
Line 1,673:
R:=Rect(0,0,SizeX-1,SizeY-1);
Inx:=0;
{draw and deflate rectanlerectangle until spiral is done}
while (R.Left<=R.Right) and (R.Top<=R.Bottom) do
begin
Line 1,732:
 
</pre>
 
 
=={{header|E}}==
Line 1,770 ⟶ 1,769:
13 22 21 20 7
12 11 10 9 8</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc mkspiral n . t[] .
subr side
for i to l
ind += d
t[ind] = cnt
cnt += 1
.
.
len t[] n * n
l = n
while cnt < len t[]
d = 1
side
l -= 1
d = n
side
d = -1
side
l -= 1
d = -n
side
.
.
n = 5
mkspiral n t[]
numfmt 0 3
for i to n * n
write t[i]
if i mod n = 0
print ""
.
.
</syntaxhighlight>
{{out}}
<pre>
0 1 2 3 4
15 16 17 18 5
14 23 24 19 6
13 22 21 20 7
12 11 10 9 8
</pre>
 
=={{header|Elixir}}==
Line 4,875 ⟶ 4,918:
 
[http://keptarhely.eu/view.php?file=20220218v00xuh6r2.jpeg Spiral matrix]
 
=={{header|RPL}}==
{{works with|RPL|HP48-R}}
« { 0 1 } → n step
« { 1 1 } n DUP 2 →LIST -1 CON <span style="color:grey">@ empty cell = -1</span>
1 n SQ '''FOR''' j
OVER j PUT
DUP2 SWAP step ADD
'''IF IFERR''' GET '''THEN''' DROP2 1 '''ELSE''' -1 ≠ '''END''' <span style="color:grey">@ if next step is out of border or an already written cell</span>
'''THEN''' step REVLIST { 1 -1 } * 'step' STO '''END''' <span style="color:grey">@ then turn right</span>
SWAP step ADD SWAP
'''NEXT'''
» » '<span style="color:blue">SPIRAL</span>' STO
 
5 <span style="color:blue">SPIRAL</span>
{{out}}
<pre>
1: [[1 2 3 4 5]
[16 17 18 19 6]
[15 24 25 20 7]
[14 23 22 21 8]
[13 12 11 10 9]]
</pre>
 
=={{header|Ruby}}==
Line 5,775 ⟶ 5,841:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv, Fmt
 
var n = 5
1,150

edits