// (c) MIT 2003.  All rights reserved.

package rec.house;


import java.awt.*;
import java.util.*;
import rec.core.*;
import rec.util.*;
import rec.sys.*;
import rec.geo.*;

public class GraphPaper extends Recognizer
{
  int cur_name = 0;

  public GraphPaper()
  {
  }
  
  /**
   * Each new stroke generated gets put into the pipe here.  It will
   * remove and add objects to the 'out' target as it deems fit.
   *
   * @return true if changes were made
   */
  public boolean recognize( Surface s, Widget w )
  {
    WLine wl = (WLine)w;

    GSegment gs = wl.getLine();

    // get angle of line in -PI to PI range
    double ang = gs.getAngle();

    //    Console.writeln( "ang = " + ang );

    double dx = Math.abs( wl.getHead().x - wl.getTail().x );
    double dy = Math.abs( wl.getHead().y - wl.getTail().y );

    // horizontal
    //    if ( ( ( ang < Math.PI * -0.9 ) || ( ang > Math.PI * 0.9 ) )
    //	 || ( ( ang < Math.PI * 0.1 ) && ( ang > Math.PI * -0.1 ) ) )
    if ( dy < 20 )
      {
	GPoint t = wl.getHead();
	GPoint t2 = wl.getTail();
	double d = ( t.y + t2.y ) / 2;
	wl.setHead( new GPoint( t.x, d ) );
	wl.setTail( new GPoint( t2.x, d ) );

	return true;
      }

    // vertical
    //    else if ( ( ( ang > Math.PI * 0.4 ) && ( ang < Math.PI * 0.6 ) )
    //	      || ( ( ang > Math.PI * -0.6 ) && ( ang < Math.PI * -0.4 ) ) )
    else if ( dx < 20 )
      {
	GPoint t = wl.getHead();
	GPoint t2 = wl.getTail();
	double d = ( t.x + t2.x ) / 2;
	wl.setHead( new GPoint( d, t.y ) );
	wl.setTail( new GPoint( d, t2.y ) );

	return true;
      }

    return false;
  }
  
  public Iterator getInputTypes()
  {
    Vector v = new Vector();
    
    v.addElement( "rec.core.WLine" );
    
    return v.iterator();
  }

  public Iterator getOutputTypes() 
  {
    return getInputTypes();
  }


}
