Skip to content

Instantly share code, notes, and snippets.

@robertmaxwilliams
Created February 9, 2021 00:05
Show Gist options
  • Save robertmaxwilliams/932667cb672e9a52ff8cd3d63195ed68 to your computer and use it in GitHub Desktop.
Save robertmaxwilliams/932667cb672e9a52ff8cd3d63195ed68 to your computer and use it in GitHub Desktop.
// Pipe in random bytes, and it outputs alternting x and y ints of where to draw a low opacity point.
// Call from command line with an integer specifiying the output scale
#include "stdio.h"
#include <stdlib.h>
#include <stdint.h>
typedef struct {
float x;
float y;
} pair_t;
int frown(float x) {
int x_int = (int) x;
if (x - x_int >= 0.5)
return x_int + 1;
return x_int;
}
void draw(int x, int y) {
fwrite((void*)&x, 1, sizeof(int), stdout);
fwrite((void*)&y, 1, sizeof(int), stdout);
}
void drawf(float x, float y) {
draw(frown(x), frown(y));
}
float better_random() {
uint32_t x;
fgets((void*) &x, sizeof(uint32_t), stdin);
return ((float) x) / ((float) UINT32_MAX);
}
int randint(int max) {
// If max is not a power of 2, this may not be uniform
uint32_t x;
fgets((void*) &x, sizeof(uint32_t), stdin);
return (int) (x % max);
}
// This section makes the rules of the game
pair_t pent[5] = {{0.5, 0.0}, {0.9755282581475768, 0.3454915028125263},
{0.7938926261462367, 0.9045084971874737},
{0.2061073738537635, 0.9045084971874737},
{0.02447174185242318, 0.3454915028125264}};
pair_t hex[6] = {{0.5, 0.0},
{0.9330127018922193, 0.25},
{0.9330127018922194, 0.7499999999999999},
{0.5, 1.0},
{0.06698729810778076, 0.7500000000000002},
{0.0669872981077807, 0.24999999999999994}};
int number_corners = 6;
pair_t* corns = hex;
int random_corner(int corner) {
int corner_add = randint(number_corners - 1);
//if (corner_add == 2) corner_add = 2;
return (corner + corner_add) % number_corners;
}
pair_t make_pair(float x, float y) {
pair_t pair = {.x = x, .y = y};
return pair;
}
pair_t corner_coords(int corner) {
return corns[corner];
//float corner_x = corner % 2;
//float corner_y = corner / 2;
//return (pair_t) {corner_x, corner_y};
/*
switch (corner) {
case 0:
return (pair_t) {0, 0.5};
case 1:
return (pair_t) {0.5, 0};
case 2:
return (pair_t) {1, 0.5};
}
return (pair_t) {0.5, 1};
*/
}
int main(int argc, char** argv) {
if (argc != 2) {
printf("Needs int argument for size in pixels");
}
int size = atoi(argv[1]);
while (1) {
float x = better_random();
float y = better_random();
int corner = randint(number_corners);
for (int i = 0; i < 30; i++) {
corner = random_corner(corner);
pair_t corner_xy = corner_coords(corner);
x += (corner_xy.x - x)/2;
y += (corner_xy.y - y)/2;
//draw(x * size, y * size);
}
draw(x * size, y * size);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment