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

package rec.fun;

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

public class ParallelLineMaker extends Recognizer {

  WLine last;
  double ang;

  public ParallelLineMaker()
  {
  }

  public boolean recognize( Surface s, Widget w )
  {
    WLine l = (WLine)w;

    //Console.writeln( "|| line test" );
    if ( ( last == null ) || !s.containsWidget( last ) || w.equals( last ) )
      {
	last = l;
	ang = last.getLine().getAngle();

	return false;
      }

    //Console.writeln( "going to compare." );

    double nang = l.getLine().getAngle();
    boolean ret = false;
    
    double df = delta( ang, nang );

    //Console.writeln( "angles: del=" + df + "   " + ang + " - " + nang );

    if ( df < Math.PI / 16 )
      {
	//Console.writeln( "Making lines parallel." );
	l.rotate( l.getMidpoint(), -1 * ( ang - nang ) );

	// our new angle for the line
	nang = ang;

	ret = false;
      }

    last = l;
    ang = nang;
	
    return ret;
  }

  public double delta( double a1, double a2 )
  {
    double df = Geometry.delta( a1, a2 );
    
    df %= Math.PI;

    if ( df > Math.PI / 2 )
      df -= Math.PI;

    return Math.abs( df );
  }


  public Iterator getInputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.core.widgets.WLine" );

    return( v.iterator() );
  }

  public Iterator getOutputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.core.widgets.WLine" );

    return( v.iterator() );
  }

}
