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

package rec.core.widgets;

import edu.mit.sketch.geom.Point;
import edu.mit.sketch.geom.Polygon;
import java.awt.Graphics;
import java.awt.Rectangle;
import rec.geo.GPoint;
import rec.geo.Location;
import rec.sys.VisibleWidget;
import rec.util.Assert;



/**
 * 
 * <p>
 * Created:   Tue Mar 19 12:14:42 2002<br>
 * Copyright: Copyright (C) 2002 by MIT.  All rights reserved.<br>
 * 
 * @author <a href="mailto:moltmans@ai.mit.edu">Michael Oltmans</a>
 * @version $Id: WPolyLine.java,v 1.2 2003/03/06 00:40:47 moltmans Exp $
 **/

public class WPolyLine extends VisibleWidget
{
  // this is actually a polyline it just happens to be named polygon...
  private Polygon m_polyline;
    
  public WPolyLine( Polygon polyline )
  {
    Assert.notNull( polyline, "Can't have a polyline with a null polyline" );
    m_polyline = polyline;
  }

  /**
   * the length of the diagonal of the bounding box.
   **/
  public double size()
  {
    java.awt.Rectangle bounds = m_polyline.getBounds();
    return Math.sqrt( (bounds.x * bounds.x) + (bounds.y * bounds.y) );
  }

  /**
   * Unsupported operation.
   **/
  public void resize( double scaleFactor )
  {
    throw( new UnsupportedOperationException( "Unimplimented resize method" ) );
  }

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

  public void draw( Graphics g )
  {
    m_polyline.paint( g );
  }

  /**
   * Check of gp is within offset distance of the polyline.
   **/
  public boolean includes( GPoint gp, double offset )
  {
    return m_polyline.pointIsOn( new Point( (int)gp.getX(), (int)gp.getY() ), (int)offset );
  }

  /**
   * Check if gp is on the civen polyline.
   **/
  public boolean contains( GPoint gp )
  {
    return includes( gp, 3 );
  }

  /**
   * Move the polyline to the location given at gp.
   **/
  public void move( GPoint gp )
  {
    GPoint center = getLocation().getCenter();
    m_polyline.translate( gp.getX() - center.getX(),
			  gp.getY() - center.getY() );
  }

  /**
   * Move by the given amount.  gp is a vector, not absolute position.
   */
  public void translate( GPoint gp )
  {
    m_polyline.translate( gp.x, gp.y );
  }

  public void rotate( GPoint gp, double angle )
  {
    m_polyline.translate( -1.0 * gp.getX(), -1.0 * gp.getY() );
    m_polyline.rotateAboutOrigin( angle );
    m_polyline.translate( gp.getX(), gp.getY() );
  }

  public Location getLocation()
  {
    return new Location( getBounds() );
  }

  public Rectangle getBounds()
  {
    return m_polyline.getHorizontalBounds();
  }

  public double getLength()
  {
    double length = 0;
    for( int i = 0; i < m_polyline.npoints; i++ ) {
      length += m_polyline.getEdge( i ).length();
    }
    
    return length;
  }

  public Polygon getPolyLine() 
  {
    return m_polyline;
  }
  

}// WPolyLine
