Skip to content

Instantly share code, notes, and snippets.

@louisswarren

louisswarren/Makefile

Last active Aug 5, 2020
Embed
What would you like to do?
Pipe pattern drawing
.PHONY: display
display: out.png
feh -Z $^
out.png: out.pbm Makefile
convert $< $@
out.pbm: pipes
./$^ > $@
pipes: pipes.c
gcc -std=c99 $< -o $@
.PHONY: clean
clean:
rm -r out.png out.pbm pipes
#include <stdio.h>
#include <stdlib.h>
#define G_WIDTH 40
#define G_HEIGHT 40
int
main(void)
{
int i, j, a, b;
int r;
char (*hor)[G_WIDTH] = malloc(sizeof(*hor) * (G_HEIGHT + 1));
char (*ver)[G_WIDTH + 1] = malloc(sizeof(*ver) * G_HEIGHT);
char (*img)[G_WIDTH * 4] = calloc(G_HEIGHT * 4, sizeof(*img));
srand(2020);
for (i = 0; i < G_WIDTH * (G_HEIGHT + 1); ++i)
hor[0][i] = rand() % 100 < 45;
for (i = 0; i < (G_WIDTH + 1) * G_HEIGHT; ++i)
ver[0][i] = rand() % 100 < 45;
printf("P1\n");
printf("%d %d\n", G_WIDTH * 4, G_HEIGHT * 4);
for (i = 0; i < G_HEIGHT; ++i) {
for (j = 0; j < G_WIDTH; ++j) {
img[4*i+0][4*j+0] = 0 + (hor[i][j] || ver[i][j]);
img[4*i+0][4*j+1] = 0 + (hor[i][j]);
img[4*i+0][4*j+2] = 0 + (hor[i][j]);
img[4*i+0][4*j+3] = 0 + (hor[i][j] || ver[i][j + 1]);
img[4*i+1][4*j+0] = 0 + (ver[i][j]);
img[4*i+1][4*j+3] = 0 + (ver[i][j + 1]);
img[4*i+2][4*j+0] = 0 + (ver[i][j]);
img[4*i+2][4*j+3] = 0 + (ver[i][j + 1]);
img[4*i+3][4*j+0] = 0 + (hor[i + 1][j] || ver[i][j]);
img[4*i+3][4*j+1] = 0 + (hor[i + 1][j]);
img[4*i+3][4*j+2] = 0 + (hor[i + 1][j]);
img[4*i+3][4*j+3] = 0 + (hor[i + 1][j] || ver[i][j + 1]);
}
}
/* Postprocess to add missing corners */
for (i = 1; i < G_HEIGHT; ++i) {
for (j = 1; j < G_WIDTH; ++j) {
if (!!img[4*i-1][4*j-1]
+ !!img[4*i][4*j-1]
+ !!img[4*i-1][4*j]
+ !!img[4*i][4*j] == 3) {
img[4*i-1][4*j-1] = 1;
img[4*i][4*j-1] = 1;
img[4*i-1][4*j] = 1;
img[4*i][4*j] = 1;
}
}
}
for (i = 0; i < G_HEIGHT * 4; ++i) {
for (j = 0; j < G_WIDTH * 4; ++j)
printf("%d", img[i][j]);
putchar('\n');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.