Munching squares: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 51: Line 51:
=={{header|Gnuplot}}==
=={{header|Gnuplot}}==


<lang gnuplot>set pm3d
<lang gnuplot>set pm3d map
set size square
set size square
set isosamples 255,255
set isosamples 255,255

Revision as of 10:54, 30 November 2011

Munching squares is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Render a graphical pattern where each pixel is colored by the value of 'x xor y' from a color table.

C

<lang c>#include <stdlib.h>

  1. include <stdio.h>

int main(void) {

 const int width = 512, height = 512;
 const unsigned int color_table_size = 256;
 unsigned char color_table[color_table_size][3];
 unsigned int i, x, y;
 for (i = 0; i < color_table_size; ++i) {
   color_table[i][0] = 255 - i;  /* red */
   color_table[i][1] = i / 2;    /* green */
   color_table[i][2] = i;        /* blue */
 }
 FILE *fp = fopen("xor_pattern.ppm", "wb");
 (void) fprintf(fp, "P6\n%d %d\n255\n", width, height);
 for (y = 0; y < height; ++y) {
   for (x = 0; x < width; ++x) {
     (void) fwrite(color_table[(x ^ y) % color_table_size], 1, 3, fp);
   }
 }
 (void) fclose(fp);
 return EXIT_SUCCESS;

}</lang>

C output

D

<lang d>import std.stdio;

void main() {

   enum width = 512, height = 512;
   auto f = File("xor_pattern.ppm", "wb");
   f.writefln("P6\n%d %d\n255", width, height);
   foreach (y; 0 .. height)
       foreach (x; 0 .. width) {
           ubyte c = (x ^ y) & ubyte.max;
           ubyte[3] rgb = [255 - c, c / 2, c];
           f.rawWrite(rgb);
       }

}</lang>

D output

Gnuplot

<lang gnuplot>set pm3d map set size square set isosamples 255,255 splot [0:255][0:255]-(floor(x)^floor(y))</lang>

D output

Java

This example will repeat the pattern if you expand the window. <lang java>import java.awt.Color; import java.awt.Graphics;

import javax.swing.JFrame; import javax.swing.JPanel;

public class XorPattern extends JFrame{

   private JPanel xorPanel;
   public XorPattern(){
       xorPanel = new JPanel(){
           @Override
           public void paint(Graphics g) {
               for(int y = 0; y < XorPattern.this.xorPanel.getHeight();y++){
                   for(int x = 0; x < XorPattern.this.xorPanel.getWidth();x++){
                       g.setColor(new Color(0, (x ^ y) % 256, 0));
                       g.drawLine(x, y, x, y);
                   }
               }
           }
       };
       add(xorPanel);
       setSize(300, 300);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
   }
   public static void main(String[] args){
       new XorPattern();
   }

}</lang>

Mathematica

<lang Mathematica>ListDensityPlot[

Table[Table[
  FromDigits[BitXor[IntegerDigits[x, 2, 8], IntegerDigits[y, 2, 8]], 
   2], {x, 0, 255}], {y, 0, 255}]]</lang>

Output #1:

Mathematica output #1

<lang Mathematica>ArrayPlot[Array[BitXor, {511, 511}]]</lang> Output #2:

Mathematica output #2

OCaml

<lang ocaml>open Graphics

let () =

 open_graph "";
 resize_window 256 256;
 for y = 0 to pred (size_y()) do
   for x = 0 to pred (size_x()) do
     let v = (x lxor y) land 0xFF in
     set_color (rgb v (255 - v) 0);
     plot x y
   done;
 done;
 ignore(read_key())</lang>

Run with:

$ ocaml graphics.cma xor_pattern.ml

Output:

OCaml output

Perl

<lang perl>use GD;

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

for my $y(0..255) {

       for my $x(0..255) {
               my $color = $img->colorAllocate( abs(255 - $x - $y),  (255-$x) ^ $y , $x ^ (255-$y));
               $img->setPixel($x, $y, $color);
       }

}

print $img->png</lang> Perl output

PHP

<lang php>header("Content-Type: image/png");

$w = 256; $h = 256;

$im = imagecreate($w, $h)

   or die("Cannot Initialize new GD image stream");

$color = array(); for($i=0;$i<256;$i++) {

       array_push($color,imagecolorallocate($im,sin(($i)*(2*3.14/256))*128+128,$i/2,$i));

}

for($i=0;$i<$w;$i++) {

       for($j=0;$j<$h;$j++)
       {
               imagesetpixel($im,$i,$j,$color[$i^$j]);
       }

}

imagepng($im); imagedestroy($im);</lang>

PHP output

Tcl

Library: Tk

<lang tcl>package require Tk

proc xorImage {img table} {

   set data {}
   set h [image height $img]
   set w [image width $img]
   for {set y 0} {$y < $h} {incr y} {

set row {} for {set x 0} {$x < $w} {incr x} { lappend row [lindex $table [expr {($x^$y) % [llength $table]}]] } lappend data $row

   }
   $img put $data

} proc inRange {i f t} {expr {$f + ($t-$f)*$i/255}} proc mkTable {rf rt gf gt bf bt} {

   for {set i 0} {$i < 256} {incr i} {

lappend tbl [format "#%02x%02x%02x" \ [inRange $i $rf $rt] [inRange $i $gf $gt] [inRange $i $bf $bt]]

   }
   return $tbl

}

set img [image create photo -width 512 -height 512] xorImage $img [mkTable 0 255 64 192 255 0] pack [label .l -image $img]</lang>