فهرست منبع

x11-random/worley-noise k&r indentation and little refactor

cmte 5 سال پیش
والد
کامیت
42a267f54c
1فایلهای تغییر یافته به همراه35 افزوده شده و 30 حذف شده
  1. 35 30
      x11-random/worley-noise.c

+ 35 - 30
x11-random/worley-noise.c

@@ -17,7 +17,8 @@ struct point {
 };
 
 
-double dist_p1_p2(int p1x, int p1y, int p2x, int p2y){
+double dist_p1_p2(int p1x, int p1y, int p2x, int p2y)
+{
 	double pA, pB;
 	pA = p1x - p2x;
 	pB = p1y - p2y;
@@ -26,43 +27,48 @@ double dist_p1_p2(int p1x, int p1y, int p2x, int p2y){
 
 }
 
-void draw_pixel(Display * di, Window wi, GC gc, int x, int y, int color){
+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(){
+float rand_0_1()
+{
 	int o0;
 	float o0f, r;
-	srand(SEED+seed_offset++); // define random initial seed
+	srand(SEED + seed_offset++);	// define random initial seed
 	o0 = rand();
 	o0f = (float) o0;
 	r = o0f / RAND_MAX;
 	return (r);
 }
 
-double * sort(double * arr){
+void 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]){
+	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){
+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){
+void worley_noise(Display * di, Window wi, GC gc)
+{
 	int x, y, i;
 	int c;
 	int noise_space[WIDTH][HEIGHT];
@@ -70,40 +76,40 @@ void worley_noise(Display * di, Window wi, GC gc){
 	struct point p[POINTS];
 
 
-	for (x=0; x<WIDTH; x++){
-		for (y=0;y<HEIGHT; y++){
+	for (x = 0; x < WIDTH; x++) {
+		for (y = 0; y < HEIGHT; y++) {
 			c = (int) (rand_0_1() * 255);
-			c = (c << 16 ) | (c << 8) | c;
+			c = (c << 16) | (c << 8) | c;
 			noise_space[x][y] = c;
 		}
 	}
-	
-	for (i=0; i<POINTS; i++)
-	{
+
+	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++){
+	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 = map(dist[0], 0, WIDTH / 4, 255, 20);
 
-			c = (c << 16 ) | (c << 8) | c;
+			c = (c << 16) | (c << 8) | c;
 			draw_pixel(di, wi, gc, x, y, c);
 		}
 	}
 }
 
-int main(){
+int main()
+{
 	Display *di = XOpenDisplay(getenv("DISPLAY"));
-	if (di == NULL){
+	if (di == NULL) {
 		fprintf(stderr, "ERROR: No display");
 		exit(EXIT_FAILURE);
 	}
-	int x=0, y=0, width=WIDTH, height=HEIGHT, border_width=1;
+	int x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1;
 	int sc = DefaultScreen(di);
 	Window ro = DefaultRootWindow(di);
 	Window wi = XCreateSimpleWindow(di,
@@ -125,9 +131,10 @@ int main(){
 	XEvent ev;
 	int quit = 0;
 	int z = 50;
-	while (!quit){
+	while (!quit) {
 		int a = XNextEvent(di, &ev);
-		if (ev.type == KeyPress) quit == 1;
+		if (ev.type == KeyPress)
+			quit == 1;
 		if (ev.type == Expose) {
 			worley_noise(di, wi, gc);
 		}
@@ -139,5 +146,3 @@ int main(){
 	return (EXIT_SUCCESS);
 
 }
-
-