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

// $Id: LooseCircleRecognizer.java,v 1.5 2003/03/06 00:40:48 moltmans Exp $
// Time-stamp: <2000-03-11 18:54:46 moltmans>
package rec.core.recognizers;


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

public class LooseCircleRecognizer extends Recognizer
{

  public static boolean isCircle( rec.sys.Squiggle st )
  {
    int num = st.getNumberOfPoints();
    
    double dist = 0.0;
    int cntr = 0;
    int jump = num / 20;
    if( jump == 0 ) jump = 1;
    //    num -= jump;

    for( int i = 0; i < (num - jump); i += jump ) 
      {
	dist += Geometry.distance( st.getPointAt( cntr ), 
				   st.getPointAt( cntr + jump ) );
      }

    if ( isLoop( st ) )
      {
	if( st.getLength() < 50 )
	  return false;

	return true;
      }

    return false;
  }

  public static boolean isLoop( rec.sys.Squiggle st )

  {
    if( !( Geometry.distance( st.getFirstPoint(),
			      st.getLastPoint() )
	   < Math.min( st.getLength() / 5, 50.0 ) ) )
      return( false );

    return( true );
  }


  public LooseCircleRecognizer()
  {
  }


  public boolean recognize( Surface s, Widget w )
  {
    rec.sys.Squiggle st = (rec.sys.Squiggle)w;

    if( st.getLength() < 40 )
      return( false );
    
    if( isCircle( st ) ) {

      Console.writeln( "circle online" );

      WCircle c = new WCircle( st.getBounds() );
      s.replaceWidget( st, c );

      return( true );
    }

    return( false );
  }

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

    v.addElement( "rec.sys.Squiggle" );

    return( v.iterator() );
  }

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

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

    return( v.iterator() );
  }
}

////////////////////////////////////////////////////////////////////////////////
