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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	EquilateralTriangle.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 rec.geo package
//
////////////////////////////////////////////////////////////////////////////////

package rec.core.widgets;

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

public class WEquilateralTriangle extends WTriangle
{
  public WEquilateralTriangle( WTriangle t )
  {
    this( (WPolygon)t );
  }

  public WEquilateralTriangle( WPolygon t )
  {
    super();
    setEnglishName( "Equilateral Triangle" );
    Assert.check( t.numSides() == 3,
		  "making triangle with wrong number of sides." );

    GPolygon gp = t.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 < 3; ++i ) {
      sideLength += sides[i].getLength();
    }
    sideLength /= 3.0;

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

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

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

    double radius = Math.sqrt( 3.0 ) * sideLength / 3.0;

    // Start with the first point and create the other points
    //   around it.
    // move the circle to the origin, then move it back
    GPoint firstAtOrigin = new GPoint( points[0].x - center.x, 
				       points[0].y - center.y );
    GPoint secondAtOrigin;
    GPoint thirdAtOrigin;
    GPoint[] arrPoints = new GPoint[3];

    arrPoints[0] = new GPoint( points[0] );
    
    double rad120 = Math.toRadians( 120.0 );
    double sin120 = Math.sin( rad120 );
    double cos120 = Math.cos( rad120 );

    secondAtOrigin = new GPoint( firstAtOrigin.x * cos120 - 
				 firstAtOrigin.y * sin120,
				 firstAtOrigin.x * sin120 +
				 firstAtOrigin.y * cos120 );
    arrPoints[1] = new GPoint( secondAtOrigin.x + center.x, 
			       secondAtOrigin.y + center.y );
    thirdAtOrigin = new GPoint( secondAtOrigin.x * cos120 - 
				secondAtOrigin.y * sin120,
				secondAtOrigin.x * sin120 +
				secondAtOrigin.y * cos120 );
    arrPoints[2] = new GPoint( thirdAtOrigin.x + center.x, 
			       thirdAtOrigin.y + center.y );
    

    m_Polygon = new GPolygon( arrPoints );
  }

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

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


  public void draw( Graphics graph )
  {
    super.draw( graph );

    graph.setColor( Colors.getColor( "text" ) );
    GPoint p = getLocation().getCenter();
    graph.drawString( "E", (int) p.x, (int) p.y );
  }

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

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

    ret.m_Polygon = new GPolygon( m_Polygon );

    return( ret );
  }
}

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