Munching squares: Difference between revisions

→‎{{header|C}}: better colors
No edit summary
(→‎{{header|C}}: better colors)
Line 6:
<lang c>#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
 
void hue_to_rgb(double hue, double sat, unsigned char *p)
{
double x;
int c = 255 * sat;
hue /= 60;
x = (1 - fabs(fmod(hue, 2) - 1)) * 255;
 
switch((int)hue) {
case 0: p[0] = c; p[1] = x; p[2] = 0; return;
case 1: p[0] = x; p[1] = c; p[2] = 0; return;
case 2: p[0] = 0; p[1] = c; p[2] = x; return;
case 3: p[0] = 0; p[1] = x; p[2] = c; return;
case 4: p[0] = x; p[1] = 0; p[2] = c; return;
case 5: p[0] = c; p[1] = 0; p[2] = x; return;
}
}
 
int main(void)
{
const int width = 512, heightsize = 512;
int i, j;
const unsigned int color_table_size = 256;
unsigned char color_table[color_table_size][*colors = malloc(size * 3]);
unsigned char *pix = malloc(size * size * 3), *p;
unsigned int i, x, y;
FILE *fp;
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>
 
for (i = 0; i < color_table_sizesize; i++i) {
hue_to_rgb(i * 240. / size, i * 1. / size, colors + 3 * i);
 
for (i = 0, p = pix; i < size; i++)
for (yj = 0; yj < heightsize; j++y), {p += 3)
memcpy(p, colors + (i ^ j) * 3, 3);
 
FILE * fp = fopen("xor_patternxor.ppm", "wb");
(void) fprintf(fp, "P6\n%d %d\n255\n", widthsize, heightsize);
fwrite(pix, size * size * 3, 1, fp);
(void) fclose(fp);
 
return 0;
}</lang>
[[Image:Xor_pattern_c.png|C output|200px]]
 
Anonymous user