Munching squares: Difference between revisions
Content added Content deleted
(+Java) |
(added C) |
||
Line 1: | Line 1: | ||
{{draft task|Raster graphics operations}}[[Category:Graphics algorithms]] |
{{draft task|Raster graphics operations}}[[Category:Graphics algorithms]] |
||
Render a graphical pattern where each pixel is colored by the value of 'x xor y' from a color table. |
Render a graphical pattern where each pixel is colored by the value of 'x xor y' from a color table. |
||
=={{header|C}}== |
|||
<lang c>#include <stdlib.h> |
|||
#include <stdio.h> |
|||
int main(void) |
|||
{ |
|||
const int width = 512, height = 512; |
|||
unsigned int i, x, y; |
|||
unsigned char color_table[256][3]; |
|||
for (i = 0; i < 256; ++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) & 0xFF], 1, 3, fp); |
|||
} |
|||
} |
|||
(void) fclose(fp); |
|||
return EXIT_SUCCESS; |
|||
}</lang> |
|||
[[Image:Xor_pattern_c.png|C output|200px]] |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
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; |
<lang java>import java.awt.Color; |
||
Line 35: | Line 65: | ||
}</lang> |
}</lang> |
||
[[Image:Xor pattern Java.png|200px]] |
[[Image:Xor pattern Java.png|200px]] |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
Revision as of 18:53, 28 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>
- include <stdio.h>
int main(void) {
const int width = 512, height = 512; unsigned int i, x, y; unsigned char color_table[256][3]; for (i = 0; i < 256; ++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) & 0xFF], 1, 3, fp); } } (void) fclose(fp); return EXIT_SUCCESS;
}</lang>
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(); }
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:
<lang Mathematica> ArrayPlot[Array[BitXor, {511, 511}]] </lang>
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: