Jump to content

Munching squares: Difference between revisions

m
syntax highlighting fixup automation
(Applesoft BASIC)
m (syntax highlighting fixup automation)
Line 6:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PutBigPixel(BYTE x,y,c)
BYTE i
 
Line 36:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{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
<langsyntaxhighlight Adalang="ada">with Cairo; use Cairo;
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;</langsyntaxhighlight>
{{out}} [[Image:AdaXorPattern.png|Ada Output|200px]]
 
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight lang="gwbasic"> 100 DATA 0,2, 6,10,5, 6, 7,15
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</langsyntaxhighlight>
=={{header|AWK}}==
{{works with|gawk}}
This program generates a PPM image, that you can view/convert using The GIMP or ImageMagick
<langsyntaxhighlight lang="awk">
BEGIN {
# square size
Line 113:
}
}
</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> size% = 256
 
VDU 23,22,size%;size%;8,8,16,0
Line 137:
 
REPEAT WAIT 1 : UNTIL FALSE
</syntaxhighlight>
</lang>
 
=={{header|Befunge}}==
Line 143:
Writes the image to stdout using the PPM format.
 
<langsyntaxhighlight lang="befunge">55+::"3P",,,28*:*::..\,:.\,:v
>2%*28*:**-2/\1-:v<:8:-1<_@ v
^\-1*2%2/*:*82::\_$0.0..:^:*<</langsyntaxhighlight>
 
=={{header|Burlesque}}==
 
<langsyntaxhighlight lang="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>
</lang>
 
Must be converted to an image with a seperate program.
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <math.h>
Line 225:
 
return 0;
}</langsyntaxhighlight>
{{out}} [[Image:Xor_pattern_c.png|C output|200px]]
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
Line 252:
}
}
}</langsyntaxhighlight>
{{out}}
[[File:XORPatternCSharp.png|XORPatternCSharp.png]]
Line 258:
=={{header|C++}}==
[[File:msquares_cpp2.png|300px]]
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <string>
Line 420:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
 
=={{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"):
 
<langsyntaxhighlight lang="basic">100 FOR I=0 TO 24
110 : Y=INT(I*127/24)
120 : FOR J=0 TO 39
Line 437:
190 : NEXT J
210 NEXT I
220 GETKEY K$</langsyntaxhighlight>
 
{{Out}}
Line 443:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio;
 
Line 456:
f.rawWrite(u3);
}
}</langsyntaxhighlight>
 
=={{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 .
<langsyntaxhighlight lang="scheme">
(lib 'types)
(lib 'plot)
Line 477:
 
(plot-much) ;; ESC to see tge drawing
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: accessors images images.loader kernel math sequences ;
IN: rosetta-code.munching-squares
 
Line 500:
: main ( -- ) <munching-img> "munching.png" save-graphic-image ;
 
MAIN: main</langsyntaxhighlight>
Output image is identical to the Racket version.
{{out}}
Line 506:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 03-11-2016
' compile with: fbc -s gui
 
Line 526:
WindowTitle "Close window or hit any key to end program"
Sleep
End</langsyntaxhighlight>
 
=={{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]
<langsyntaxhighlight GLSLlang="glsl">vec3 color;
float c,p;
vec2 b;
Line 580:
gl_FragColor = vec4(color,1.0);
}</langsyntaxhighlight>
 
=={{header|Gnuplot}}==
 
<langsyntaxhighlight lang="gnuplot">set pm3d map
set size square
set isosamples 255,255
splot [0:255][0:255]-(floor(x)^floor(y))</langsyntaxhighlight>
{{out}} [[Image:gnuplot_xor.png|Gnuplot output|200px]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 607:
png.Encode(f, g)
f.Close()
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import qualified Data.ByteString as BY (writeFile, pack)
 
import Data.Bits (xor)
Line 622:
[ x `xor` y
| x <- [0 .. 255]
, y <- [0 .. 255] ]))</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
[[File:XORimage-unicon-GR512.png|thumb|right|512x512 bit green and red]]
<langsyntaxhighlight Iconlang="icon">link printf
 
procedure main(A) #: XOR graphic
Line 643:
until Event() == &lpress # wait for left button to quit
close(&window)
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 649:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> require 'viewmat'
viewmat ~:"1/&.#: ~ i.256</langsyntaxhighlight>
 
=={{header|Java}}==
{{libheader|Swing}}
This example will repeat the pattern if you expand the window.
<langsyntaxhighlight lang="java">import java.awt.Color;
import java.awt.Graphics;
 
Line 685:
new XorPattern();
}
}</langsyntaxhighlight>
[[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:
<langsyntaxhighlight lang="sh">jq -n -r -f Munching_squares.jq > Munching_squares.svg</langsyntaxhighlight>
'''Part 1: Infrastructure'''
<langsyntaxhighlight lang="jq"># Convert the input integer to an array of bits with lsb first
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;</langsyntaxhighlight>
'''Part 2: SVG'''
<langsyntaxhighlight lang="jq">def rgb2rgb:
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)' />";</langsyntaxhighlight>
'''Part 3: xor pattern'''
<langsyntaxhighlight lang="jq"># rgb is a JSON object: { "red": _, "green": _, "blue": _}
 
def xor_pattern(width; height; rgb1; rgb2):
Line 745:
| range(0;height) as $y
| pixel($x; $y; $colours[ xor($x; $y) % $size] ) ) ),
"</svg>" ;</langsyntaxhighlight>
'''Part 4: Example'''
<langsyntaxhighlight lang="jq">def black: { "red": 0, "green": 0, "blue": 0};
def red: black + { "red": 255 };
def yellow: red + { "green": 255 };
 
xor_pattern(384; 384; red; yellow)</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Gtk, Cairo
 
const can = @GtkCanvas()
Line 773:
signal_connect(endit, win, :destroy)
wait(cond)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
import javax.swing.JFrame
Line 822:
}
}
}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
 
Line 857:
end
end sub
</syntaxhighlight>
</lang>
Image available at [[http://www.diga.me.uk/xorRC.gif]]
 
=={{header|Lua}}==
{{works with|LÖVE|11.0 or higher}}
<langsyntaxhighlight lang="lua">local clr = {}
function drawMSquares()
local points = {}
Line 895:
love.graphics.setColor(1,1,1)
love.graphics.draw(canvas)
end</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ListDensityPlot[
Table[Table[
FromDigits[BitXor[IntegerDigits[x, 2, 8], IntegerDigits[y, 2, 8]],
2], {x, 0, 255}], {y, 0, 255}]]</langsyntaxhighlight>
{{out|Output #1}}
[[File:xorpattern3.png|Mathematica output #1|200px]]
 
<langsyntaxhighlight Mathematicalang="mathematica">ArrayPlot[Array[BitXor, {511, 511}]]</langsyntaxhighlight>
{{out|Output #2}}
[[File:xorpattern4.png|Mathematica output #2|200px]]
 
=={{header|MATLAB}}==
<langsyntaxhighlight lang="matlab">size = 256;
[x,y] = meshgrid([0:size-1]);
 
Line 917:
colormap bone(size);
image(c);
axis equal;</langsyntaxhighlight>
{{out}} [[File:matlab_xor.png|MATLAB output|200px]]
 
=={{header|Microsoft Small Basic}}==
<langsyntaxhighlight lang="smallbasic">' Munching squares - smallbasic - 27/07/2018
size=256
GraphicsWindow.Width=size
Line 964:
x=t
EndFor
EndSub </langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight MiniScriptlang="miniscript">xor = function(a, b)
result = 0
bit = 1
Line 987:
gfx.setPixel x, y, color.rgb(0, xor(x,y), 0)
end for
end for</langsyntaxhighlight>
 
{{out}}
Line 994:
=={{header|Nim}}==
{{libheader|imageman}}
<langsyntaxhighlight Nimlang="nim">import random
import imageman
 
Line 1,011:
image[i, j] = colors[i xor j]
 
image.savePNG("munching_squares.png")</langsyntaxhighlight>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">open Graphics
 
let () =
Line 1,027:
done;
done;
ignore(read_key())</langsyntaxhighlight>
 
Run with:
Line 1,036:
 
=={{header|Octave}}==
<langsyntaxhighlight Octavelang="octave">size = 256;
[x,y] = meshgrid([0:size-1]);
 
Line 1,043:
colormap(jet(size));
image(c);
axis equal;</langsyntaxhighlight>
{{out}} [[File:Xor_pattern_octave.png|Octave output|320px]]
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use GD;
 
my $img = new GD::Image(256, 256, 1);
Line 1,058:
}
 
print $img->png</langsyntaxhighlight>
{{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].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">header("Content-Type: image/png");
 
$w = 256;
Line 1,136:
 
imagepng($im);
imagedestroy($im);</langsyntaxhighlight>
{{out}}
[[File:xor_pattern_php.png|PHP output|200px]]
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">munch: procedure options (main); /* 21 May 2014 */
 
