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

// $Id: WSimQuadrilateral.java,v 1.9 2003/03/06 00:40:40 moltmans Exp $
// Time-stamp: <2000-07-18 13:49:52 moltmans>
package rec.simulate.widgets;


import java.util.*;
import rec.util.*;
import rec.geo.*;
import rec.sys.*;
import rec.core.widgets.*;
import java.io.*;
import java.lang.*;
import rec.meche.Estimator;

public abstract class WSimQuadrilateral extends WSimBody
{
  

  public WSimQuadrilateral( WQuadrilateral q )
  {
    setEnglishName( "Quadrilateral body" );
    m_Polygon = q.getPolygon();
    
    m_Name = "Quad" + getId();
    Console.writeln( "Name = " + getName() );
  }

  public Collection isa()
  {
    Class c = null;
    try {
      c = Class.forName( "rec.simulate.widgets.WSimPolygon" );
    }
    catch( ClassNotFoundException e ) {

      Assert.error( "bad/unrecognized widget class name" );
    }

    Collection types = super.isa();
    types.add( c );
    
    return types;
  }

  public GSegment getMajorAxis() 
  {
    GSegment[] sides = m_Polygon.getSides();
    if( ( sides[0].getLength() + sides[2].getLength() ) >
	( sides[1].getLength() + sides[3].getLength() ) )
    {
      return new GSegment( sides[0].getMidpoint(),
			   sides[2].getMidpoint() );
    }
    else {
      return new GSegment( sides[1].getMidpoint(),
			   sides[3].getMidpoint() );
    }
  }

  public GSegment getMinorAxis() 
  {
    GSegment[] sides = m_Polygon.getSides();
    if( ( sides[0].getLength() + sides[2].getLength() ) >
	( sides[1].getLength() + sides[3].getLength() ) )
    {
      return new GSegment( sides[1].getMidpoint(),
			   sides[3].getMidpoint() );
    }
    else {
      return new GSegment( sides[0].getMidpoint(),
			   sides[2].getMidpoint() );
    }
  }

  
  

  public boolean isOblong() 
  {
    GSegment[] sides = m_Polygon.getSides();

    // check that one set is longer than the other
    return Estimator.isOblong( sides[0].getLength() +
			       sides[2].getLength(),
			       sides[1].getLength() +
			       sides[3].getLength() );

    // this checks if the figure is approx a parallelogram:
//      double ratio1 = sides[0].getLength() / sides[2].getLength();
//      double ratio2 = sides[1].getLength() / sides[3].getLength();
//      // first check if the opposite pairs of sides are approx equal
//      if( ( (ratio1 > 0.8) && ( ratio1 < 1.25 ) ) &&
//  	( (ratio2 > 0.8) && ( ratio2 < 1.25 ) ) )
//      {
//        // approx. parallelogram
//      }
//      return false;
  }
  
  public GShape getShape() 
  {
    return m_Polygon;
  }

  

}
