Munching squares: Difference between revisions

Dialects of BASIC moved to the BASIC section.
m (syntax highlighting fixup automation)
(Dialects of BASIC moved to the BASIC section.)
Line 66:
{{out}} [[Image:AdaXorPattern.png|Ada Output|200px]]
 
=={{header|Applesoft BASICAWK}}==
{{works with|gawk}}
This program generates a PPM image, that you can view/convert using The GIMP or ImageMagick
<syntaxhighlight lang="awk">
BEGIN {
# square size
s = 256
# the PPM image header needs 3 lines:
# P3
# width height
# max colors number (per channel)
print("P3\n", s, s, "\n", s - 1)
# and now we generate pixels as a RGB pair in a relaxed
# form "R G B\n"
for (x = 0; x < s; x++) {
for (y = 0; y < s; y++) {
p = xor(x, y)
print(0, p, p)
}
}
}
</syntaxhighlight>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="gwbasic"> 100 DATA 0,2, 6,10,5, 6, 7,15
110 DATA 0,1, 3,10,5, 3,11,15
Line 92 ⟶ 116:
330 VLIN Y1,Y1 + 1 AT W * S + X
340 NEXT X,Y,W,H</syntaxhighlight>
=={{header|AWK}}==
{{works with|gawk}}
This program generates a PPM image, that you can view/convert using The GIMP or ImageMagick
<syntaxhighlight lang="awk">
BEGIN {
# square size
s = 256
# the PPM image header needs 3 lines:
# P3
# width height
# max colors number (per channel)
print("P3\n", s, s, "\n", s - 1)
# and now we generate pixels as a RGB pair in a relaxed
# form "R G B\n"
for (x = 0; x < s; x++) {
for (y = 0; y < s; y++) {
p = xor(x, y)
print(0, p, p)
}
}
}
</syntaxhighlight>
 
 
=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> size% = 256
Line 139 ⟶ 142:
</syntaxhighlight>
 
==={{header|BefungeCommodore BASIC}}===
{{works with|Commodore BASIC|4.0}}
The TED machines (C-16, Plus/4) are the only Commodore 8-bits with a large- or structured- enough color palette to make this interesting. Here's an extremely low-res version (40x25 character-sized "pixels"):
 
