worley_noise.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 200
  9. #define POINTS 15
  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. return drand48();
  30. }
  31. void sort(double *arr)
  32. {
  33. int arr_size = POINTS;
  34. int i, j, aux;
  35. for (i = 0; i < arr_size; i++) {
  36. for (j = i + 1; j < arr_size; j++) {
  37. if (arr[i] > arr[j]) {
  38. aux = arr[j];
  39. arr[j] = arr[i];
  40. arr[i] = aux;
  41. }
  42. }
  43. }
  44. }
  45. int map(int value, int min, int max, int map_min, int map_max)
  46. {
  47. double R = (double) (map_max - map_min) / (double) (max - min);
  48. double y = map_min + (value * R) + R;
  49. return (int) y;
  50. }
  51. void worley_noise(Display * di, Window wi, GC gc)
  52. {
  53. int x, y, i;
  54. int c;
  55. int noise_space[WIDTH][HEIGHT];
  56. double dist[POINTS], sdist[POINTS];
  57. struct point p[POINTS];
  58. for (i = 0; i < POINTS; i++) {
  59. p[i].x = (int) (rand_0_1() * WIDTH);
  60. p[i].y = (int) (rand_0_1() * HEIGHT);
  61. }
  62. for (x = 0; x < WIDTH; x++) {
  63. for (y = 0; y < HEIGHT; y++) {
  64. for (i = 0; i < POINTS; i++) {
  65. dist[i] = dist_p1_p2(x, y, p[i].x, p[i].y);
  66. sort(dist);
  67. }
  68. int d1 = map(dist[0], 0, WIDTH / 2, 200, 0);
  69. int d2 = map(dist[1], 0, WIDTH / 2, 200, 0);
  70. int d3 = map(dist[2], 0, WIDTH / 2, 200, 0);
  71. /* 0x000000 rgb value where
  72. * 16 r
  73. * 8 g
  74. * 4 b*/
  75. c = (d1 << 16) + (d2 << 8) + d3;
  76. noise_space[x][y] = c;
  77. }
  78. }
  79. for (x = 0; x < WIDTH; x++) {
  80. for (y = 0; y < HEIGHT; y++) {
  81. draw_pixel(di, wi, gc, x, y, noise_space[x][y]);
  82. }
  83. }
  84. }
  85. void redraw(Display * di, Window wi, GC gc)
  86. {
  87. XClearWindow(di, wi);
  88. worley_noise(di, wi, gc);
  89. XFlush(di);
  90. }
  91. int main()
  92. {
  93. Display *di = XOpenDisplay(getenv("DISPLAY"));
  94. if (di == NULL) {
  95. fprintf(stderr, "ERROR: No display");
  96. exit(EXIT_FAILURE);
  97. }
  98. int x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1;
  99. int sc = DefaultScreen(di);
  100. Window ro = DefaultRootWindow(di);
  101. Window wi = XCreateSimpleWindow(di,
  102. ro,
  103. x,
  104. y,
  105. width,
  106. height,
  107. border_width,
  108. BlackPixel(di, sc),
  109. WhitePixel(di, sc));
  110. XMapWindow(di, wi);
  111. XStoreName(di, wi, "Default Window");
  112. GC gc = XCreateGC(di, ro, 0, NULL);
  113. XSelectInput(di, wi, KeyPressMask | ExposureMask);
  114. XEvent ev;
  115. int quit = 0;
  116. while (!quit) {
  117. if (XPending(di) > 0) {
  118. int a = XNextEvent(di, &ev);
  119. if (ev.type == KeyPress) {
  120. printf("KeyPress %x\n", ev.xkey.keycode);
  121. if (ev.xkey.keycode == 0x18)
  122. quit = 1;
  123. }
  124. if (ev.type == Expose) {
  125. redraw(di, wi, gc);
  126. }
  127. }
  128. redraw(di, wi, gc);
  129. }
  130. XFreeGC(di, gc);
  131. XDestroyWindow(di, wi);
  132. XCloseDisplay(di);
  133. return (EXIT_SUCCESS);
  134. }