/**
 * File          : SpaceRound.java
 * Desc          : A circular pit (or spherical, etc)
 * Hist
 *   2004-02-22  : @author Douglas Reay
 */
import java.math.*;

public class SpaceRound implements Space {

    /** Return a valid random point from within the space */
    public Point getPoint(int dimensions) {
        double mycoords[] = new double[dimensions];
        for(int loop = 0 ; loop < dimensions ; loop++) {
            mycoords[loop] = 1.0;
        }
        while( isNotValid( mycoords ) ) { recalc( mycoords ); }
        return new Point( mycoords );
    }

    // True if the point is outside the bounds of the circle
    private boolean isNotValid( double mycoords[] ) {
        double sum = 0.0;
        int dimensions = mycoords.length;
        for(int loop = 0 ; loop < dimensions ; loop++) {
            sum = sum + ( mycoords[loop] * mycoords[loop] );
        }
        return( sum > 1.0 );
    }

    // create a new random point
    private void recalc( double mycoords[] ) {
        int dimensions = mycoords.length;
        for(int loop = 0 ; loop < dimensions ; loop++) {
            mycoords[loop] = getRandomCoord();
        }
    }

    // A random number between -1 and +1
    private double getRandomCoord() {
        return ( 2 * Math.random() ) - 1;
    }


    public String toString(int dimensions) { return "Round"; }
}

// End of File