#
# Minimum Spanning Circle and Convex Hull for polys.
#
#				Gershon Elber, February 1996
#

save_mat = view_mat;
view_mat = sc( 0.5 );
viewobj( view_mat );
view_mat = save_mat;

iritstate( "RandomInit", 1964 );  # Seed-initiate the randomizer.

RandomPts = function( n ): l: i: t: r:
    l = nil():
    for ( i = 1, 1, n,
	r = random( -1, 1 ):
	t = random( 0, 2 * pi ):
        snoc( vector( cos( t ) * r, sin( t ) * r, 0 ), l ) ):
    return = poly( l, false ):
    color( return, red ):
    adwidth( return, 5 );

n = 4:
while ( n <= 1024,
    printf( "Processing %4d pts...", list( n ) ):
    p = RandomPts( n ):
    ch = cnvxhull( p, 0 ):
    color( ch, green ):
    adwidth( ch, 2 ):
    msc = mscirc( p ):
    cntr = getattr( msc, "center" ):
    rad = getattr( msc, "radius" ):
    color( msc, yellow ):
    adwidth( msc, 2 ):
    view( list( p, msc, ch ), 1 ):
    printf( "done (Center = %.5lg %.5lg, Radius = %.5lg).\\n",
	    list( coord( cntr, 0 ), coord( cntr, 1 ), rad ) ):
#    pause():
    msleep( 1000 ):
    n = n * 2 );

save( "msc_ch1", list( ch, msc, p ) );
 
RandomVecs = function( n ): l: i: t: r:
    l = nil():
    for ( i = 1, 1, n,
	r = random( -1, 1 ):
	t = random( 0, 2 * pi ):
        snoc( vector( cos( t ) * r, 2.0, sin( t ) * r ), l ) ):
    return = l:
    color( return, red ):
    adwidth( return, 3 );

n = 4:
while ( n <= 1024,
    printf( "Processing %4d vecs...", list( n ) ):
    p = RandomVecs( n ):
    msc = mscirc( p ):
    cntr = getattr( msc, "center" ):
    rad = getattr( msc, "angle" ):
    color( msc, yellow ):
    attrib( p, "UseAvg", 1 ):
    msc2 = mscirc( p ):
    cntr2 = getattr( msc2, "center" ):
    rad2 = getattr( msc2, "angle" ):
    color( msc2, cyan ):

    view( list( msc, msc2, axes, p ), 1 ):
    printf( "done Radius1 = %.5lg Radius2 = %.5lg.\\n", list( rad, rad2 ) ):
#    pause():
    msleep( 1000 ):
    n = n * 2 );

save( "msc_ch2", list( msc2, msc, p ) );
 

free( n );
free( p );
free( ch );
free( msc );
free( cntr );
free( rad );
free( msc2 );
free( cntr2 );
free( rad2 );

