/*---------------------------------------------------------------------- Sphere Susan L. Weller ---------------------------------------------------------------------- This program will generate a given number of points that lie on the unit sphere. The number of points is given on the command line. ---------------------------------------------------------------------- */ #include #include #define MAX_INT 2147483647 /* Max int is 2^31 - 1 */ #define FUZZ 0.000001 main( argc, argv ) int argc; char *argv[]; { int n; double x, y, z, r; srandom( (int) time((long *) 0 ) ); if ( argc != 2 ) printf( "Usage: sphere \n" ), exit(1); n = atoi( argv[1] ); /* n is number of points */ while (n--) { do { /* the do-while will generate points until one is inside the unit sphere. */ x = 2.0 * (double) random() / MAX_INT - 1.0; y = 2.0 * (double) random() / MAX_INT - 1.0; z = 2.0 * (double) random() / MAX_INT - 1.0; r = sqrt( x*x + y*y + z*z ); } while ( r <= FUZZ || r > 1.); printf ("%6d %6d %6d\n", (int) (100 * x/r), (int) (100 * y/r), (int) (100 * z/r) ); } }