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

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


import rec.util.*;
import rec.geo.*;
import rec.core.widgets.*;
import rec.meche.Estimator;

public abstract class WSimRectangle extends WSimBody
{
  protected double m_height;
  protected double m_width;

  public WSimRectangle( WRectangle r )
  {

    setEnglishName( "Rectangular body" );

    m_Polygon = r.getPolygon();
    //m_Center = r.getLocation().getCenter();
    
    setHeightAndWidth();

    m_Name = "Rect" + getId();
    Console.writeln( "Name = " + getName() );
  }

  public double getWidth() 
  {
    return m_width;
  }

  public double getHeight()
  {
    return m_height;
  }
  
  // calculates the height of the rectangle.
  private void setHeightAndWidth()
  {
    
    GSegment[] sides = m_Polygon.getSides();
    GPoint[] points = m_Polygon.getPoints();
    
    // check which way is height

    double h1 = points[0].y - points[1].y;
    double h2 = points[1].y - points[2].y;
    
    if ( Math.abs( h1 ) > Math.abs( h2 ) ) {
      m_height = sides[0].getLength();
      m_width = sides[1].getLength();
    }
    else {
      m_height = sides[1].getLength();
      m_width = sides[0].getLength();
    }
  }

  public GSegment getMajorAxis() 
  {
    GSegment[] sides = m_Polygon.getSides();
    if( sides[0].getLength() > sides[1].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[1].getLength() )
    {
      return new GSegment( sides[1].getMidpoint(),
			   sides[3].getMidpoint() );
    }
    else {
      return new GSegment( sides[0].getMidpoint(),
			   sides[2].getMidpoint() );
    }
  }

  public boolean isOblong() 
  {
    // check that one pair of sides is longer than the other
    return Estimator.isOblong( getWidth(), getHeight() );
  }

  public GShape getShape() 
  {
    return m_Polygon;
  }

  
}
