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

// -*-Mode: Java-*- 
// WMRectangle.java -- 
// Author: Christine Alvarado <calvarad@ai.mit.edu> 
// Maintainer: Christine Alvarado <calvarad@ai.mit.edu> 
// Version: 
// Created: <Wed Mar 10 19:09:22 1999> 
// Time-stamp: <1999-11-08 21:23:45 calvarad> 
// Keywords: 
package rec.simulate.widgets.wm;

import java.awt.*;
//import java.io.*;
import rec.util.*;
import rec.geo.*;
import rec.sys.*;
import rec.core.widgets.*;
import java.math.*;
import rec.simulate.*;
import rec.simulate.widgets.*;

// class used when an interface to Working Model is desired.
// this is a rectangle, but for working model we must convert it to 
// a polygon if it is not stright up and down.  For now assume all 
// rectangles that we get at WM rectangles and deal with the rest later.
public class WMRectangle extends WSimRectangle
{

  public WMRectangle( WRectangle r )
  {
    super( r );
    // here we should check to see if we want to draw this rectangle
    // as a WM Rectangle or as a polygon.
    GPolygon gp = r.getPolygon();
    GPoint[] points = gp.getPoints();

    GPoint midPoint0To3 =
      (new GSegment( points[0], points[3] )).getMidpoint();
    GPoint midPoint1To2 = 
      (new GSegment( points[1], points[2] )).getMidpoint();
    
    double angle = Geometry.angle( midPoint1To2, midPoint0To3 );
    double epsilon = 0.1;
    // if the angle is not close enough to 0 or pi/2 then the 
    // rectangle should be drawn as a polygon
    m_IsWMRectangle = false;
    for ( int i = 0; i < 4; i++ ) {
      if (Math.abs(angle) < epsilon) {
	Console.writeln("This is a vertical rectangle");
	m_IsWMRectangle = true;
      }
      angle -= Math.PI / 2;
    }
  }

  // the key to this class is the load funciton, which "loads"
  // the object into the simulator.  that is, prints it to the file.
  // it takes a WMSimulator so that it knows the conversion factors
  // that it should use to convert dimensions.
  // it needs another parameter to resolve name conflicts.  The system
  // is seriously limited at the moment.
  public String load( WMSimulatorRegistryBase reg ) 
  {
    GPoint newCenter = reg.convert( getLocation().getCenter() );
    double newH = reg.convert( m_height );
    double newW = reg.convert( m_width );
    String output;
    
    Console.writeln( "loading rectangle" );
    
    
    output = "Dim " + m_Name + " as WMBody\n";
    // assume the point center is a temp point here for our use
    // also there is a dynamic array which is here for our use as well
    output += "center.X = " + newCenter.x + " : center.Y = " + 
		    newCenter.y + "\n";
    if ( m_IsWMRectangle ) {
      output += "Set " + m_Name + " = Draw_Rect( " + newW + ", " +
		      newH + ", center )\n";
    }
    else {
      // build the points array
      // remember to multiply the y coord by -1
      GPoint[] points = m_Polygon.getPoints();
      output +=  "redim p(4)\n" ;
      for ( int i = 1; i <= 4; i++ ) {
	GPoint vertex = reg.convert( points[i-1] );
	output += "p(" + i + ").X = " + vertex.x + "\n";
	output += "p(" + i + ").Y = " + vertex.y + "\n";
      }
      output +=  "Set " + m_Name + " = Draw_Poly( 4, p, center )\n" ;
    }
      
    return output;

  }

///////////////////////////////////////////////////////////////////////////  
//
  private boolean m_IsWMRectangle;
}
