Munching squares: Difference between revisions

added C
(+Java)
(added C)
Line 1:
{{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.
 
=={{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}}==
 
This example will repeat the pattern if you expand the window.
<lang java>import java.awt.Color;
Line 35 ⟶ 65:
}</lang>
[[Image:Xor pattern Java.png|200px]]
 
=={{header|Mathematica}}==