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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	WRectangle.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	07-XX-98
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:     Drawing recognition
//
// HISTORY:     11-15-98 archon
//              Now aware of new rec.geo package
//
////////////////////////////////////////////////////////////////////////////////

package rec.core.widgets;

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

public class WRectangle extends WQuadrilateral
{
  public WRectangle( WQuadrilateral q )
  {
    super();
    setEnglishName( "Rectangle" );

    GPolygon gp = q.getPolygon();
    GSegment[] sides = gp.getSides();
    GPoint[] points = gp.getPoints();

    // Calculate the side length as average side length.

    double sideLength1 = 0.0;
    for( int i = 0; i < 4; i += 2 ) {
      sideLength1 += sides[i].getLength();
    }
    sideLength1 /= 2.0;

    double sideLength2 = 0.0;
    for( int i = 1; i < 4; i += 2 ) {
      sideLength2 += sides[i].getLength();
    }
    sideLength2 /= 2.0;

    // Estimate one orientation of rectangle.

    GPoint midPoint0To3 =
      (new GSegment( points[0], points[3] )).getMidpoint();
    GPoint midPoint1To2 =
      (new GSegment( points[1], points[2] )).getMidpoint();

    double angle = Geometry.angle( midPoint1To2, midPoint0To3 );

    // Get the center of the rectangle.

    GPoint center = q.getLocation().getCenter();

    // Calculate the sides.

    GPoint[] arrPoints = new GPoint[4];

    GPoint mp0To1 = Geometry.locatePoint( center,
					  sideLength2 / 2.0,
					  angle - Math.PI / 2.0 );

    arrPoints[0] = new GPoint( Geometry.locatePoint(
      mp0To1,
      sideLength1 / 2.0,
      angle ) );
    arrPoints[1] = new GPoint( Geometry.locatePoint(
      mp0To1,
      sideLength1 / 2.0,
      angle + Math.PI ) );

    GPoint mp3To4 = Geometry.locatePoint( center,
					  sideLength2 / 2.0,
					  angle + Math.PI / 2.0 );

    arrPoints[2] = new GPoint( Geometry.locatePoint(
      mp3To4,
      sideLength1 / 2.0,
      angle + Math.PI ) );
    arrPoints[3] = new GPoint( Geometry.locatePoint(
      mp3To4,
      sideLength1 / 2.0,
      angle ) );

    m_Polygon = new GPolygon( arrPoints );
  }

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

  protected WRectangle()
  {
    // For subclass use.
  }

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

  public void draw( Graphics g )
  {
    if( Display.isOutputEnabled() ) {
      Color oldColor = g.getColor();
      g.setColor( Color.blue );
      getMajorAxis().display( g );
      g.setColor( Color.green );
      getMinorAxis().display( g );
      g.setColor( oldColor );
    }

    if( Display.isOutputEnabled() ) {
      Color oldColor = g.getColor();
      g.setColor( Color.red );
      GSegment maj = getMajorAxis();
      GSegment min = getMinorAxis();
      (new GOval( new GPoint( getLocation().getCenter() ),
		  Geometry.angle( maj.getTailPoint(), maj.getHeadPoint() ),
		  (int)Math.rint( maj.getLength() / 2.0 ),
		  (int)Math.rint( min.getLength() / 2.0 ) ))
	.display( g );
      g.setColor( oldColor );
    }

    super.draw( g );
  }

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

  public GSegment getMajorAxis()
  {
    GSegment[] lines = m_Polygon.getSides();

    if( lines[0].getLength() <= lines[1].getLength() )
      return( new GSegment( lines[0].getMidpoint(),
			    lines[2].getMidpoint() ) );

    return( new GSegment( lines[1].getMidpoint(),
			  lines[3].getMidpoint() ) );
  }

  public GSegment getMinorAxis()
  {
    GSegment[] lines = m_Polygon.getSides();

    if( lines[0].getLength() > lines[1].getLength() )
      return( new GSegment( lines[0].getMidpoint(),
			    lines[2].getMidpoint() ) );

    return( new GSegment( lines[1].getMidpoint(),
			  lines[3].getMidpoint() ) );
  }

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

  public Object clone()
  {
    WRectangle ret = new WRectangle();

    ret.m_Polygon = new GPolygon( m_Polygon );

    return( ret );
  }
}

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