Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/spiral-matrix.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / spiral-matrix.wiki`
119 lines ยท 2306 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 global grid ulong 64
10 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11 fn emit a view uchar * to void
12 o = ulong 0
13 while lt o len a bound 4096
14 k = write 1 a o sub len a o
15 if le k 0
16 exit 1
17 end
18 o = add o ulong k
19 end
20 end
21 note emitu writes n in decimal; 20 digits hold every ulong
22 fn emitu n ulong to void
23 d = new uchar 20 0
24 i = ulong 20
25 v = n
26 while true bound 20
27 i = sub i 1
28 put d i uchar add rem v 10 48
29 v = div v 10
30 if eq v 0
31 break
32 end
33 end
34 emit part d i sub 20 i
35 end
36 note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut)
37 fn emitw n ulong w ulong to void
38 d = new uchar 20 32
39 i = ulong 20
40 v = n
41 while true bound 20
42 i = sub i 1
43 put d i uchar add rem v 10 48
44 v = div v 10
45 if eq v 0
46 break
47 end
48 end
49 if gt w sub 20 i
50 i = sub 20 w
51 end
52 emit part d i sub 20 i
53 end
54 note the walk starts at the top left going right and turns clockwise whenever the next cell is off the
55 note grid or already numbered; grid holds number+1 so that 0 means empty
56 fn spiral n ulong to void
57 for i ulong mul n n
58 put grid i 0
59 end
60 r = long 0
61 c = long 0
62 dr = long 0
63 dc = long 1
64 for k ulong mul n n
65 put grid add mul ulong r n ulong c add k 1
66 nr = add r dr
67 nc = add c dc
68 if blocked nr nc n
69 t = dr
70 dr = dc
71 dc = neg t
72 nr = add r dr
73 nc = add c dc
74 end
75 r = nr
76 c = nc
77 end
78 end
79 note blocked says whether (r, c) is outside the n x n grid or already numbered
80 fn blocked r long c long n ulong to bool
81 if or or lt r 0 ge r long n or lt c 0 ge c long n
82 ret true
83 end
84 ret ne get grid add mul ulong r n ulong c 0
85 end
86 fn show n ulong to void
87 for i ulong n
88 for j ulong n
89 emitw sub get grid add mul i n j 1 3
90 end
91 emit nl
92 end
93 end
94 fn main to int
95 spiral 5
96 show 5
97 emit nl
98 spiral 8
99 show 8
100 ret 0
101 end
102
103 {{out}}
104
105 0 1 2 3 4
106 15 16 17 18 5
107 14 23 24 19 6
108 13 22 21 20 7
109 12 11 10 9 8
110
111 0 1 2 3 4 5 6 7
112 27 28 29 30 31 32 33 8
113 26 47 48 49 50 51 34 9
114 25 46 59 60 61 52 35 10
115 24 45 58 63 62 53 36 11
116 23 44 57 56 55 54 37 12
117 22 43 42 41 40 39 38 13
118 21 20 19 18 17 16 15 14
119