worley-noise.c 2.7 KB

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