worley-noise.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 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. 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 (x = 0; x < WIDTH; x++) {
  59. for (y = 0; y < HEIGHT; y++) {
  60. c = (int) (rand_0_1() * 255);
  61. c = (c << 16) | (c << 8) | c;
  62. noise_space[x][y] = c;
  63. }
  64. }
  65. for (i = 0; i < POINTS; i++) {
  66. p[i].x = (int) (rand_0_1() * WIDTH);
  67. p[i].y = (int) (rand_0_1() * HEIGHT);
  68. }
  69. for (x = 0; x < WIDTH; x++) {
  70. for (y = 0; y < HEIGHT; y++) {
  71. for (i = 0; i < POINTS; i++) {
  72. dist[i] = dist_p1_p2(x, y, p[i].x, p[i].y);
  73. sort(dist);
  74. }
  75. c = map(dist[0], 0, WIDTH / 4, 255, 20);
  76. c = (c << 16) | (c << 8) | c;
  77. draw_pixel(di, wi, gc, x, y, c);
  78. }
  79. }
  80. }
  81. int main()
  82. {
  83. Display *di = XOpenDisplay(getenv("DISPLAY"));
  84. if (di == NULL) {
  85. fprintf(stderr, "ERROR: No display");
  86. exit(EXIT_FAILURE);
  87. }
  88. int x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1;
  89. int sc = DefaultScreen(di);
  90. Window ro = DefaultRootWindow(di);
  91. Window wi = XCreateSimpleWindow(di,
  92. ro,
  93. x,
  94. y,
  95. width,
  96. height,
  97. border_width,
  98. BlackPixel(di, sc),
  99. WhitePixel(di, sc));
  100. XMapWindow(di, wi);
  101. XStoreName(di, wi, "Default Window");
  102. GC gc = XCreateGC(di, ro, 0, NULL);
  103. XSelectInput(di, wi, KeyPressMask | ExposureMask);
  104. XEvent ev;
  105. int quit = 0;
  106. int z = 50;
  107. while (!quit) {
  108. int a = XNextEvent(di, &ev);
  109. if (ev.type == KeyPress) {
  110. printf("KeyPress %x\n", ev.xkey.keycode);
  111. quit = 1;
  112. }
  113. if (ev.type == Expose) {
  114. worley_noise(di, wi, gc);
  115. }
  116. }
  117. XFreeGC(di, gc);
  118. XDestroyWindow(di, wi);
  119. XCloseDisplay(di);
  120. return (EXIT_SUCCESS);
  121. }