#include #include #define SIZE 256 #define SCALE ((double)SIZE / 1024.0) typedef struct { int x, y; } XY; static XY points[] = { {100, 900}, {100, 500}, {600, 300}, {900, 100}, {1000, 400}, {700, 600}, {900, 900}, {600, 1000}, {500, 900}, {100, 900}, {0, 0} }; /* Interpolate between two points. */ static double Interpolate(double a, double b, double t) { return a + (b - a) * t; } int main(int argc, char **argv) { unsigned char *image = calloc(SIZE * SIZE, 1); int x, y, p, t; double ft; if( image == NULL ) return printf("Out of memory\n"); /* Draw edges. */ for(p = 0; points[p + 1].x != 0; p += 3) { for(t = 0; t < SIZE * 4; t++) { ft = (double)t / (SIZE * 4.0); /* https://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm */ x = (int) Interpolate( Interpolate( Interpolate(points[p].x * SCALE, points[p+1].x * SCALE, ft), Interpolate(points[p+1].x * SCALE, points[p+2].x * SCALE, ft), ft), Interpolate( Interpolate(points[p+1].x * SCALE, points[p+2].x * SCALE, ft), Interpolate(points[p+2].x * SCALE, points[p+3].x * SCALE, ft), ft), ft); y = (int) Interpolate( Interpolate( Interpolate(points[p].y * SCALE, points[p+1].y * SCALE, ft), Interpolate(points[p+1].y * SCALE, points[p+2].y * SCALE, ft), ft), Interpolate( Interpolate(points[p+1].y * SCALE, points[p+2].y * SCALE, ft), Interpolate(points[p+2].y * SCALE, points[p+3].y * SCALE, ft), ft), ft); if( x >= 0 && x < SIZE && y >= 0 && y < SIZE ) image[y * SIZE + x] = 1; } } /* Output image as PBM. */ printf("P1\n%d %d\n", SIZE, SIZE); for(y = 0; y < SIZE; y++) { for(x = 0; x < SIZE; x++) fputs(image[y * SIZE + x] ? "0 " : "1 ", stdout); putchar('\n'); } return 0; }