Munching squares: Difference between revisions

Content added Content deleted
(Applesoft BASIC)
m (syntax highlighting fixup automation)
Line 6: Line 6:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>PROC PutBigPixel(BYTE x,y,c)
<syntaxhighlight lang="action!">PROC PutBigPixel(BYTE x,y,c)
BYTE i
BYTE i


Line 36: Line 36:
DO UNTIL CH#$FF OD
DO UNTIL CH#$FF OD
CH=$FF
CH=$FF
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Munching_squares.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Munching_squares.png Screenshot from Atari 8-bit computer]
Line 43: Line 43:
{{libheader|GtkAda}}
{{libheader|GtkAda}}
Uses the Cairo component of GtkAda to create and save as png
Uses the Cairo component of GtkAda to create and save as png
<lang Ada>with Cairo; use Cairo;
<syntaxhighlight lang="ada">with Cairo; use Cairo;
with Cairo.Png; use Cairo.Png;
with Cairo.Png; use Cairo.Png;
with Cairo.Image_Surface; use Cairo.Image_Surface;
with Cairo.Image_Surface; use Cairo.Image_Surface;
Line 63: Line 63:
Status := Write_To_Png (Surface, "AdaXorPattern.png");
Status := Write_To_Png (Surface, "AdaXorPattern.png");
pragma Assert (Status = Cairo_Status_Success);
pragma Assert (Status = Cairo_Status_Success);
end XorPattern;</lang>
end XorPattern;</syntaxhighlight>
{{out}} [[Image:AdaXorPattern.png|Ada Output|200px]]
{{out}} [[Image:AdaXorPattern.png|Ada Output|200px]]


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic> 100 DATA 0,2, 6,10,5, 6, 7,15
<syntaxhighlight lang="gwbasic"> 100 DATA 0,2, 6,10,5, 6, 7,15
110 DATA 0,1, 3,10,5, 3,11,15
110 DATA 0,1, 3,10,5, 3,11,15
120 DATA 0,8, 9,10,5, 9,13,15
120 DATA 0,8, 9,10,5, 9,13,15
Line 91: Line 91:
320 COLOR= C( PEEK (235))
320 COLOR= C( PEEK (235))
330 VLIN Y1,Y1 + 1 AT W * S + X
330 VLIN Y1,Y1 + 1 AT W * S + X
340 NEXT X,Y,W,H</lang>
340 NEXT X,Y,W,H</syntaxhighlight>
=={{header|AWK}}==
=={{header|AWK}}==
{{works with|gawk}}
{{works with|gawk}}
This program generates a PPM image, that you can view/convert using The GIMP or ImageMagick
This program generates a PPM image, that you can view/convert using The GIMP or ImageMagick
<lang awk>
<syntaxhighlight lang="awk">
BEGIN {
BEGIN {
# square size
# square size
Line 113: Line 113:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> size% = 256
<syntaxhighlight lang="bbcbasic"> size% = 256


VDU 23,22,size%;size%;8,8,16,0
VDU 23,22,size%;size%;8,8,16,0
Line 137: Line 137:


REPEAT WAIT 1 : UNTIL FALSE
REPEAT WAIT 1 : UNTIL FALSE
</syntaxhighlight>
</lang>


=={{header|Befunge}}==
=={{header|Befunge}}==
Line 143: Line 143:
Writes the image to stdout using the PPM format.
Writes the image to stdout using the PPM format.


<lang befunge>55+::"3P",,,28*:*::..\,:.\,:v
<syntaxhighlight lang="befunge">55+::"3P",,,28*:*::..\,:.\,:v
>2%*28*:**-2/\1-:v<:8:-1<_@ v
>2%*28*:**-2/\1-:v<:8:-1<_@ v
^\-1*2%2/*:*82::\_$0.0..:^:*<</lang>
^\-1*2%2/*:*82::\_$0.0..:^:*<</syntaxhighlight>


=={{header|Burlesque}}==
=={{header|Burlesque}}==


<lang burlesque>
<syntaxhighlight lang="burlesque">
blsq ) 0 25r@{0 25r@\/{$$Sh2' P[}\/+]m[}m[sp
blsq ) 0 25r@{0 25r@\/{$$Sh2' P[}\/+]m[}m[sp
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Line 177: Line 177:
24 25 26 27 28 29 30 31 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1
24 25 26 27 28 29 30 31 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1
25 24 27 26 29 28 31 30 17 16 19 18 21 20 23 22 9 8 11 10 13 12 15 14 1 0
25 24 27 26 29 28 31 30 17 16 19 18 21 20 23 22 9 8 11 10 13 12 15 14 1 0
</syntaxhighlight>
</lang>


Must be converted to an image with a seperate program.
Must be converted to an image with a seperate program.


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdlib.h>
<syntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <math.h>
#include <math.h>
Line 225: Line 225:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}} [[Image:Xor_pattern_c.png|C output|200px]]
{{out}} [[Image:Xor_pattern_c.png|C output|200px]]


=={{header|C sharp}}==
=={{header|C sharp}}==
<lang csharp>using System.Drawing;
<syntaxhighlight lang="csharp">using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Imaging;
using System.Linq;
using System.Linq;
Line 252: Line 252:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
[[File:XORPatternCSharp.png|XORPatternCSharp.png]]
[[File:XORPatternCSharp.png|XORPatternCSharp.png]]
Line 258: Line 258:
=={{header|C++}}==
=={{header|C++}}==
[[File:msquares_cpp2.png|300px]]
[[File:msquares_cpp2.png|300px]]
<lang cpp>
<syntaxhighlight lang="cpp">
#include <windows.h>
#include <windows.h>
#include <string>
#include <string>
Line 420: Line 420:
}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>


=={{header|Commodore BASIC}}==
=={{header|Commodore BASIC}}==
Line 426: Line 426:
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"):
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"):


<lang basic>100 FOR I=0 TO 24
<syntaxhighlight lang="basic">100 FOR I=0 TO 24
110 : Y=INT(I*127/24)
110 : Y=INT(I*127/24)
120 : FOR J=0 TO 39
120 : FOR J=0 TO 39
Line 437: Line 437:
190 : NEXT J
190 : NEXT J
210 NEXT I
210 NEXT I
220 GETKEY K$</lang>
220 GETKEY K$</syntaxhighlight>


{{Out}}
{{Out}}
Line 443: Line 443:


=={{header|D}}==
=={{header|D}}==
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
import std.stdio;
import std.stdio;


Line 456: Line 456:
f.rawWrite(u3);
f.rawWrite(u3);
}
}
}</lang>
}</syntaxhighlight>


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
Use the '''plot''' library, hsv->rgb ((x xor y) modulo m) as color table, and see the nice results here : http://www.echolalie.org/echolisp/help.html#bit-map .
Use the '''plot''' library, hsv->rgb ((x xor y) modulo m) as color table, and see the nice results here : http://www.echolalie.org/echolisp/help.html#bit-map .
<lang scheme>
<syntaxhighlight lang="scheme">
(lib 'types)
(lib 'types)
(lib 'plot)
(lib 'plot)
Line 477: Line 477:


(plot-much) ;; ESC to see tge drawing
(plot-much) ;; ESC to see tge drawing
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: accessors images images.loader kernel math sequences ;
<syntaxhighlight lang="factor">USING: accessors images images.loader kernel math sequences ;
IN: rosetta-code.munching-squares
IN: rosetta-code.munching-squares


Line 500: Line 500:
: main ( -- ) <munching-img> "munching.png" save-graphic-image ;
: main ( -- ) <munching-img> "munching.png" save-graphic-image ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>
Output image is identical to the Racket version.
Output image is identical to the Racket version.
{{out}}
{{out}}
Line 506: Line 506:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' version 03-11-2016
<syntaxhighlight lang="freebasic">' version 03-11-2016
' compile with: fbc -s gui
' compile with: fbc -s gui


Line 526: Line 526:
WindowTitle "Close window or hit any key to end program"
WindowTitle "Close window or hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>


=={{header|Fōrmulæ}}==
=={{header|Fōrmulæ}}==
Line 538: Line 538:
=={{header|GLSL}}==
=={{header|GLSL}}==
This is an example that will work directly on shadertoy.com, Example [https://www.shadertoy.com/view/Mss3Rs]
This is an example that will work directly on shadertoy.com, Example [https://www.shadertoy.com/view/Mss3Rs]
<lang GLSL>vec3 color;
<syntaxhighlight lang="glsl">vec3 color;
float c,p;
float c,p;
vec2 b;
vec2 b;
Line 580: Line 580:
gl_FragColor = vec4(color,1.0);
gl_FragColor = vec4(color,1.0);
}</lang>
}</syntaxhighlight>


=={{header|Gnuplot}}==
=={{header|Gnuplot}}==


<lang gnuplot>set pm3d map
<syntaxhighlight lang="gnuplot">set pm3d map
set size square
set size square
set isosamples 255,255
set isosamples 255,255
splot [0:255][0:255]-(floor(x)^floor(y))</lang>
splot [0:255][0:255]-(floor(x)^floor(y))</syntaxhighlight>
{{out}} [[Image:gnuplot_xor.png|Gnuplot output|200px]]
{{out}} [[Image:gnuplot_xor.png|Gnuplot output|200px]]


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 607: Line 607:
png.Encode(f, g)
png.Encode(f, g)
f.Close()
f.Close()
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import qualified Data.ByteString as BY (writeFile, pack)
<syntaxhighlight lang="haskell">import qualified Data.ByteString as BY (writeFile, pack)


import Data.Bits (xor)
import Data.Bits (xor)
Line 622: Line 622:
[ x `xor` y
[ x `xor` y
| x <- [0 .. 255]
| x <- [0 .. 255]
, y <- [0 .. 255] ]))</lang>
, y <- [0 .. 255] ]))</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
[[File:XORimage-unicon-GR512.png|thumb|right|512x512 bit green and red]]
[[File:XORimage-unicon-GR512.png|thumb|right|512x512 bit green and red]]
<lang Icon>link printf
<syntaxhighlight lang="icon">link printf


procedure main(A) #: XOR graphic
procedure main(A) #: XOR graphic
Line 643: Line 643:
until Event() == &lpress # wait for left button to quit
until Event() == &lpress # wait for left button to quit
close(&window)
close(&window)
end</lang>
end</syntaxhighlight>


{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}
Line 649: Line 649:


=={{header|J}}==
=={{header|J}}==
<lang J> require 'viewmat'
<syntaxhighlight lang="j"> require 'viewmat'
viewmat ~:"1/&.#: ~ i.256</lang>
viewmat ~:"1/&.#: ~ i.256</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{libheader|Swing}}
{{libheader|Swing}}
This example will repeat the pattern if you expand the window.
This example will repeat the pattern if you expand the window.
<lang java>import java.awt.Color;
<syntaxhighlight lang="java">import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics;


Line 685: Line 685:
new XorPattern();
new XorPattern();
}
}
}</lang>
}</syntaxhighlight>
[[Image:Xor pattern Java.png|200px]]
[[Image:Xor pattern Java.png|200px]]


Line 691: Line 691:
{{works with|jq|1.4}}
{{works with|jq|1.4}}
The following is an adaptation of the Ruby entry, but generates an SVG image file:
The following is an adaptation of the Ruby entry, but generates an SVG image file:
<lang sh>jq -n -r -f Munching_squares.jq > Munching_squares.svg</lang>
<syntaxhighlight lang="sh">jq -n -r -f Munching_squares.jq > Munching_squares.svg</syntaxhighlight>
'''Part 1: Infrastructure'''
'''Part 1: Infrastructure'''
<lang jq># Convert the input integer to an array of bits with lsb first
<syntaxhighlight lang="jq"># Convert the input integer to an array of bits with lsb first
def integer_to_lsb:
def integer_to_lsb:
[recurse(if . > 0 then ./2|floor else empty end) | . % 2] ;
[recurse(if . > 0 then ./2|floor else empty end) | . % 2] ;
Line 715: Line 715:
| reduce range(0;$length) as $i
| reduce range(0;$length) as $i
([]; . + [ lxor($s[$i]; $t[$i]) ] )
([]; . + [ lxor($s[$i]; $t[$i]) ] )
| lsb_to_integer;</lang>
| lsb_to_integer;</syntaxhighlight>
'''Part 2: SVG'''
'''Part 2: SVG'''
<lang jq>def rgb2rgb:
<syntaxhighlight lang="jq">def rgb2rgb:
def p: (. + 0.5) | floor; # to nearest integer
def p: (. + 0.5) | floor; # to nearest integer
"rgb(\(.red|p),\(.green|p),\(.blue|p))";
"rgb(\(.red|p),\(.green|p),\(.blue|p))";
Line 727: Line 727:
def pixel(x; y; color):
def pixel(x; y; color):
(color | if type == "string" then . else rgb2rgb end) as $c
(color | if type == "string" then . else rgb2rgb end) as $c
| "<circle r='1' cx='\(x)' cy='\(y)' fill='\($c)' />";</lang>
| "<circle r='1' cx='\(x)' cy='\(y)' fill='\($c)' />";</syntaxhighlight>
'''Part 3: xor pattern'''
'''Part 3: xor pattern'''
<lang jq># rgb is a JSON object: { "red": _, "green": _, "blue": _}
<syntaxhighlight lang="jq"># rgb is a JSON object: { "red": _, "green": _, "blue": _}


def xor_pattern(width; height; rgb1; rgb2):
def xor_pattern(width; height; rgb1; rgb2):
Line 745: Line 745:
| range(0;height) as $y
| range(0;height) as $y
| pixel($x; $y; $colours[ xor($x; $y) % $size] ) ) ),
| pixel($x; $y; $colours[ xor($x; $y) % $size] ) ) ),
"</svg>" ;</lang>
"</svg>" ;</syntaxhighlight>
'''Part 4: Example'''
'''Part 4: Example'''
<lang jq>def black: { "red": 0, "green": 0, "blue": 0};
<syntaxhighlight lang="jq">def black: { "red": 0, "green": 0, "blue": 0};
def red: black + { "red": 255 };
def red: black + { "red": 255 };
def yellow: red + { "green": 255 };
def yellow: red + { "green": 255 };


xor_pattern(384; 384; red; yellow)</lang>
xor_pattern(384; 384; red; yellow)</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Gtk, Cairo
<syntaxhighlight lang="julia">using Gtk, Cairo


const can = @GtkCanvas()
const can = @GtkCanvas()
Line 773: Line 773:
signal_connect(endit, win, :destroy)
signal_connect(endit, win, :destroy)
wait(cond)
wait(cond)
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.4-3
<syntaxhighlight lang="scala">// version 1.1.4-3


import javax.swing.JFrame
import javax.swing.JFrame
Line 822: Line 822:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
nomainwin


Line 857: Line 857:
end
end
end sub
end sub
</syntaxhighlight>
</lang>
Image available at [[http://www.diga.me.uk/xorRC.gif]]
Image available at [[http://www.diga.me.uk/xorRC.gif]]


=={{header|Lua}}==
=={{header|Lua}}==
{{works with|LÖVE|11.0 or higher}}
{{works with|LÖVE|11.0 or higher}}
<lang lua>local clr = {}
<syntaxhighlight lang="lua">local clr = {}
function drawMSquares()
function drawMSquares()
local points = {}
local points = {}
Line 895: Line 895:
love.graphics.setColor(1,1,1)
love.graphics.setColor(1,1,1)
love.graphics.draw(canvas)
love.graphics.draw(canvas)
end</lang>
end</syntaxhighlight>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ListDensityPlot[
<syntaxhighlight lang="mathematica">ListDensityPlot[
Table[Table[
Table[Table[
FromDigits[BitXor[IntegerDigits[x, 2, 8], IntegerDigits[y, 2, 8]],
FromDigits[BitXor[IntegerDigits[x, 2, 8], IntegerDigits[y, 2, 8]],
2], {x, 0, 255}], {y, 0, 255}]]</lang>
2], {x, 0, 255}], {y, 0, 255}]]</syntaxhighlight>
{{out|Output #1}}
{{out|Output #1}}
[[File:xorpattern3.png|Mathematica output #1|200px]]
[[File:xorpattern3.png|Mathematica output #1|200px]]


<lang Mathematica>ArrayPlot[Array[BitXor, {511, 511}]]</lang>
<syntaxhighlight lang="mathematica">ArrayPlot[Array[BitXor, {511, 511}]]</syntaxhighlight>
{{out|Output #2}}
{{out|Output #2}}
[[File:xorpattern4.png|Mathematica output #2|200px]]
[[File:xorpattern4.png|Mathematica output #2|200px]]


=={{header|MATLAB}}==
=={{header|MATLAB}}==
<lang matlab>size = 256;
<syntaxhighlight lang="matlab">size = 256;
[x,y] = meshgrid([0:size-1]);
[x,y] = meshgrid([0:size-1]);


Line 917: Line 917:
colormap bone(size);
colormap bone(size);
image(c);
image(c);
axis equal;</lang>
axis equal;</syntaxhighlight>
{{out}} [[File:matlab_xor.png|MATLAB output|200px]]
{{out}} [[File:matlab_xor.png|MATLAB output|200px]]


=={{header|Microsoft Small Basic}}==
=={{header|Microsoft Small Basic}}==
<lang smallbasic>' Munching squares - smallbasic - 27/07/2018
<syntaxhighlight lang="smallbasic">' Munching squares - smallbasic - 27/07/2018
size=256
size=256
GraphicsWindow.Width=size
GraphicsWindow.Width=size
Line 964: Line 964:
x=t
x=t
EndFor
EndFor
EndSub </lang>
EndSub </syntaxhighlight>
{{out}}
{{out}}
[https://github.com/Pat-Garrett/RC/blob/master/Munching%20squares%20-%20vbnet.jpg Munching squares - SmallBasic]
[https://github.com/Pat-Garrett/RC/blob/master/Munching%20squares%20-%20vbnet.jpg Munching squares - SmallBasic]
Line 971: Line 971:
This version runs in Mini Micro (for the graphics). Note that because MiniScript does not currently have any bit operations (all numbers are floating-point), we have to implement an <code>xor</code> function the hard way.
This version runs in Mini Micro (for the graphics). Note that because MiniScript does not currently have any bit operations (all numbers are floating-point), we have to implement an <code>xor</code> function the hard way.


<lang MiniScript>xor = function(a, b)
<syntaxhighlight lang="miniscript">xor = function(a, b)
result = 0
result = 0
bit = 1
bit = 1
Line 987: Line 987:
gfx.setPixel x, y, color.rgb(0, xor(x,y), 0)
gfx.setPixel x, y, color.rgb(0, xor(x,y), 0)
end for
end for
end for</lang>
end for</syntaxhighlight>


{{out}}
{{out}}
Line 994: Line 994:
=={{header|Nim}}==
=={{header|Nim}}==
{{libheader|imageman}}
{{libheader|imageman}}
<lang Nim>import random
<syntaxhighlight lang="nim">import random
import imageman
import imageman


Line 1,011: Line 1,011:
image[i, j] = colors[i xor j]
image[i, j] = colors[i xor j]


image.savePNG("munching_squares.png")</lang>
image.savePNG("munching_squares.png")</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>open Graphics
<syntaxhighlight lang="ocaml">open Graphics


let () =
let () =
Line 1,027: Line 1,027:
done;
done;
done;
done;
ignore(read_key())</lang>
ignore(read_key())</syntaxhighlight>


Run with:
Run with:
Line 1,036: Line 1,036:


=={{header|Octave}}==
=={{header|Octave}}==
<lang Octave>size = 256;
<syntaxhighlight lang="octave">size = 256;
[x,y] = meshgrid([0:size-1]);
[x,y] = meshgrid([0:size-1]);


Line 1,043: Line 1,043:
colormap(jet(size));
colormap(jet(size));
image(c);
image(c);
axis equal;</lang>
axis equal;</syntaxhighlight>
{{out}} [[File:Xor_pattern_octave.png|Octave output|320px]]
{{out}} [[File:Xor_pattern_octave.png|Octave output|320px]]


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use GD;
<syntaxhighlight lang="perl">use GD;


my $img = new GD::Image(256, 256, 1);
my $img = new GD::Image(256, 256, 1);
Line 1,058: Line 1,058:
}
}


print $img->png</lang>
print $img->png</syntaxhighlight>
{{out}} [[File:perl_xor_pattern.png|Perl output|200px]]
{{out}} [[File:perl_xor_pattern.png|Perl output|200px]]


Line 1,065: Line 1,065:
{{libheader|Phix/online}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Munching_squares.htm here].
You can run this online [http://phix.x10.mx/p2js/Munching_squares.htm here].
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Munching_squares.exw
-- demo\rosetta\Munching_squares.exw
Line 1,110: Line 1,110:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PHP}}==
=={{header|PHP}}==
<lang php>header("Content-Type: image/png");
<syntaxhighlight lang="php">header("Content-Type: image/png");


$w = 256;
$w = 256;
Line 1,136: Line 1,136:


imagepng($im);
imagepng($im);
imagedestroy($im);</lang>
imagedestroy($im);</syntaxhighlight>
{{out}}
{{out}}
[[File:xor_pattern_php.png|PHP output|200px]]
[[File:xor_pattern_php.png|PHP output|200px]]


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>munch: procedure options (main); /* 21 May 2014 */
<syntaxhighlight lang="pl/i">munch: procedure options (main); /* 21 May 2014 */


declare screen (0:255, 0:255) bit(24) aligned;
declare screen (0:255, 0:255) bit(24) aligned;
Line 1,154: Line 1,154:
end;
end;
call writeppm(screen);
call writeppm(screen);
end munch;</lang>
end munch;</syntaxhighlight>


=={{header|Processing}}==
=={{header|Processing}}==
Renders grayscale image, the larger the window the more the squares will repeat along the main diagonal from top left to bottom right.
Renders grayscale image, the larger the window the more the squares will repeat along the main diagonal from top left to bottom right.


<lang java>
<syntaxhighlight lang="java">
//Aamrun, 26th June 2022
//Aamrun, 26th June 2022


Line 1,173: Line 1,173:


updatePixels();
updatePixels();
</syntaxhighlight>
</lang>


=={{header|Prolog}}==
=={{header|Prolog}}==
Works with SWI-Prolog and his GUI XPCE.
Works with SWI-Prolog and his GUI XPCE.
<lang Prolog>xor_pattern :-
<syntaxhighlight lang="prolog">xor_pattern :-
new(D, window('XOR Pattern')),
new(D, window('XOR Pattern')),
send(D, size, size(512,512)),
send(D, size, size(512,512)),
Line 1,195: Line 1,195:
send(D, display, Bmp, point(0,0)),
send(D, display, Bmp, point(0,0)),
send(D, open).
send(D, open).
</syntaxhighlight>
</lang>
[[File:Prolog_xor_pattern.png|200px]]
[[File:Prolog_xor_pattern.png|200px]]


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang purebasic>#palletteSize = 128
<syntaxhighlight lang="purebasic">#palletteSize = 128
Procedure.f XorPattern(x, y) ;compute the gradient value from the pixel values
Procedure.f XorPattern(x, y) ;compute the gradient value from the pixel values
Protected result = x ! y
Protected result = x ! y
Line 1,224: Line 1,224:
event = WaitWindowEvent(20)
event = WaitWindowEvent(20)
Until event = #PB_Event_CloseWindow
Until event = #PB_Event_CloseWindow
EndIf</lang>
EndIf</syntaxhighlight>
[[File:PureBasic_XOR_Pattern.png|Sample display of PureBasic solution|200px]]
[[File:PureBasic_XOR_Pattern.png|Sample display of PureBasic solution|200px]]


=={{header|Python}}==
=={{header|Python}}==
{{libheader|PIL}}
{{libheader|PIL}}
<lang Python>import Image, ImageDraw
<syntaxhighlight lang="python">import Image, ImageDraw


image = Image.new("RGB", (256, 256))
image = Image.new("RGB", (256, 256))
Line 1,239: Line 1,239:


del drawingTool
del drawingTool
image.save("xorpic.png", "PNG")</lang>
image.save("xorpic.png", "PNG")</syntaxhighlight>
[[File:PythonXORPic.png|Sample produced by the above code|200px]]
[[File:PythonXORPic.png|Sample produced by the above code|200px]]


=={{header|QBasic}}==
=={{header|QBasic}}==
<lang qbasic>w = 254
<syntaxhighlight lang="qbasic">w = 254


SCREEN 13
SCREEN 13
Line 1,253: Line 1,253:
PSET (x, y)
PSET (x, y)
NEXT y
NEXT y
NEXT x</lang>
NEXT x</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
[[File:munching-racket.png|thumb|right]]
[[File:munching-racket.png|thumb|right]]
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(require racket/draw)
(require racket/draw)
Line 1,267: Line 1,267:
(send dc set-pixel x y c))
(send dc set-pixel x y c))
bm
bm
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 1,274: Line 1,274:
Here's one simple way:
Here's one simple way:


<lang perl6>my $ppm = open("munching0.ppm", :w) orelse .die;
<syntaxhighlight lang="raku" line>my $ppm = open("munching0.ppm", :w) orelse .die;


$ppm.print(q :to 'EOT');
$ppm.print(q :to 'EOT');
Line 1,291: Line 1,291:


$ppm.close();
$ppm.close();
</syntaxhighlight>
</lang>


Another way:
Another way:


<lang perl6>my @colors = map -> $r, $g, $b { Buf.new: $r, $g, $b },
<syntaxhighlight lang="raku" line>my @colors = map -> $r, $g, $b { Buf.new: $r, $g, $b },
map -> $x { floor ($x/256) ** 3 * 256 },
map -> $x { floor ($x/256) ** 3 * 256 },
(flat (0...255) Z
(flat (0...255) Z
Line 1,313: Line 1,313:
$PPM.write: @colors[$_] for ^256 X+^ ^256;
$PPM.write: @colors[$_] for ^256 X+^ ^256;


$PPM.close;</lang>
$PPM.close;</syntaxhighlight>
[[File:perl_6_xor_pattern.png|Raku output|200px]]
[[File:perl_6_xor_pattern.png|Raku output|200px]]


=={{header|REXX}}==
=={{header|REXX}}==
{{trans|Burlesque}}
{{trans|Burlesque}}
<lang rexx>/*REXX program renders a graphical pattern by coloring each pixel with x XOR y */
<syntaxhighlight lang="rexx">/*REXX program renders a graphical pattern by coloring each pixel with x XOR y */
/*───────────────────────────────────────── from an arbitrary constructed color table. */
/*───────────────────────────────────────── from an arbitrary constructed color table. */
rows= 2 /*the number of rows in the color table*/
rows= 2 /*the number of rows in the color table*/
Line 1,335: Line 1,335:
@.x.y= bitxor(@.x, @.y) /*renders 3 bytes (a pixel) at a time. */
@.x.y= bitxor(@.x, @.y) /*renders 3 bytes (a pixel) at a time. */
end /*y*/
end /*y*/
end /*x*/ /*stick a fork in it, we're all done. */</lang>
end /*x*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
Must be converted to an image with a separate program. <br><br>
Must be converted to an image with a separate program. <br><br>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Munching squares
# Project : Munching squares


Line 1,394: Line 1,394:
label1 { setpicture(p1) show() }
label1 { setpicture(p1) show() }
return
return
</syntaxhighlight>
</lang>
Output:
Output:


Line 1,402: Line 1,402:
Uses [[Raster graphics operations/Ruby]]
Uses [[Raster graphics operations/Ruby]]
[[File:xorpattern_rb.png|thumb|right|Sample output from Ruby program]]
[[File:xorpattern_rb.png|thumb|right|Sample output from Ruby program]]
<lang ruby>load 'raster_graphics.rb'
<syntaxhighlight lang="ruby">load 'raster_graphics.rb'


class Pixmap
class Pixmap
Line 1,426: Line 1,426:


img = Pixmap.xor_pattern(384, 384, RGBColour::RED, RGBColour::YELLOW)
img = Pixmap.xor_pattern(384, 384, RGBColour::RED, RGBColour::YELLOW)
img.save_as_png('xorpattern.png')</lang>
img.save_as_png('xorpattern.png')</syntaxhighlight>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>w = 100
<syntaxhighlight lang="runbasic">w = 100
graphic #g, w,w
graphic #g, w,w
for x = 0 to w
for x = 0 to w
Line 1,439: Line 1,439:
next x
next x
render #g
render #g
#g "flush"</lang>
#g "flush"</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>extern crate image;
<syntaxhighlight lang="rust">extern crate image;


use image::{ImageBuffer, Pixel, Rgb};
use image::{ImageBuffer, Pixel, Rgb};
Line 1,457: Line 1,457:


let _ = img.save("output.png");
let _ = img.save("output.png");
}</lang>
}</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
===Scala Swing===
===Scala Swing===
{{libheader|org.scala-lang.modules scala-swing}}
{{libheader|org.scala-lang.modules scala-swing}}
<lang Scala>import scala.swing.Swing.pair2Dimension
<syntaxhighlight lang="scala">import scala.swing.Swing.pair2Dimension
import scala.swing.{Color, Graphics2D, MainFrame, Panel, SimpleSwingApplication}
import scala.swing.{Color, Graphics2D, MainFrame, Panel, SimpleSwingApplication}


Line 1,486: Line 1,486:
centerOnScreen()
centerOnScreen()
}
}
}</lang>
}</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<lang ruby>require('Imager')
<syntaxhighlight lang="ruby">require('Imager')


var img = %O<Imager>.new(xsize => 256, ysize => 256)
var img = %O<Imager>.new(xsize => 256, ysize => 256)
Line 1,499: Line 1,499:
}
}


img.write(file => 'xor.png')</lang>
img.write(file => 'xor.png')</syntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/munching-squares-sidef.png Munching squares]
Output image: [https://github.com/trizen/rc/blob/master/img/munching-squares-sidef.png Munching squares]


=={{header|Tcl}}==
=={{header|Tcl}}==
{{libheader|Tk}}<lang tcl>package require Tk
{{libheader|Tk}}<syntaxhighlight lang="tcl">package require Tk


proc xorImage {img table} {
proc xorImage {img table} {
Line 1,529: Line 1,529:
set img [image create photo -width 512 -height 512]
set img [image create photo -width 512 -height 512]
xorImage $img [mkTable 0 255 64 192 255 0]
xorImage $img [mkTable 0 255 64 192 255 0]
pack [label .l -image $img]</lang>
pack [label .l -image $img]</syntaxhighlight>


=={{header|TI-83 BASIC}}==
=={{header|TI-83 BASIC}}==
Line 1,537: Line 1,537:




<lang ti-83b>PROGRAM:XORPATT
<syntaxhighlight lang="ti-83b">PROGRAM:XORPATT
" •.-,+-°-1+o*:πOX"→Str1
" •.-,+-°-1+o*:πOX"→Str1


Line 1,613: Line 1,613:
End
End
Pause
Pause
</syntaxhighlight>
</lang>


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|2011}}
{{works with|Visual Basic .NET|2011}}
<lang vbnet>' Munching squares - 27/07/2018
<syntaxhighlight lang="vbnet">' Munching squares - 27/07/2018
Public Class MunchingSquares
Public Class MunchingSquares
Const xsize = 256
Const xsize = 256
Line 1,638: Line 1,638:
End Sub 'Paint
End Sub 'Paint


End Class </lang>
End Class </syntaxhighlight>
{{out}}
{{out}}
[https://github.com/Pat-Garrett/RC/blob/7e9842513d361a5b4241bc6bb28f9985c2bfe161/Munching%20squares%20-%20vbnet.jpg Munching squares - vbnet]
[https://github.com/Pat-Garrett/RC/blob/7e9842513d361a5b4241bc6bb28f9985c2bfe161/Munching%20squares%20-%20vbnet.jpg Munching squares - vbnet]
Line 1,645: Line 1,645:
{{trans|D}}
{{trans|D}}
{{libheader|DOME}}
{{libheader|DOME}}
<lang ecmascript>import "graphics" for Canvas, Color
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
import "dome" for Window


Line 1,666: Line 1,666:


static draw(alpha) {}
static draw(alpha) {}
}</lang>
}</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int X, Y;
int X, Y;
[SetVid($101); \set 640x480 graphics with 8-bit color
[SetVid($101); \set 640x480 graphics with 8-bit color
Line 1,683: Line 1,683:
X:= ChIn(1); \wait for keystroke
X:= ChIn(1); \wait for keystroke
SetVid(3); \restore normal text mode
SetVid(3); \restore normal text mode
]</lang>
]</syntaxhighlight>
{{out}} [[File:MunchXPL0.png]]
{{out}} [[File:MunchXPL0.png]]


=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang Yabasic>w = 256
<syntaxhighlight lang="yabasic">w = 256
open window w, w
open window w, w
Line 1,698: Line 1,698:
dot x, y
dot x, y
Next
Next
Next</lang>
Next</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
Line 1,704: Line 1,704:
{{trans|XPL0}}
{{trans|XPL0}}
For a kaleidoscopic image, play with coolness.
For a kaleidoscopic image, play with coolness.
<lang zkl>fcn muncher{
<syntaxhighlight lang="zkl">fcn muncher{
bitmap:=PPM(256,256);
bitmap:=PPM(256,256);
coolness:=(1).random(0x10000); // 55379, 18180, 40, 51950, 57619, 43514, 65465
coolness:=(1).random(0x10000); // 55379, 18180, 40, 51950, 57619, 43514, 65465
Line 1,715: Line 1,715:
}
}
bitmap.write(File("foo.ppm","wb"));
bitmap.write(File("foo.ppm","wb"));
}();</lang>
}();</syntaxhighlight>
For a cheap slide show (on Linux):
For a cheap slide show (on Linux):
<lang zkl>while(1){ muncher(); Atomic.sleep(3); }</lang>
<syntaxhighlight lang="zkl">while(1){ muncher(); Atomic.sleep(3); }</syntaxhighlight>
run ImageViewer on foo.ppm and watch it [auto] update as the image changes.
run ImageViewer on foo.ppm and watch it [auto] update as the image changes.
{{out}}
{{out}}