declare screen (0:255, 0:255) bit(24) aligned;
Line 1,154:
end;
call writeppm(screen);
end munch;</langsyntaxhighlight>
 
=={{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.
 
<langsyntaxhighlight lang="java">
//Aamrun, 26th June 2022
 
Line 1,173:
 
updatePixels();
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Works with SWI-Prolog and his GUI XPCE.
<langsyntaxhighlight Prologlang="prolog">xor_pattern :-
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>
</lang>
[[File:Prolog_xor_pattern.png|200px]]
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">#palletteSize = 128
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</langsyntaxhighlight>
[[File:PureBasic_XOR_Pattern.png|Sample display of PureBasic solution|200px]]
 
=={{header|Python}}==
{{libheader|PIL}}
<langsyntaxhighlight Pythonlang="python">import Image, ImageDraw
 
image = Image.new("RGB", (256, 256))
Line 1,239:
 
del drawingTool
image.save("xorpic.png", "PNG")</langsyntaxhighlight>
[[File:PythonXORPic.png|Sample produced by the above code|200px]]
 
=={{header|QBasic}}==
<langsyntaxhighlight lang="qbasic">w = 254
 
SCREEN 13
Line 1,253:
PSET (x, y)
NEXT y
NEXT x</langsyntaxhighlight>
 
=={{header|Racket}}==
[[File:munching-racket.png|thumb|right]]
<langsyntaxhighlight lang="racket">
#lang racket
(require racket/draw)
Line 1,267:
(send dc set-pixel x y c))
bm
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,274:
Here's one simple way:
 
<syntaxhighlight lang="raku" perl6line>my $ppm = open("munching0.ppm", :w) orelse .die;
 
$ppm.print(q :to 'EOT');
Line 1,291:
 
$ppm.close();
</syntaxhighlight>
</lang>
 
Another way:
 
<syntaxhighlight lang="raku" perl6line>my @colors = map -> $r, $g, $b { Buf.new: $r, $g, $b },
map -> $x { floor ($x/256) ** 3 * 256 },
(flat (0...255) Z
Line 1,313:
$PPM.write: @colors[$_] for ^256 X+^ ^256;
 
$PPM.close;</langsyntaxhighlight>
[[File:perl_6_xor_pattern.png|Raku output|200px]]
 
=={{header|REXX}}==
{{trans|Burlesque}}
<langsyntaxhighlight lang="rexx">/*REXX program renders a graphical pattern by coloring each pixel with x XOR y */
/*───────────────────────────────────────── 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. */</langsyntaxhighlight>
Must be converted to an image with a separate program. <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Munching squares
 
Line 1,394:
label1 { setpicture(p1) show() }
return
</syntaxhighlight>
</lang>
Output:
 
Line 1,402:
Uses [[Raster graphics operations/Ruby]]
[[File:xorpattern_rb.png|thumb|right|Sample output from Ruby program]]
<langsyntaxhighlight lang="ruby">load 'raster_graphics.rb'
 
class Pixmap
Line 1,426:
 
img = Pixmap.xor_pattern(384, 384, RGBColour::RED, RGBColour::YELLOW)
img.save_as_png('xorpattern.png')</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">w = 100
graphic #g, w,w
for x = 0 to w
Line 1,439:
next x
render #g
#g "flush"</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">extern crate image;
 
use image::{ImageBuffer, Pixel, Rgb};
Line 1,457:
 
let _ = img.save("output.png");
}</langsyntaxhighlight>
 
=={{header|Scala}}==
===Scala Swing===
{{libheader|org.scala-lang.modules scala-swing}}
<langsyntaxhighlight Scalalang="scala">import scala.swing.Swing.pair2Dimension
import scala.swing.{Color, Graphics2D, MainFrame, Panel, SimpleSwingApplication}
 
Line 1,486:
centerOnScreen()
}
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('Imager')
 
