|
@@ -0,0 +1,143 @@
|
|
|
+#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);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|