worley-noise.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #define _XOPEN_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <X11/Xlib.h>
  5. #include <math.h>
  6. #define WIDTH 1280
  7. #define HEIGHT 720
  8. #define SEED 1234567
  9. #define POINTS 20
  10. static int seed_offset = 1;
  11. struct point {
  12. int x;
  13. int y;
  14. };
  15. double dist_p1_p2(int p1x, int p1y, int p2x, int p2y)
  16. {
  17. double pA, pB;
  18. pA = p1x - p2x;
  19. pB = p1y - p2y;
  20. return sqrt(pA * pA + pB * pB);
  21. }
  22. void draw_pixel(Display * di, Window wi, GC gc, int x, int y, int color)
  23. {
  24. XSetForeground(di, gc, color);
  25. XDrawPoint(di, wi, gc, x, y);
  26. }
  27. float rand_0_1()
  28. {
  29. srand48(SEED + seed_offset++);
  30. return drand48();
  31. }
  32. void sort(double *arr)
  33. {
  34. int arr_size = POINTS;
  35. int i, j, aux;
  36. for (i = 0; i < arr_size; i++) {
  37. for (j = i + 1; j < arr_size; j++) {
  38. if (arr[i] > arr[j]) {
  39. aux = arr[j];
  40. arr[j] = arr[i];
  41. arr[i] = aux;
  42. }
  43. }
  44. }
  45. }
  46. int map(int value, int min, int max, int map_min, int map_max)
  47. {
  48. double R = (double) (map_max - map_min) / (double) (max - min);
  49. double y = map_min + (value * R) + R;
  50. return (int) y;
  51. }
  52. void worley_noise(Display * di, Window wi, GC gc)
  53. {
  54. int x, y, i;
  55. int c;
  56. int noise_space[WIDTH][HEIGHT];
  57. double dist[POINTS], sdist[POINTS];
  58. struct point p[POINTS];
  59. for (x = 0; x < WIDTH; x++) {
  60. for (y = 0; y < HEIGHT; y++) {
  61. c = (int) (rand_0_1() * 255);
  62. c = (c << 16) | (c << 8) | c;
  63. noise_space[x][y] = c;
  64. }
  65. }
  66. for (i = 0; i < POINTS; i++) {
  67. p[i].x = (int) (rand_0_1() * WIDTH);
  68. p[i].y = (int) (rand_0_1() * HEIGHT);
  69. }
  70. for (x = 0; x < WIDTH; x++) {
  71. for (y = 0; y < HEIGHT; y++) {
  72. for (i = 0; i < POINTS; i++) {
  73. dist[i] = dist_p1_p2(x, y, p[i].x, p[i].y);
  74. sort(dist);
  75. }
  76. c = map(dist[0], 0, WIDTH / 4, 255, 20);
  77. c = (c << 16) | (c << 8) | c;
  78. draw_pixel(di, wi, gc, x, y, c);
  79. }
  80. }
  81. }
  82. int main()
  83. {
  84. Display *di = XOpenDisplay(getenv("DISPLAY"));
  85. if (di == NULL) {
  86. fprintf(stderr, "ERROR: No display");
  87. exit(EXIT_FAILURE);
  88. }
  89. int x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1;
  90. int sc = DefaultScreen(di);
  91. Window ro = DefaultRootWindow(di);
  92. Window wi = XCreateSimpleWindow(di,
  93. ro,
  94. x,
  95. y,
  96. width,
  97. height,
  98. border_width,
  99. BlackPixel(di, sc),
  100. WhitePixel(di, sc));
  101. XMapWindow(di, wi);
  102. XStoreName(di, wi, "Default Window");
  103. GC gc = XCreateGC(di, ro, 0, NULL);
  104. XSelectInput(di, wi, KeyPressMask | ExposureMask);
  105. XEvent ev;
  106. int quit = 0;
  107. int z = 50;
  108. while (!quit) {
  109. int a = XNextEvent(di, &ev);
  110. if (ev.type == KeyPress) {
  111. printf("KeyPress %x\n", ev.xkey.keycode);
  112. quit = 1;
  113. }
  114. if (ev.type == Expose) {
  115. worley_noise(di, wi, gc);
  116. }
  117. }
  118. XFreeGC(di, gc);
  119. XDestroyWindow(di, wi);
  120. XCloseDisplay(di);
  121. return (EXIT_SUCCESS);
  122. }