Munching squares: Difference between revisions
m
syntax highlighting fixup automation
(Applesoft BASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 6:
=={{header|Action!}}==
<
BYTE i
Line 36:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Munching_squares.png Screenshot from Atari 8-bit computer]
Line 43:
{{libheader|GtkAda}}
Uses the Cairo component of GtkAda to create and save as png
<
with Cairo.Png; use Cairo.Png;
with Cairo.Image_Surface; use Cairo.Image_Surface;
Line 63:
Status := Write_To_Png (Surface, "AdaXorPattern.png");
pragma Assert (Status = Cairo_Status_Success);
end XorPattern;</
{{out}} [[Image:AdaXorPattern.png|Ada Output|200px]]
=={{header|Applesoft BASIC}}==
<
110 DATA 0,1, 3,10,5, 3,11,15
120 DATA 0,8, 9,10,5, 9,13,15
Line 91:
320 COLOR= C( PEEK (235))
330 VLIN Y1,Y1 + 1 AT W * S + X
340 NEXT X,Y,W,H</
=={{header|AWK}}==
{{works with|gawk}}
This program generates a PPM image, that you can view/convert using The GIMP or ImageMagick
<
BEGIN {
# square size
Line 113:
}
}
</syntaxhighlight>
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
VDU 23,22,size%;size%;8,8,16,0
Line 137:
REPEAT WAIT 1 : UNTIL FALSE
</syntaxhighlight>
=={{header|Befunge}}==
Line 143:
Writes the image to stdout using the PPM format.
<
>2%*28*:**-2/\1-:v<:8:-1<_@ v
^\-1*2%2/*:*82::\_$0.0..:^:*<</
=={{header|Burlesque}}==
<
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
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
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>
Must be converted to an image with a seperate program.
=={{header|C}}==
<
#include <stdio.h>
#include <math.h>
Line 225:
return 0;
}</
{{out}} [[Image:Xor_pattern_c.png|C output|200px]]
=={{header|C sharp}}==
<
using System.Drawing.Imaging;
using System.Linq;
Line 252:
}
}
}</
{{out}}
[[File:XORPatternCSharp.png|XORPatternCSharp.png]]
Line 258:
=={{header|C++}}==
[[File:msquares_cpp2.png|300px]]
<
#include <windows.h>
#include <string>
Line 420:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
=={{header|Commodore BASIC}}==
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"):
<
110 : Y=INT(I*127/24)
120 : FOR J=0 TO 39
Line 437:
190 : NEXT J
210 NEXT I
220 GETKEY K$</
{{Out}}
Line 443:
=={{header|D}}==
<
import std.stdio;
Line 456:
f.rawWrite(u3);
}
}</
=={{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 .
<
(lib 'types)
(lib 'plot)
Line 477:
(plot-much) ;; ESC to see tge drawing
</syntaxhighlight>
=={{header|Factor}}==
<
IN: rosetta-code.munching-squares
Line 500:
: main ( -- ) <munching-img> "munching.png" save-graphic-image ;
MAIN: main</
Output image is identical to the Racket version.
{{out}}
Line 506:
=={{header|FreeBASIC}}==
<
' compile with: fbc -s gui
Line 526:
WindowTitle "Close window or hit any key to end program"
Sleep
End</
=={{header|Fōrmulæ}}==
Line 538:
=={{header|GLSL}}==
This is an example that will work directly on shadertoy.com, Example [https://www.shadertoy.com/view/Mss3Rs]
<
float c,p;
vec2 b;
Line 580:
gl_FragColor = vec4(color,1.0);
}</
=={{header|Gnuplot}}==
<
set size square
set isosamples 255,255
splot [0:255][0:255]-(floor(x)^floor(y))</
{{out}} [[Image:gnuplot_xor.png|Gnuplot output|200px]]
=={{header|Go}}==
<
import (
Line 607:
png.Encode(f, g)
f.Close()
}</
=={{header|Haskell}}==
<
import Data.Bits (xor)
Line 622:
[ x `xor` y
| x <- [0 .. 255]
, y <- [0 .. 255] ]))</
=={{header|Icon}} and {{header|Unicon}}==
[[File:XORimage-unicon-GR512.png|thumb|right|512x512 bit green and red]]
<
procedure main(A) #: XOR graphic
Line 643:
until Event() == &lpress # wait for left button to quit
close(&window)
end</
{{libheader|Icon Programming Library}}
Line 649:
=={{header|J}}==
<
viewmat ~:"1/&.#: ~ i.256</
=={{header|Java}}==
{{libheader|Swing}}
This example will repeat the pattern if you expand the window.
<
import java.awt.Graphics;
Line 685:
new XorPattern();
}
}</
[[Image:Xor pattern Java.png|200px]]
Line 691:
{{works with|jq|1.4}}
The following is an adaptation of the Ruby entry, but generates an SVG image file:
<
'''Part 1: Infrastructure'''
<
def integer_to_lsb:
[recurse(if . > 0 then ./2|floor else empty end) | . % 2] ;
Line 715:
| reduce range(0;$length) as $i
([]; . + [ lxor($s[$i]; $t[$i]) ] )
| lsb_to_integer;</
'''Part 2: SVG'''
<
def p: (. + 0.5) | floor; # to nearest integer
"rgb(\(.red|p),\(.green|p),\(.blue|p))";
Line 727:
def pixel(x; y; color):
(color | if type == "string" then . else rgb2rgb end) as $c
| "<circle r='1' cx='\(x)' cy='\(y)' fill='\($c)' />";</
'''Part 3: xor pattern'''
<
def xor_pattern(width; height; rgb1; rgb2):
Line 745:
| range(0;height) as $y
| pixel($x; $y; $colours[ xor($x; $y) % $size] ) ) ),
"</svg>" ;</
'''Part 4: Example'''
<
def red: black + { "red": 255 };
def yellow: red + { "green": 255 };
xor_pattern(384; 384; red; yellow)</
=={{header|Julia}}==
<
const can = @GtkCanvas()
Line 773:
signal_connect(endit, win, :destroy)
wait(cond)
</syntaxhighlight>
=={{header|Kotlin}}==
<
import javax.swing.JFrame
Line 822:
}
}
}</
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
nomainwin
Line 857:
end
end sub
</syntaxhighlight>
Image available at [[http://www.diga.me.uk/xorRC.gif]]
=={{header|Lua}}==
{{works with|LÖVE|11.0 or higher}}
<
function drawMSquares()
local points = {}
Line 895:
love.graphics.setColor(1,1,1)
love.graphics.draw(canvas)
end</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Table[Table[
FromDigits[BitXor[IntegerDigits[x, 2, 8], IntegerDigits[y, 2, 8]],
2], {x, 0, 255}], {y, 0, 255}]]</
{{out|Output #1}}
[[File:xorpattern3.png|Mathematica output #1|200px]]
<
{{out|Output #2}}
[[File:xorpattern4.png|Mathematica output #2|200px]]
=={{header|MATLAB}}==
<
[x,y] = meshgrid([0:size-1]);
Line 917:
colormap bone(size);
image(c);
axis equal;</
{{out}} [[File:matlab_xor.png|MATLAB output|200px]]
=={{header|Microsoft Small Basic}}==
<
size=256
GraphicsWindow.Width=size
Line 964:
x=t
EndFor
EndSub </
{{out}}
[https://github.com/Pat-Garrett/RC/blob/master/Munching%20squares%20-%20vbnet.jpg Munching squares - SmallBasic]
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.
<
result = 0
bit = 1
Line 987:
gfx.setPixel x, y, color.rgb(0, xor(x,y), 0)
end for
end for</
{{out}}
Line 994:
=={{header|Nim}}==
{{libheader|imageman}}
<
import imageman
Line 1,011:
image[i, j] = colors[i xor j]
image.savePNG("munching_squares.png")</
=={{header|OCaml}}==
<
let () =
Line 1,027:
done;
done;
ignore(read_key())</
Run with:
Line 1,036:
=={{header|Octave}}==
<
[x,y] = meshgrid([0:size-1]);
Line 1,043:
colormap(jet(size));
image(c);
axis equal;</
{{out}} [[File:Xor_pattern_octave.png|Octave output|320px]]
=={{header|Perl}}==
<
my $img = new GD::Image(256, 256, 1);
Line 1,058:
}
print $img->png</
{{out}} [[File:perl_xor_pattern.png|Perl output|200px]]
Line 1,065:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Munching_squares.htm here].
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Munching_squares.exw
Line 1,110:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
=={{header|PHP}}==
<
$w = 256;
Line 1,136:
imagepng($im);
imagedestroy($im);</
{{out}}
[[File:xor_pattern_php.png|PHP output|200px]]
=={{header|PL/I}}==
<
declare screen (0:255, 0:255) bit(24) aligned;
Line 1,154:
end;
call writeppm(screen);
end munch;</
=={{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.
<
//Aamrun, 26th June 2022
Line 1,173:
updatePixels();
</syntaxhighlight>
=={{header|Prolog}}==
Works with SWI-Prolog and his GUI XPCE.
<
new(D, window('XOR Pattern')),
send(D, size, size(512,512)),
Line 1,195:
send(D, display, Bmp, point(0,0)),
send(D, open).
</syntaxhighlight>
[[File:Prolog_xor_pattern.png|200px]]
=={{header|PureBasic}}==
<
Procedure.f XorPattern(x, y) ;compute the gradient value from the pixel values
Protected result = x ! y
Line 1,224:
event = WaitWindowEvent(20)
Until event = #PB_Event_CloseWindow
EndIf</
[[File:PureBasic_XOR_Pattern.png|Sample display of PureBasic solution|200px]]
=={{header|Python}}==
{{libheader|PIL}}
<
image = Image.new("RGB", (256, 256))
Line 1,239:
del drawingTool
image.save("xorpic.png", "PNG")</
[[File:PythonXORPic.png|Sample produced by the above code|200px]]
=={{header|QBasic}}==
<
SCREEN 13
Line 1,253:
PSET (x, y)
NEXT y
NEXT x</
=={{header|Racket}}==
[[File:munching-racket.png|thumb|right]]
<
#lang racket
(require racket/draw)
Line 1,267:
(send dc set-pixel x y c))
bm
</syntaxhighlight>
=={{header|Raku}}==
Line 1,274:
Here's one simple way:
<syntaxhighlight lang="raku"
$ppm.print(q :to 'EOT');
Line 1,291:
$ppm.close();
</syntaxhighlight>
Another way:
<syntaxhighlight lang="raku"
map -> $x { floor ($x/256) ** 3 * 256 },
(flat (0...255) Z
Line 1,313:
$PPM.write: @colors[$_] for ^256 X+^ ^256;
$PPM.close;</
[[File:perl_6_xor_pattern.png|Raku output|200px]]
=={{header|REXX}}==
{{trans|Burlesque}}
<
/*───────────────────────────────────────── from an arbitrary constructed color table. */
rows= 2 /*the number of rows in the color table*/
Line 1,335:
@.x.y= bitxor(@.x, @.y) /*renders 3 bytes (a pixel) at a time. */
end /*y*/
end /*x*/ /*stick a fork in it, we're all done. */</
Must be converted to an image with a separate program. <br><br>
=={{header|Ring}}==
<
# Project : Munching squares
Line 1,394:
label1 { setpicture(p1) show() }
return
</syntaxhighlight>
Output:
Line 1,402:
Uses [[Raster graphics operations/Ruby]]
[[File:xorpattern_rb.png|thumb|right|Sample output from Ruby program]]
<
class Pixmap
Line 1,426:
img = Pixmap.xor_pattern(384, 384, RGBColour::RED, RGBColour::YELLOW)
img.save_as_png('xorpattern.png')</
=={{header|Run BASIC}}==
<
graphic #g, w,w
for x = 0 to w
Line 1,439:
next x
render #g
#g "flush"</
=={{header|Rust}}==
<
use image::{ImageBuffer, Pixel, Rgb};
Line 1,457:
let _ = img.save("output.png");
}</
=={{header|Scala}}==
===Scala Swing===
{{libheader|org.scala-lang.modules scala-swing}}
<
import scala.swing.{Color, Graphics2D, MainFrame, Panel, SimpleSwingApplication}
Line 1,486:
centerOnScreen()
}
}</
=={{header|Sidef}}==
{{trans|Perl}}
<
var img = %O<Imager>.new(xsize => 256, ysize => 256)
Line 1,499:
}
img.write(file => 'xor.png')</
Output image: [https://github.com/trizen/rc/blob/master/img/munching-squares-sidef.png Munching squares]
=={{header|Tcl}}==
{{libheader|Tk}}<
proc xorImage {img table} {
Line 1,529:
set img [image create photo -width 512 -height 512]
xorImage $img [mkTable 0 255 64 192 255 0]
pack [label .l -image $img]</
=={{header|TI-83 BASIC}}==
Line 1,537:
<
" •.-,+-°-1+o*:πOX"→Str1
Line 1,613:
End
Pause
</syntaxhighlight>
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|2011}}
<
Public Class MunchingSquares
Const xsize = 256
Line 1,638:
End Sub 'Paint
End Class </
{{out}}
[https://github.com/Pat-Garrett/RC/blob/7e9842513d361a5b4241bc6bb28f9985c2bfe161/Munching%20squares%20-%20vbnet.jpg Munching squares - vbnet]
Line 1,645:
{{trans|D}}
{{libheader|DOME}}
<
import "dome" for Window
Line 1,666:
static draw(alpha) {}
}</
=={{header|XPL0}}==
<
int X, Y;
[SetVid($101); \set 640x480 graphics with 8-bit color
Line 1,683:
X:= ChIn(1); \wait for keystroke
SetVid(3); \restore normal text mode
]</
{{out}} [[File:MunchXPL0.png]]
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<
open window w, w
Line 1,698:
dot x, y
Next
Next</
=={{header|zkl}}==
Line 1,704:
{{trans|XPL0}}
For a kaleidoscopic image, play with coolness.
<
bitmap:=PPM(256,256);
coolness:=(1).random(0x10000); // 55379, 18180, 40, 51950, 57619, 43514, 65465
Line 1,715:
}
bitmap.write(File("foo.ppm","wb"));
}();</
For a cheap slide show (on Linux):
<
run ImageViewer on foo.ppm and watch it [auto] update as the image changes.
{{out}}
|