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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	WSquare.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 WSquare extends WRectangle
{
  public WSquare( WRectangle r )
  {
    setEnglishName( "Square" );
    GPolygon gp = r.getPolygon();
    GSegment[] sides = gp.getSides();
    GPoint[] points = gp.getPoints();

    // Calculate the side length as the average side length.

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

    // Calculate the orientation of the first point relative to
    //      the squares's center.

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

    double firstAngle = Degree.fromPoints( center, points[0] );

    double radius = Math.sqrt( Math.pow( sideLength, 2.0 )
      + Math.pow( sideLength, 2.0 ) ) / 2.0;

    // Start with the first point and create the other points
    //      around it.

    GPoint[] arrPoints = new GPoint[4];
    arrPoints[0] = new GPoint( points[0] );
    arrPoints[1] = new GPoint( Geometry.locatePoint(
      center,
      radius,
      Radian.fromDegrees( firstAngle + 90.0 ) ) );
    arrPoints[2] = new GPoint( Geometry.locatePoint(
      center,
      radius,
      Radian.fromDegrees( firstAngle + 180.0 ) ) );
    arrPoints[3] = new GPoint( Geometry.locatePoint(
      center,
      radius,
      Radian.fromDegrees( firstAngle + 270.0 ) ) );

    m_Polygon = new GPolygon( arrPoints );
  }

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

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

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