123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/Xlib.h>
- #include <math.h>
- #define WIDTH 1280
- #define HEIGHT 720
- #define SEED 8081
- #define POINTS 20
- static int seed_offset = 1;
- struct point {
- int x;
- int y;
- };
- double dist_p1_p2(int p1x, int p1y, int p2x, int p2y){
- double pA, pB;
- pA = p1x - p2x;
- pB = p1y - p2y;
- return sqrt(pA * pA + pB * pB);
- }
- void draw_pixel(Display * di, Window wi, GC gc, int x, int y, int color){
- XSetForeground(di, gc, color);
- XDrawPoint(di, wi, gc, x, y);
- }
- float rand_0_1(){
- int o0;
- float o0f, r;
- srand(SEED+seed_offset++); // define random initial seed
- o0 = rand();
- o0f = (float) o0;
- r = o0f / RAND_MAX;
- return (r);
- }
- double * sort(double * arr){
- int arr_size = POINTS;
- int i,j, aux;
- for (i=0; i<arr_size;i++){
- for (j=i+1;j<arr_size;j++){
- if (arr[i] > arr[j]){
- aux = arr[j];
- arr[j] = arr[i];
- arr[i] = aux;
- }
- }
- }
-
- }
- int map (int value, int min, int max, int map_min, int map_max){
- double R = (double) (map_max - map_min) / (double) (max - min);
- double y = map_min + (value * R) + R;
- return (int) y;
- }
- void worley_noise(Display * di, Window wi, GC gc){
- int x, y, i;
- int c;
- int noise_space[WIDTH][HEIGHT];
- double dist[POINTS], sdist[POINTS];
- struct point p[POINTS];
- for (x=0; x<WIDTH; x++){
- for (y=0;y<HEIGHT; y++){
- c = (int) (rand_0_1() * 255);
- c = (c << 16 ) | (c << 8) | c;
- noise_space[x][y] = c;
- }
- }
-
- for (i=0; i<POINTS; i++)
- {
- p[i].x = (int) (rand_0_1() * WIDTH);
- p[i].y = (int) (rand_0_1() * HEIGHT);
- }
- for (x=0; x<WIDTH; x++){
- for (y=0;y<HEIGHT; y++){
- for (i=0;i<POINTS;i++){
- dist[i] = dist_p1_p2(x, y, p[i].x, p[i].y);
- sort(dist);
- }
- c = map(dist[0], 0, WIDTH/4, 255, 20);
- c = (c << 16 ) | (c << 8) | c;
- draw_pixel(di, wi, gc, x, y, c);
- }
- }
- }
- int main(){
- Display *di = XOpenDisplay(getenv("DISPLAY"));
- if (di == NULL){
- fprintf(stderr, "ERROR: No display");
- exit(EXIT_FAILURE);
- }
- int x=0, y=0, width=WIDTH, height=HEIGHT, border_width=1;
- int sc = DefaultScreen(di);
- Window ro = DefaultRootWindow(di);
- Window wi = XCreateSimpleWindow(di,
- ro,
- x,
- y,
- width,
- height,
- border_width,
- BlackPixel(di, sc),
- WhitePixel(di, sc));
- XMapWindow(di, wi);
- XStoreName(di, wi, "Default Window");
- GC gc = XCreateGC(di, ro, 0, NULL);
- XSelectInput(di, wi, KeyPressMask | ExposureMask);
- XEvent ev;
- int quit = 0;
- int z = 50;
- while (!quit){
- int a = XNextEvent(di, &ev);
- if (ev.type == KeyPress) quit == 1;
- if (ev.type == Expose) {
- worley_noise(di, wi, gc);
- }
- }
- XFreeGC(di, gc);
- XDestroyWindow(di, wi);
- XCloseDisplay(di);
- return (EXIT_SUCCESS);
- }
|