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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	Estimator.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	February, 1999
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Mechanical engineering drawing recognition estimator
//
// HISTORY:     Feb 99  archon  created
//
////////////////////////////////////////////////////////////////////////////////

package rec.meche.util;

import rec.util.*;
import rec.geo.*;
import rec.loc.*;

public final class Estimator
{
  public static GPoint connects( GPoint p1, GPoint p2 )
  {
    if( (new Region( p1, 10 )).intersects( new Region( p2, 10 ) ) )
      return( new GPoint( (p1.x + p2.x) / 2.0, (p1.y + p2.y) / 2.0 ) );

    return( null );
  }

  public static boolean isPerpendicular( double angle )
  {
    return( (angle >= Radian.scaleFromDegrees( 70.0 )
             && angle <= Radian.scaleFromDegrees( 110.0 ))
            ||
            (angle >= Radian.scaleFromDegrees( 250.0 )
             && angle <= Radian.scaleFromDegrees( 290.0 )) );
  }

  public static boolean arePerpendicular( GSegment s1, GSegment s2 )
  {
    GPoint intersection;

    if( (intersection = s1.getIntersection( s2 )) == null )
      return( false );

    GPoint p1 = s1.getHeadPoint();
    GPoint p2 = s2.getHeadPoint();

    if( intersection.equals( p1 ) )
      p1 = s1.getTailPoint();

    if( intersection.equals( p2 ) )
      p2 = s1.getTailPoint();

    double angle = Geometry.angleBetween( p1, intersection, p2 );

    return( isPerpendicular( angle ) );
  }
}

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