Mosaic matrix: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
(15 intermediate revisions by 6 users not shown)
Line 1:
{{Draft task|Matrices}}
;Task:
Draw a 'mosaic' matrix which, for the purposes of this task, is a square matrix which has 1's in alternate cells (both horizontally and vertically) starting with a 1 in the top-left hand cell.
Line 17:
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
V size = 9
Line 45 ⟶ 44:
<syntaxhighlight lang="action!">
;;; draw a "mosaic matrix" - one with a 1 in the top-left and then
;;; alternating with another character vertically and horiontallyhorizontally
 
;;; draws a mosaic matrix with height and width = n
Line 230 ⟶ 229:
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
Line 342 ⟶ 340:
1 0 1 0 1 0 1
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Mosaic ← =⌜˜2|↕
 
Mosaic 6</syntaxhighlight>
{{out}}
<pre>┌─
╵ 1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
┘</pre>
 
=={{header|C}}==
Line 366 ⟶ 378:
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Line 449 ⟶ 460:
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Another example of modular code and saving time by reusing subroutines and structures. Most of the code from is take from this previously worked example:
 
[[https://rosettacode.org/wiki/Matrix_with_two_diagonals#Delphi| Matrix with two Diagonals]]
 
<syntaxhighlight lang="Delphi">
Line 591 ⟶ 602:
[0 1 0 1 0 1 0 1 0 1]
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 615 ⟶ 625:
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99}}
<syntaxhighlight lang="factor">USING: accessors colors kernel math math.matrices ui
ui.gadgets.grid-lines ui.gadgets.grids ui.gadgets.labels
ui.pens.solid ;
IN: mosaic
 
: <square> ( m n -- gadget )
+ 2 mod 0 = [
" 1 " <label> COLOR: AntiqueWhite2 <solid> >>interior
[ 50 >>size ] change-font
] [ "" <label> ] if ;
 
: <checkerboard> ( n -- gadget )
dup [ <square> ] <matrix-by-indices> <grid>
COLOR: gray <grid-lines> >>boundary ;
 
MAIN: [ 8 <checkerboard> "Mosaic" open-window ]</syntaxhighlight>
{{out}}
[[File:Factor-mosaic.png|thumb|center]]
 
=={{header|FreeBASIC}}==
Line 740 ⟶ 770:
 
=={{header|J}}==
 
Implementation:
<syntaxhighlight lang="j">mosq=:{{0 [: =2|+/~ 2 | i.y}}</syntaxhighlight>
 
Examples:
 
<syntaxhighlight lang="j"> mosq 4
1 0 1 0
Line 898 ⟶ 925:
1 0 1 0 1
</syntaxhighlight>
 
=={{header|K}}==
K6
<syntaxhighlight lang="k">mosaic: {x=/:x}2!!:
 
mosaic 6</syntaxhighlight>
{{out}}
<pre>(1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1)</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a mosaic matrix */
mosaic(n):=genmatrix(lambda([i,j],charfun(evenp(abs(i-j)))),n,n)$
 
/* Alternative fumction */
altern_mosaic(n):=genmatrix(lambda([i,j],if evenp(abs(i-j)) then 1 else 0),n,n)$
 
/* Examples */
mosaic(3);
mosaic(4);
</syntaxhighlight>
{{out}}
<pre>
matrix(
[1, 0, 1],
[0, 1, 0],
[1, 0, 1]
)
matrix(
[1, 0, 1, 0],
[0, 1, 0, 1],
[1, 0, 1, 0],
[0, 1, 0, 1]
)
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">proc drawMosaicMatrix(side: Positive) =
var start = 1
for i in 0..<side:
var c = start
for j in 0..<side:
stdout.write c
c = 1 - c
echo()
start = 1 - start
 
echo "6x6 matrix:\n"
drawMosaicMatrix(6)
 
echo "\n7x7 matrix:\n"
drawMosaicMatrix(7)
</syntaxhighlight>
 
{{out}}
<pre>6x6 matrix:
 
101010
010101
101010
010101
101010
010101
 
7x7 matrix:
 
1010101
0101010
1010101
0101010
1010101
0101010
1010101
</pre>
 
=={{header|Pascal}}==
Line 1,000 ⟶ 1,106:
0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1</pre>
 
 
=={{header|PL/M}}==
Line 1,177 ⟶ 1,282:
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ 1 & not ] is even ( n --> b )
 
Line 1,342 ⟶ 1,446:
<br>
[http://keptarhely.eu/view.php?file=20220218v00xf1rfh.jpeg Mosaic Matrix]
 
=={{header|RPL}}==
'''Filling a matrix'''
≪ → n
≪ n DUP 2 →LIST 0 CON
1 n '''FOR''' r
1 n '''FOR''' c
r c 2 →LIST r c + 2 MOD NOT PUT
'''NEXT NEXT'''
≫ ≫ '<span style="color:blue">'''MOZTX'''</span>' STO
 
4 <span style="color:blue">'''MOZTX'''</span>
{{out}}
<pre>
1: [[ 1 0 1 0 ]
[ 0 1 0 1 ]
[ 1 0 1 0 ]
[ 0 1 0 1 ]]
</pre>
'''Filling the stack'''
« → n
« 1 n '''FOR''' j
j 2 MOD
2 n '''START''' DUP NOT '''NEXT'''
'''NEXT'''
n DUP 2 →LIST →ARRY
» » '<span style="color:blue">'''MOZTX'''</span>' STO
'''Direct approach'''
 
Latest RPL versions have a specific instruction to generate a matrix ruled by a formula.
≪ DUP 'NOT(I+J) MOD 2' LCXM →NUM ≫ '<span style="color:blue">'''MOZTX'''</span>' STO
 
=={{header|Sidef}}==
Line 1,371 ⟶ 1,506:
=={{header|Wren}}==
===Text based===
<syntaxhighlight lang="ecmascriptwren">var mosaicMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
Line 1,397 ⟶ 1,532:
{{libheader|DOME}}
{{libheader|Go-fonts}}
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color, Font, ImageData
 
9,483

edits