var img = %O<Imager>.new(xsize => 256, ysize => 256)
Line 1,499:
}
 
img.write(file => 'xor.png')</langsyntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/munching-squares-sidef.png Munching squares]
 
=={{header|Tcl}}==
{{libheader|Tk}}<langsyntaxhighlight lang="tcl">package require 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]</langsyntaxhighlight>
 
=={{header|TI-83 BASIC}}==
Line 1,537:
 
 
<langsyntaxhighlight lang="ti-83b">PROGRAM:XORPATT
" •.-,+-°-1+o*:πOX"→Str1
 
Line 1,613:
End
Pause
</syntaxhighlight>
</lang>
 
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|2011}}
<langsyntaxhighlight lang="vbnet">' Munching squares - 27/07/2018
Public Class MunchingSquares
Const xsize = 256
Line 1,638:
End Sub 'Paint
 
End Class </langsyntaxhighlight>
{{out}}
[https://github.com/Pat-Garrett/RC/blob/7e9842513d361a5b4241bc6bb28f9985c2bfe161/Munching%20squares%20-%20vbnet.jpg Munching squares - vbnet]
Line 1,645:
{{trans|D}}
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
 
Line 1,666:
 
static draw(alpha) {}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
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
]</langsyntaxhighlight>
{{out}} [[File:MunchXPL0.png]]
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight Yabasiclang="yabasic">w = 256
open window w, w
Line 1,698:
dot x, y
Next
Next</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 1,704:
{{trans|XPL0}}
For a kaleidoscopic image, play with coolness.
<langsyntaxhighlight lang="zkl">fcn muncher{
bitmap:=PPM(256,256);
coolness:=(1).random(0x10000); // 55379, 18180, 40, 51950, 57619, 43514, 65465
Line 1,715:
}
bitmap.write(File("foo.ppm","wb"));
}();</langsyntaxhighlight>
For a cheap slide show (on Linux):
<langsyntaxhighlight lang="zkl">while(1){ muncher(); Atomic.sleep(3); }</langsyntaxhighlight>
run ImageViewer on foo.ppm and watch it [auto] update as the image changes.
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.