Mosaic matrix: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
(20 intermediate revisions by 7 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 9:
[http://keptarhely.eu/view.php?file=20220218v00xf1rfh.jpeg Mosaic Matrix - image]
<br>
 
===See also===
* [[Four sides of square]]
* [[Matrix with two diagonals]]
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
V size = 9
Line 35 ⟶ 39:
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">
;;; draw a "mosaic matrix" - one with a 1 in the top-left and then
;;; alternating with another character vertically and horizontally
 
;;; draws a mosaic matrix with height and width = n
PROC drawMosaic( INT n )
CARD i, j, one
FOR i = 1 TO n DO
one = i AND 1
FOR j = 1 TO n DO
Put(' )
IF one = 1 THEN Put('1) ELSE Put('.) FI
one = ( one + 1 ) AND 1
OD
PutE()
OD
RETURN
 
PROC Main()
drawMosaic( 6 )
PutE()
drawMosaic( 7 )
RETURN
</syntaxhighlight>
{{out}}
<pre>
1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1
 
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
</pre>
 
Line 141 ⟶ 188:
</pre>
 
=={{header|ArturoALGOL W}}==
<syntaxhighlight lang="algolw">
begin % draw a "mosaic matrix" - one with a 1 in the top-left and then %
% alternating with another character vertically and horiontally %
% horiontally %
% draws a mosaic matrix with height and width = n using "1" and "." %
procedure drawMosaic ( integer value n ) ;
for i := 1 until n do begin
logical one;
one := odd( i );
for j := 1 until n do begin
writeon( s_w := 0, if one then " 1" else " ." );
one := not one
end for_j ;
write()
end drawMosaic ;
% test the draw mosaic procedure %
drawMosaic( 6 );
write();
drawMosaic( 7 )
end.
</syntaxhighlight>
{{out}}
<pre>
1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1
1 . 1 . 1 .
. 1 . 1 . 1
 
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
. 1 . 1 . 1 .
1 . 1 . 1 . 1
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
Line 254 ⟶ 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 278 ⟶ 378:
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Line 356 ⟶ 455:
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
</pre>
 
=={{header|Delphi}}==
{{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">
{Define a matrix}
 
type TMatrix = array of array of double;
 
procedure DisplayMatrix(Memo: TMemo; Mat: TMatrix);
{Display specified matrix}
var X,Y: integer;
var S: string;
begin
S:='';
for Y:=0 to High(Mat) do
begin
S:=S+'[';
for X:=0 to High(Mat[0]) do
if X=0 then S:=S+Format('%0.0f',[Mat[X,Y]])
else S:=S+Format('%2.0f',[Mat[X,Y]]);
S:=S+']'+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
 
procedure ClearMatrix(var Mat: TMatrix; Value: double);
{Set all elements of the matrix to specified value}
var X,Y: integer;
begin
for Y:=0 to High(Mat) do
for X:=0 to High(Mat[0]) do Mat[X,Y]:=0;
end;
 
procedure SetMosaicMatrix(var Mat: TMatrix);
{Set matrix to mosaic pattern}
var X,Y: integer;
begin
{Set every other cell to one or zero}
for Y:=0 to High(Mat) do
for X:=0 to High(Mat) do Mat[X,Y]:=(X+Y+1) mod 2;
end;
 
 
procedure BuildMosaicMatrix(var Mat: TMatrix; Size: integer);
{Build a matrix with diagonals of the specified size }
var X,Y: integer;
begin
SetLength(Mat,Size,Size);
ClearMatrix(Mat,0);
SetMosaicMatrix(Mat);
end;
 
procedure BuildShowMosaicMatrix(Memo: TMemo; var Mat: TMatrix; Size: integer);
{Build and show a matrix of specific size}
begin
Memo.Lines.Add(Format('Matrix = %2d X %2d',[Size,Size]));
BuildMosaicMatrix(Mat,Size);
DisplayMatrix(Memo,Mat);
end;
 
 
procedure DisplayMosaicMatrices(Memo: TMemo);
{Build and display matrices of various sizes}
var Mat: TMatrix;
var I: integer;
begin
for I:=3 to 10 do BuildShowMosaicMatrix(Memo,Mat,I);
end;
 
</syntaxhighlight>
{{out}}
<pre>
Matrix = 3 X 3
[1 0 1]
[0 1 0]
[1 0 1]
 
Matrix = 4 X 4
[1 0 1 0]
[0 1 0 1]
[1 0 1 0]
[0 1 0 1]
 
Matrix = 5 X 5
[1 0 1 0 1]
[0 1 0 1 0]
[1 0 1 0 1]
[0 1 0 1 0]
[1 0 1 0 1]
 
Matrix = 6 X 6
[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]
 
Matrix = 7 X 7
[1 0 1 0 1 0 1]
[0 1 0 1 0 1 0]
[1 0 1 0 1 0 1]
[0 1 0 1 0 1 0]
[1 0 1 0 1 0 1]
[0 1 0 1 0 1 0]
[1 0 1 0 1 0 1]
 
Matrix = 8 X 8
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
 
Matrix = 9 X 9
[1 0 1 0 1 0 1 0 1]
[0 1 0 1 0 1 0 1 0]
[1 0 1 0 1 0 1 0 1]
[0 1 0 1 0 1 0 1 0]
[1 0 1 0 1 0 1 0 1]
[0 1 0 1 0 1 0 1 0]
[1 0 1 0 1 0 1 0 1]
[0 1 0 1 0 1 0 1 0]
[1 0 1 0 1 0 1 0 1]
 
Matrix = 10 X 10
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1 0 1]
</pre>
 
Line 380 ⟶ 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 505 ⟶ 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 663 ⟶ 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 766 ⟶ 1,107:
1 0 1 0 1 0 1 0 1 0 1</pre>
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* DRAW SOME MOSAIC MATRICES WITH ALTERNATING 1S and .S */
 
/* CP/M SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
 
/* TASK */
 
DRAW$MOSAIC: PROCEDURE( N );
DECLARE N BYTE;
DECLARE ( I, J, ONE ) BYTE;
DO I = 1 TO N;
ONE = I; /* EVEN NUMBERS ARE FALSE, ODD ARE TRUE IN PL/M */
DO J = 1 TO N;
CALL PR$CHAR( ' ' );
IF ONE THEN CALL PR$CHAR( '1' ); ELSE CALL PR$CHAR( '0' );
ONE = NOT ONE;
END;
CALL PR$NL;
END;
END DRAW$MOSAIC ;
 
CALL DRAW$MOSAIC( 6 );
CALL PR$NL;
CALL DRAW$MOSAIC( 7 );
 
EOF
</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
 
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
</pre>
 
=={{header|Processing}}==
Line 892 ⟶ 1,282:
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ 1 & not ] is even ( n --> b )
 
Line 1,057 ⟶ 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,086 ⟶ 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,112 ⟶ 1,532:
{{libheader|DOME}}
{{libheader|Go-fonts}}
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color, Font, ImageData
 
9,476

edits