<syntaxhighlight lang="basic">100 FOR I=0 TO 24
110 : Y=INT(I*127/24)
120 : FOR J=0 TO 39
130 : X=INT(J*127/39)
140 : HL = (X OR Y) AND NOT (X AND Y)
150 : H = INT(HL / 8)
160 : L = HL - 8 * H
170 : POKE 2048+I*40+J,L*16+H
180 : POKE 3072+I*40+J,160
190 : NEXT J
210 NEXT I
220 GETKEY K$</syntaxhighlight>
{{Out}}
[https://imgur.com/a/pjl2Pd4 Screenshot.]
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' version 03-11-2016
' compile with: fbc -s gui
 
Dim As ULong x, y, r, w = 256
 
ScreenRes w, w, 32
 
For x = 0 To w -1
For y = 0 To w -1
r =(x Xor y) And 255
PSet(x, y), RGB(r, r , r) ' gray scale
' PSet(x, y), RGB(r, 255 - r, 0) ' red + green
' PSet(x, y), RGB(r, 0, 0) ' red
Next
Next
 
' empty keyboard buffer
While Inkey <> "" : Wend
WindowTitle "Close window or hit any key to end program"
Sleep
End</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">
nomainwin
 
w =512
' allow for title bar and window border
WindowWidth =w +2
WindowHeight =w +34
 
open "XOR Pattern" for graphics_nsb_nf as #w
 
#w "trapclose quit"
 
#w "down"
 
for x =0 to w -1
for y =0 to w -1
b =( x xor y) and 255
print b
#w "color "; 255 -b; " "; b /2; " "; b
#w "set "; x; " "; w -y -1
scan
next y
next x
 
#w "flush"
 
wait
 
sub quit j$
close #w
end
end sub
</syntaxhighlight>
Image available at [[http://www.diga.me.uk/xorRC.gif]]
 
==={{header|Microsoft Small Basic}}===
<syntaxhighlight lang="smallbasic">' Munching squares - smallbasic - 27/07/2018
size=256
GraphicsWindow.Width=size
GraphicsWindow.Height=size
For i=0 To size-1
For j=0 To size-1
BitXor() 'color=i Xor j
GraphicsWindow.SetPixel(i,j,GraphicsWindow.GetColorFromRGB(0,color,color))
EndFor
EndFor
 
Sub BitXor '(i,j)->color
n=i
Int2Bit()
ib=ret
n=j
Int2Bit()
jb=ret
color=0
For k=1 to 8
ki=Text.GetSubText(ib,k,1)
kj=Text.GetSubText(jb,k,1)
If ki="1" Or kj="1" Then
kk="1"
Else
kk="0"
EndIf
If ki="1" And kj="1" Then
kk="0"
EndIf
color=2*color+kk
EndFor
EndSub
 
Sub Int2Bit 'n->ret
x=n
ret=""
For k=1 to 8
t=Math.Floor(x/2)
r=Math.Remainder(x,2)
ret=Text.Append(r,ret)
x=t
EndFor
EndSub </syntaxhighlight>
{{out}}
[https://github.com/Pat-Garrett/RC/blob/master/Munching%20squares%20-%20vbnet.jpg Munching squares - SmallBasic]
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">#palletteSize = 128
Procedure.f XorPattern(x, y) ;compute the gradient value from the pixel values
Protected result = x ! y
ProcedureReturn Mod(result, #palletteSize) / #palletteSize
EndProcedure
 
Procedure drawPattern()
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_Gradient)
CustomGradient(@XorPattern())
;specify a gradient pallette from which only specific indexes will be used
For i = 1 To #palletteSize
GradientColor(1 / i, i * $BACE9B) ; or alternatively use $BEEFDEAD
Next
Box(0, 0, ImageWidth(0), ImageHeight(0))
StopDrawing()
EndProcedure
 
If OpenWindow(0, 0, 0, 128, 128, "XOR Pattern", #PB_Window_SystemMenu)
Writes the image to stdout using the PPM format.
CreateImage(0, WindowWidth(0), WindowHeight(0))
drawPattern()
ImageGadget(0, 0, 0, ImageWidth(0), ImageHeight(0), ImageID(0))
Repeat
event = WaitWindowEvent(20)
Until event = #PB_Event_CloseWindow
EndIf</syntaxhighlight>
[[File:PureBasic_XOR_Pattern.png|Sample display of PureBasic solution|200px]]
 
==={{header|QBasic}}===
<syntaxhighlight lang="qbasic">w = 254
 
SCREEN 13
VIEW (0, 0)-(w / 2, w / 2), , 0
 
FOR x = 0 TO w
FOR y = 0 TO w
COLOR ((x XOR y) AND 255)
PSET (x, y)
NEXT y
NEXT x</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">w = 100
graphic #g, w,w
for x = 0 to w
for y = 0 to w
b = (x xor y) and 255
#g color(255 -b,b /2,b)
#g "set "; x; " "; w -y -1
next y
next x
render #g
#g "flush"</syntaxhighlight>
 
==={{header|TI-83 BASIC}}===
Due to the TI-83's 1 bit black and white display, this program uses the home screen and a gradient of characters. Since the TI-83 does not use a standard encoding, the first Sto→ to Str1 may be subjectively interpreted.
<syntaxhighlight lang="ti-83b">PROGRAM:XORPATT
" •.-,+-°-1+o*:πOX"→Str1
 
ClrHome
 
{0,0,0,0}→L1
{0,0,0,0)→L2
 
For(I,1,8,1)
For(J,1,16,1)
J→A
I→B
 
If A>8
Then
A-8→A
1→L1(1)
Else
0→L1(1)
End
 
If A>4
Then
A-4→A
1→L1(2)
Else
0→L1(2)
End
 
If A>2
Then
A-2→A
1→L1(3)
Else
0→L1(3)
End
 
If A>1
Then
1→L1(4)
Else
0→L1(4)
End
 
0→L2(1)
 
If B>4
Then
B-4→B
1→L2(2)
Else
0→L2(2)
End
 
If B>2
Then
B-2→B
1→L2(3)
Else
0→L2(3)
End
 
If B>1
Then
1→L2(4)
Else
0→L2(4)
End
 
L1≠L2→L3
8L3(1)+4L3(2)+2L3(3)+L3(4)→C
Output(I,J,sub(Str1,C+1,1))
 
End
End
Pause
</syntaxhighlight>
 
==={{header|Visual Basic .NET}}===
{{works with|Visual Basic .NET|2011}}
<syntaxhighlight lang="vbnet">' Munching squares - 27/07/2018
Public Class MunchingSquares
Const xsize = 256
Dim BMP As New Drawing.Bitmap(xsize, xsize)
Dim GFX As Graphics = Graphics.FromImage(BMP)
 
Private Sub MunchingSquares_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
'draw
Dim MyGraph As Graphics = Me.CreateGraphics
Dim nColor As Color
Dim i, j, cp As Integer
xPictureBox.Image = BMP
For i = 0 To xsize - 1
For j = 0 To xsize - 1
cp = i Xor j
nColor = Color.FromArgb(cp, 0, cp)
BMP.SetPixel(i, j, nColor)
Next j
Next i
End Sub 'Paint
 
End Class </syntaxhighlight>
{{out}}
[https://github.com/Pat-Garrett/RC/blob/7e9842513d361a5b4241bc6bb28f9985c2bfe161/Munching%20squares%20-%20vbnet.jpg Munching squares - vbnet]
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">w = 256
open window w, w
For x = 0 To w-1
For y = 0 To w-1
r =and(xor(x, y), 255)
color r, and(r*2, 255), and(r*3, 255)
dot x, y
Next
Next</syntaxhighlight>
 
=={{header|Befunge}}==
Writes the image to stdout using the PPM format.
<syntaxhighlight lang="befunge">55+::"3P",,,28*:*::..\,:.\,:v
>2%*28*:**-2/\1-:v<:8:-1<_@ v
Line 421 ⟶ 724:
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
 
=={{header|Commodore BASIC}}==
{{works with|Commodore BASIC|4.0}}
The TED machines (C-16, Plus/4) are the only Commodore 8-bits with a large- or structured- enough color palette to make this interesting. Here's an extremely low-res version (40x25 character-sized "pixels"):
 
<syntaxhighlight lang="basic">100 FOR I=0 TO 24
110 : Y=INT(I*127/24)
120 : FOR J=0 TO 39
130 : X=INT(J*127/39)
140 : HL = (X OR Y) AND NOT (X AND Y)
150 : H = INT(HL / 8)
160 : L = HL - 8 * H
170 : POKE 2048+I*40+J,L*16+H
180 : POKE 3072+I*40+J,160
190 : NEXT J
210 NEXT I
220 GETKEY K$</syntaxhighlight>
 
{{Out}}
[https://imgur.com/a/pjl2Pd4 Screenshot.]
 
=={{header|D}}==
Line 504 ⟶ 787:
{{out}}
[[File:munching-racket.png]]
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 03-11-2016
' compile with: fbc -s gui
 
Dim As ULong x, y, r, w = 256
 
ScreenRes w, w, 32
 
For x = 0 To w -1
For y = 0 To w -1
r =(x Xor y) And 255
PSet(x, y), RGB(r, r , r) ' gray scale
' PSet(x, y), RGB(r, 255 - r, 0) ' red + green
' PSet(x, y), RGB(r, 0, 0) ' red
Next
Next
 
' empty keyboard buffer
While Inkey <> "" : Wend
WindowTitle "Close window or hit any key to end program"
Sleep
End</syntaxhighlight>
 
=={{header|Fōrmulæ}}==
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
Line 823 ⟶ 1,082:
}
}</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
nomainwin
 
w =512
' allow for title bar and window border
WindowWidth =w +2
WindowHeight =w +34
 
open "XOR Pattern" for graphics_nsb_nf as #w
 
#w "trapclose quit"
 
#w "down"
 
for x =0 to w -1
for y =0 to w -1
b =( x xor y) and 255
print b
#w "color "; 255 -b; " "; b /2; " "; b
#w "set "; x; " "; w -y -1
scan
next y
next x
 
#w "flush"
 
wait
 
sub quit j$
close #w
end
end sub
</syntaxhighlight>
Image available at [[http://www.diga.me.uk/xorRC.gif]]
 
=={{header|Lua}}==
Line 919 ⟶ 1,142:
axis equal;</syntaxhighlight>
{{out}} [[File:matlab_xor.png|MATLAB output|200px]]
 
=={{header|Microsoft Small Basic}}==
<syntaxhighlight lang="smallbasic">' Munching squares - smallbasic - 27/07/2018
size=256
GraphicsWindow.Width=size
GraphicsWindow.Height=size
For i=0 To size-1
For j=0 To size-1
BitXor() 'color=i Xor j
GraphicsWindow.SetPixel(i,j,GraphicsWindow.GetColorFromRGB(0,color,color))
EndFor
EndFor
 
Sub BitXor '(i,j)->color
n=i
Int2Bit()
ib=ret
n=j
Int2Bit()
jb=ret
color=0
For k=1 to 8
ki=Text.GetSubText(ib,k,1)
kj=Text.GetSubText(jb,k,1)
If ki="1" Or kj="1" Then
kk="1"
Else
kk="0"
EndIf
If ki="1" And kj="1" Then
kk="0"
EndIf
color=2*color+kk
EndFor
EndSub
 
Sub Int2Bit 'n->ret
x=n
ret=""
For k=1 to 8
t=Math.Floor(x/2)
r=Math.Remainder(x,2)
ret=Text.Append(r,ret)
x=t
EndFor
EndSub </syntaxhighlight>
{{out}}
[https://github.com/Pat-Garrett/RC/blob/master/Munching%20squares%20-%20vbnet.jpg Munching squares - SmallBasic]
 
=={{header|MiniScript}}==
Line 1,197 ⟶ 1,372:
</syntaxhighlight>
[[File:Prolog_xor_pattern.png|200px]]
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">#palletteSize = 128
Procedure.f XorPattern(x, y) ;compute the gradient value from the pixel values
Protected result = x ! y
ProcedureReturn Mod(result, #palletteSize) / #palletteSize
EndProcedure
 
Procedure drawPattern()
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_Gradient)
CustomGradient(@XorPattern())
;specify a gradient pallette from which only specific indexes will be used
For i = 1 To #palletteSize
GradientColor(1 / i, i * $BACE9B) ; or alternatively use $BEEFDEAD
Next
Box(0, 0, ImageWidth(0), ImageHeight(0))
StopDrawing()
EndProcedure
 
If OpenWindow(0, 0, 0, 128, 128, "XOR Pattern", #PB_Window_SystemMenu)
CreateImage(0, WindowWidth(0), WindowHeight(0))
drawPattern()
ImageGadget(0, 0, 0, ImageWidth(0), ImageHeight(0), ImageID(0))
Repeat
event = WaitWindowEvent(20)
Until event = #PB_Event_CloseWindow
EndIf</syntaxhighlight>
[[File:PureBasic_XOR_Pattern.png|Sample display of PureBasic solution|200px]]
 
=={{header|Python}}==
Line 1,241 ⟶ 1,387:
image.save("xorpic.png", "PNG")</syntaxhighlight>
[[File:PythonXORPic.png|Sample produced by the above code|200px]]
 
=={{header|QBasic}}==
<syntaxhighlight lang="qbasic">w = 254
 
SCREEN 13
VIEW (0, 0)-(w / 2, w / 2), , 0
 
FOR x = 0 TO w
FOR y = 0 TO w
COLOR ((x XOR y) AND 255)
PSET (x, y)
NEXT y
NEXT x</syntaxhighlight>
 
=={{header|Racket}}==
Line 1,427 ⟶ 1,560:
img = Pixmap.xor_pattern(384, 384, RGBColour::RED, RGBColour::YELLOW)
img.save_as_png('xorpattern.png')</syntaxhighlight>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">w = 100
graphic #g, w,w
for x = 0 to w
for y = 0 to w
b = (x xor y) and 255
#g color(255 -b,b /2,b)
#g "set "; x; " "; w -y -1
next y
next x
render #g
#g "flush"</syntaxhighlight>
 
=={{header|Rust}}==
Line 1,530 ⟶ 1,650:
xorImage $img [mkTable 0 255 64 192 255 0]
pack [label .l -image $img]</syntaxhighlight>
 
=={{header|TI-83 BASIC}}==
Due to the TI-83's 1 bit black and white display, this program uses the home screen and a
gradient of characters. Since the TI-83 does not use a standard encoding,
the first Sto→ to Str1 may be subjectively interpreted.
 
 
<syntaxhighlight lang="ti-83b">PROGRAM:XORPATT
" •.-,+-°-1+o*:πOX"→Str1
 
ClrHome
 
{0,0,0,0}→L1
{0,0,0,0)→L2
 
For(I,1,8,1)
For(J,1,16,1)
J→A
I→B
 
If A>8
Then
A-8→A
1→L1(1)
Else
0→L1(1)
End
 
If A>4
Then
A-4→A
1→L1(2)
Else
0→L1(2)
End
 
If A>2
Then
A-2→A
1→L1(3)
Else
0→L1(3)
End
 
If A>1
Then
1→L1(4)
Else
0→L1(4)
End
 
0→L2(1)
 
If B>4
Then
B-4→B
1→L2(2)
Else
0→L2(2)
End
 
If B>2
Then
B-2→B
1→L2(3)
Else
0→L2(3)
End
 
If B>1
Then
1→L2(4)
Else
0→L2(4)
End
 
L1≠L2→L3
8L3(1)+4L3(2)+2L3(3)+L3(4)→C
Output(I,J,sub(Str1,C+1,1))
 
End
End
Pause
</syntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|2011}}
<syntaxhighlight lang="vbnet">' Munching squares - 27/07/2018
Public Class MunchingSquares
Const xsize = 256
Dim BMP As New Drawing.Bitmap(xsize, xsize)
Dim GFX As Graphics = Graphics.FromImage(BMP)
 
Private Sub MunchingSquares_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
'draw
Dim MyGraph As Graphics = Me.CreateGraphics
Dim nColor As Color
Dim i, j, cp As Integer
xPictureBox.Image = BMP
For i = 0 To xsize - 1
For j = 0 To xsize - 1
cp = i Xor j
nColor = Color.FromArgb(cp, 0, cp)
BMP.SetPixel(i, j, nColor)
Next j
Next i
End Sub 'Paint
 
End Class </syntaxhighlight>
{{out}}
[https://github.com/Pat-Garrett/RC/blob/7e9842513d361a5b4241bc6bb28f9985c2bfe161/Munching%20squares%20-%20vbnet.jpg Munching squares - vbnet]
 
=={{header|Wren}}==
Line 1,685 ⟶ 1,694:
]</syntaxhighlight>
{{out}} [[File:MunchXPL0.png]]
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">w = 256
open window w, w
For x = 0 To w-1
For y = 0 To w-1
r =and(xor(x, y), 255)
color r, and(r*2, 255), and(r*3, 255)
dot x, y
Next
Next</syntaxhighlight>
 
=={{header|zkl}}==
512

edits