#define _XOPEN_SOURCE #include #include #include #include #define WIDTH 1280 #define HEIGHT 720 #define SEED 200 #define POINTS 15 static int seed_offset = 1; struct point { int x; int y; }; double dist_p1_p2(int p1x, int p1y, int p2x, int p2y) { double pA, pB; pA = p1x - p2x; pB = p1y - p2y; return sqrt(pA * pA + pB * pB); } void draw_pixel(Display * di, Window wi, GC gc, int x, int y, int color) { XSetForeground(di, gc, color); XDrawPoint(di, wi, gc, x, y); } float rand_0_1() { return drand48(); } void sort(double *arr) { int arr_size = POINTS; int i, j, aux; for (i = 0; i < arr_size; i++) { for (j = i + 1; j < arr_size; j++) { if (arr[i] > arr[j]) { aux = arr[j]; arr[j] = arr[i]; arr[i] = aux; } } } } int map(int value, int min, int max, int map_min, int map_max) { double R = (double) (map_max - map_min) / (double) (max - min); double y = map_min + (value * R) + R; return (int) y; } void worley_noise(Display * di, Window wi, GC gc) { int x, y, i; int c; int noise_space[WIDTH][HEIGHT]; double dist[POINTS], sdist[POINTS]; struct point p[POINTS]; for (i = 0; i < POINTS; i++) { p[i].x = (int) (rand_0_1() * WIDTH); p[i].y = (int) (rand_0_1() * HEIGHT); } for (x = 0; x < WIDTH; x++) { for (y = 0; y < HEIGHT; y++) { for (i = 0; i < POINTS; i++) { dist[i] = dist_p1_p2(x, y, p[i].x, p[i].y); sort(dist); } int d1 = map(dist[0], 0, WIDTH / 2, 200, 0); int d2 = map(dist[1], 0, WIDTH / 2, 200, 0); int d3 = map(dist[2], 0, WIDTH / 2, 200, 0); /* 0x000000 rgb value where * 16 r * 8 g * 4 b*/ c = (d1 << 16) + (d2 << 8) + d3; noise_space[x][y] = c; } } for (x = 0; x < WIDTH; x++) { for (y = 0; y < HEIGHT; y++) { draw_pixel(di, wi, gc, x, y, noise_space[x][y]); } } } void redraw(Display * di, Window wi, GC gc) { XClearWindow(di, wi); worley_noise(di, wi, gc); XFlush(di); } int main() { Display *di = XOpenDisplay(getenv("DISPLAY")); if (di == NULL) { fprintf(stderr, "ERROR: No display"); exit(EXIT_FAILURE); } int x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1; int sc = DefaultScreen(di); Window ro = DefaultRootWindow(di); Window wi = XCreateSimpleWindow(di, ro, x, y, width, height, border_width, BlackPixel(di, sc), WhitePixel(di, sc)); XMapWindow(di, wi); XStoreName(di, wi, "Default Window"); GC gc = XCreateGC(di, ro, 0, NULL); XSelectInput(di, wi, KeyPressMask | ExposureMask); XEvent ev; int quit = 0; while (!quit) { if (XPending(di) > 0) { int a = XNextEvent(di, &ev); if (ev.type == KeyPress) { printf("KeyPress %x\n", ev.xkey.keycode); if (ev.xkey.keycode == 0x18) quit = 1; } if (ev.type == Expose) { redraw(di, wi, gc); } } redraw(di, wi, gc); } XFreeGC(di, gc); XDestroyWindow(di, wi); XCloseDisplay(di); return (EXIT_SUCCESS); }