worley-noise.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. double pA, pB;
  16. pA = p1x - p2x;
  17. pB = p1y - p2y;
  18. return sqrt(pA * pA + pB * pB);
  19. }
  20. void draw_pixel(Display * di, Window wi, GC gc, int x, int y, int color){
  21. XSetForeground(di, gc, color);
  22. XDrawPoint(di, wi, gc, x, y);
  23. }
  24. float rand_0_1(){
  25. int o0;
  26. float o0f, r;
  27. srand(SEED+seed_offset++); // define random initial seed
  28. o0 = rand();
  29. o0f = (float) o0;
  30. r = o0f / RAND_MAX;
  31. return (r);
  32. }
  33. double * sort(double * arr){
  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. 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. int x, y, i;
  53. int c;
  54. int noise_space[WIDTH][HEIGHT];
  55. double dist[POINTS], sdist[POINTS];
  56. struct point p[POINTS];
  57. for (x=0; x<WIDTH; x++){
  58. for (y=0;y<HEIGHT; y++){
  59. c = (int) (rand_0_1() * 255);
  60. c = (c << 16 ) | (c << 8) | c;
  61. noise_space[x][y] = c;
  62. }
  63. }
  64. for (i=0; i<POINTS; i++)
  65. {
  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. Display *di = XOpenDisplay(getenv("DISPLAY"));
  83. if (di == NULL){
  84. fprintf(stderr, "ERROR: No display");
  85. exit(EXIT_FAILURE);
  86. }
  87. int x=0, y=0, width=WIDTH, height=HEIGHT, border_width=1;
  88. int sc = DefaultScreen(di);
  89. Window ro = DefaultRootWindow(di);
  90. Window wi = XCreateSimpleWindow(di,
  91. ro,
  92. x,
  93. y,
  94. width,
  95. height,
  96. border_width,
  97. BlackPixel(di, sc),
  98. WhitePixel(di, sc));
  99. XMapWindow(di, wi);
  100. XStoreName(di, wi, "Default Window");
  101. GC gc = XCreateGC(di, ro, 0, NULL);
  102. XSelectInput(di, wi, KeyPressMask | ExposureMask);
  103. XEvent ev;
  104. int quit = 0;
  105. int z = 50;
  106. while (!quit){
  107. int a = XNextEvent(di, &ev);
  108. if (ev.type == KeyPress) quit == 1;
  109. if (ev.type == Expose) {
  110. worley_noise(di, wi, gc);
  111. }
  112. }
  113. XFreeGC(di, gc);
  114. XDestroyWindow(di, wi);
  115. XCloseDisplay(di);
  116. return (EXIT_SUCCESS);
  117. }