worley_noise.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. XClearWindow(di, wi);
  87. worley_noise(di, wi, gc);
  88. XFlush(di);
  89. }
  90. int main()
  91. {
  92. Display *di = XOpenDisplay(getenv("DISPLAY"));
  93. if (di == NULL) {
  94. fprintf(stderr, "ERROR: No display");
  95. exit(EXIT_FAILURE);
  96. }
  97. int x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1;
  98. int sc = DefaultScreen(di);
  99. Window ro = DefaultRootWindow(di);
  100. Window wi = XCreateSimpleWindow(di,
  101. ro,
  102. x,
  103. y,
  104. width,
  105. height,
  106. border_width,
  107. BlackPixel(di, sc),
  108. WhitePixel(di, sc));
  109. XMapWindow(di, wi);
  110. XStoreName(di, wi, "Default Window");
  111. GC gc = XCreateGC(di, ro, 0, NULL);
  112. XSelectInput(di, wi, KeyPressMask | ExposureMask);
  113. XEvent ev;
  114. int quit = 0;
  115. while (!quit) {
  116. if (XPending(di) > 0) {
  117. int a = XNextEvent(di, &ev);
  118. if (ev.type == KeyPress) {
  119. printf("KeyPress %x\n", ev.xkey.keycode);
  120. if (ev.xkey.keycode == 0x18)
  121. quit = 1;
  122. }
  123. if (ev.type == Expose) {
  124. redraw(di, wi, gc);
  125. }
  126. }
  127. redraw(di, wi, gc);
  128. }
  129. XFreeGC(di, gc);
  130. XDestroyWindow(di, wi);
  131. XCloseDisplay(di);
  132. return (EXIT_SUCCESS);
  133. }