Mosaic matrix: Difference between revisions
(Added Wren) |
(Realize in F#) |
||
Line 5: | Line 5: | ||
[http://keptarhely.eu/view.php?file=20220218v00xf1rfh.jpeg Mosaic Matrix - image] |
[http://keptarhely.eu/view.php?file=20220218v00xf1rfh.jpeg Mosaic Matrix - image] |
||
<br> |
<br> |
||
=={{header|F_Sharp|F#}}== |
|||
<lang fsharp> |
|||
// Four sides of square. Nigel Galloway: February 18th., 2022 |
|||
let m11 m=Array2D.init m m (fun n g->if (n+g)%2=0 then 1 else 0) |
|||
printfn "%A\n\n%A" (m11 5) (m11 6) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
[[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]] |
|||
[[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|Ring}}== |
=={{header|Ring}}== |
||
<lang ring> |
<lang ring> |
Revision as of 13:07, 18 February 2022
- Task
Show the Mosaic Matrix.
Mosaic Matrix - image
F#
<lang fsharp> // Four sides of square. Nigel Galloway: February 18th., 2022 let m11 m=Array2D.init m m (fun n g->if (n+g)%2=0 then 1 else 0) printfn "%A\n\n%A" (m11 5) (m11 6) </lang>
- Output:
[[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]] [[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]]
Ring
<lang ring>
- Project : Identity Matrix
- Date : 2022/16/02
- Author : Gal Zsolt (~ CalmoSoft ~)
- Email : <calmosoft@gmail.com>
load "stdlib.ring" load "guilib.ring"
size = 8 C_Spacing = 1
C_ButtonBlueStyle = 'border-radius:6px;color:black; background-color: blue' C_ButtonOrangeStyle = 'border-radius:6px;color:black; background-color: orange'
Button = newlist(size,size) LayoutButtonRow = list(size)
app = new qApp {
win = new qWidget() {
setWindowTitle('Identity Matrix') move(500,100) reSize(600,600) winheight = win.height() fontSize = 18 + (winheight / 100)
LayoutButtonMain = new QVBoxLayout()
LayoutButtonMain.setSpacing(C_Spacing) LayoutButtonMain.setContentsmargins(0,0,0,0)
for Row = 1 to size LayoutButtonRow[Row] = new QHBoxLayout() { setSpacing(C_Spacing) setContentsmargins(0,0,0,0) }
for Col = 1 to size
Button[Row][Col] = new QPushButton(win) {
setSizePolicy(1,1)
}
LayoutButtonRow[Row].AddWidget(Button[Row][Col]) next LayoutButtonMain.AddLayout(LayoutButtonRow[Row]) next
LayoutDataRow1 = new QHBoxLayout() { setSpacing(C_Spacing) setContentsMargins(0,0,0,0) } LayoutButtonMain.AddLayout(LayoutDataRow1) setLayout(LayoutButtonMain) show() } pBegin() exec() }
func pBegin()
for Row = 1 to size for Col = 1 to size if (Row%2 = 1 and Col%2 = 1) or (Row%2 = 0 and Col%2 = 0) Button[Row][Col].setStyleSheet(C_ButtonOrangeStyle) Button[Row][Col].settext("1") ok
next
next
</lang> Output image: Mosaic Matrix
Wren
Text based
<lang ecmascript>var mosaicMatrix = Fn.new { |n|
for (i in 0...n) { for (j in 0...n) { System.write(((i + j) % 2 == 0) ? "1 " : ". ") } System.print() }
}
mosaicMatrix.call(9)</lang>
- Output:
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
Graphical
<lang ecmascript>import "dome" for Window import "graphics" for Canvas, Color, Font, ImageData
class Main {
construct new(n) { var size = 60 * n + 10 Window.resize(size, size) Canvas.resize(size, size) Window.title = "Mosaic matrix" // see Go-fonts page Font.load("Go-Regular20", "Go-Regular.ttf", 20) Canvas.font = "Go-Regular20" var beige = Color.new(245, 245, 220) Canvas.cls(Color.lightgray) for (x in 0...n) { for (y in 0...n) { var cx = x*60 + 10 var cy = y*60 + 10 if ((x + y) % 2 == 0) { Canvas.rectfill(cx, cy, 50, 50, Color.brown) Canvas.print("1", cx + 20, cy + 15, beige) } else { Canvas.rectfill(cx, cy, 50, 50, beige) } } } // save to .png file for publication var outputImage = ImageData.create("", size, size) for (x in 0...size) { for (y in 0...size) outputImage.pset(x, y, Canvas.pget(x, y)) } outputImage.saveToFile("mosaic_matrix.png") }
init() {}
update() {}
draw(alpha) {}
}
var Game = Main.new(9)</lang>
- Output: