/**
 * @author
 *  Behrooz Badii 	bb2122@columbia.edu
 *  Hanhua Feng         hanhua@cs.columbia.edu
 *  Edan Harel 	        edan@columbia.edu
 */
package CookieCutter.g6;

import java.text.NumberFormat;

class GeometryException extends Exception {
    public GeometryException() {}

    public GeometryException( String s ) {
        super(s);
    }
}

public class Plane {
    /** a small value of computational errors */
    public static final double EPS = 1E-12;

    static java.util.Random rand = new java.util.Random();

    static NumberFormat _nf;

    static {
        _nf = NumberFormat.getInstance();
        _nf.setMinimumFractionDigits( 1 );
        _nf.setMaximumFractionDigits( 4 );
        _nf.setMinimumIntegerDigits( 1 );
    }
    
    /** convert a double value to a string 
     */
    public static String format( double x ) {
        return _nf.format( x );
    }

    /** check the sign of a number, considering acceptable
     * the computational error.
     */
    public static int sign( double x ) {
        if ( x > EPS )
            return 1;
        if ( x < -EPS )
            return -1;
        return 0;
    }

    /** get a transform matrix, given the coordinates of the new
     * origin wrt the old coordinate system, (x0, y0), and the
     * counter-clockwise angle of the new x-axis wrt the old x-axis,
     * alpha.
     *
     * The formula is
     *   x' =  x\cos\alpha + y\sin\alpha - x_0
     *   y' = -x\sin\alpha + y\cos\alpha - y_0
     */
    public static final double[] transmatrix( double x0, double y0,
                                              double alpha ) {
        double c = Math.cos( alpha );
        double s = Math.sin( alpha );
        return new double[]{ c, s, -x0, -s, c, -y0 };
    }

    /** get a transform matrix such that the new origin is p0, and
     * the new x-axis the line p0->p1.  No scaling is performed.
     */
    public static final double[] transmatrix( Point p0, Point p1 ) {
        double d = p0.distance( p1 );
        if ( d <= EPS )
            return new double[] { 1.0, 0.0, -p0.x(), 0.0, 1.0, -p0.y() };

        double c = ( p1.x() - p0.x() )/d;
        double s = ( p1.y() - p0.y() )/d;
        return new double[] { c, s, -p0.x(), -s, c, -p0.y() };
    }

    /** get a transform matrix from the old coordinates to the new if
     * we rotate the object at (x0,y0) by angle alpha.  The coordinate
     * system do not change.
     *
     * The formula is
     *   x' = x\cos\alpha - y\sin\alpha + x_0(1-\cos\alpha) + y_0\sin\alpha
     *   y' = x\sin\alpha + y\cos\alpha + y_0(1-\cos\alpha) + x_0\sin\alpha
     */
    public static final double[] rotatematrix( double x0, double y0,
                                               double alpha ) {
        double c = Math.cos( alpha );
        double s = Math.sin( alpha );
        return new double[]{ c, -s, (1-c)*x0+s*y0, s, c, (1-c)*y0-s*x0 };
    }
}
