Skip to content

Instantly share code, notes, and snippets.

@alwynallan
Created January 4, 2025 20:13
Show Gist options
  • Save alwynallan/23754b33df67efa5a4dee43821978dd2 to your computer and use it in GitHub Desktop.
Save alwynallan/23754b33df67efa5a4dee43821978dd2 to your computer and use it in GitHub Desktop.
Takes binary data in 1-byte to 8-byte words and produces a .SVG image of 100K lines where the position on the circumference is the value of each word. Most graphics software balks and the extreme number of transparent objects, but Chrome and Illustrator can handle it, and Inkscape tries.
// bin2svg.c
// $ gcc -Wall -O3 bin2svg.c -o bin2svg -lm
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <math.h>
#define BDR 50
#define WH 1000
#define CC ((WH)/2)
#define WORDS 100000
#define COLOR 0
int main(int argc, char * argv[]) {
#if(COLOR==1)
//char * colors[] = {"cyan", "magenta", "yellow"};
//char * colors[] = {"red", "green", "blue"};
char * colors[] = {"darkorange", "navy"};
const int n_colors = sizeof(colors) / sizeof(colors[0]);
#endif
int words=0, wordlen = 8;
uint64_t val=0UL;
double x, y, px, py, max2pi;
if(argc==2) wordlen = atoi(argv[1]);
assert(1 <= wordlen && wordlen <= 8);
max2pi = pow(2., wordlen*8-1) / M_PI;
printf("<svg height=\"%d\" width=\"%d\" xmlns=\"http://www.w3.org/2000/svg\" style=\"background-color:#FFF;stroke:black;stroke-width:1;stroke-opacity:0.01;zoom:1\">\n", WH+2*BDR, WH+2*BDR);
//printf("<rect fill=\"#FFF\" width=\"%d\" height=\"%d\">\n", WH+2*BDR, WH+2*BDR);
while(1 == fread(&val, wordlen, 1, stdin) && words < WORDS) {
x = CC*sin((double)val / max2pi)+CC+BDR;
y = CC*cos((double)val / max2pi)+CC+BDR;
if(words) {
#if(COLOR==1)
printf("<line x1=\"%.1lf\" y1=\"%.1lf\" x2=\"%.1lf\" y2=\"%.1lf\" style=\"stroke:%s;\" />\n", px, py, x, y, colors[words%n_colors]);
#elif(COLOR==2)
float hue = (atan2f(y-py, x-px) / M_PI + 1.) * 360.;
printf("<line x1=\"%.1lf\" y1=\"%.1lf\" x2=\"%.1lf\" y2=\"%.1lf\" style=\"stroke:hsl(%.1f 100 20);\" />\n", px, py, x, y, hue);
#elif(COLOR==3)
float hue = (y-py) / (x-px);
if(hue < 0.)
printf("<line x1=\"%.1lf\" y1=\"%.1lf\" x2=\"%.1lf\" y2=\"%.1lf\" style=\"stroke:red;\" />\n", px, py, x, y);
else
printf("<line x1=\"%.1lf\" y1=\"%.1lf\" x2=\"%.1lf\" y2=\"%.1lf\" style=\"stroke:green;\" />\n", px, py, x, y);
#else
printf("<line x1=\"%.1lf\" y1=\"%.1lf\" x2=\"%.1lf\" y2=\"%.1lf\" />\n", px, py, x, y);
#endif
}
px = x;
py = y;
words++;
}
printf("</svg>\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment