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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	WCircleGroup.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	06-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 java.util.*;

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

public class WCircleGroup extends WGroup
{
  Squiggle m_loop;

  public WCircleGroup( Squiggle loop )
  {
    setEnglishName( "Group of items" );
    this.m_loop = loop;
  }

  public WCircleGroup( Squiggle loop, Vector wid )
  {
    super( wid );
    setEnglishName( "Group of items" );
    this.m_loop = loop;
  }

  public void add( VisibleWidget w )
  {
    widgets.addElement( w );
    bounds = null;
  }

  public void move( GPoint gp )
  {
    GPoint p = getLocation().getCenter();

    GPoint spot = m_loop.getLocation().getCenter();
    spot.x = spot.x - p.x + gp.x;
    spot.y = spot.y - p.y + gp.y;
    m_loop.move( spot );

    bounds = null;
  }

  public void rotate( GPoint gp, double angle )
  {
    Iterator e = widgets.iterator();

    while( e.hasNext() )
      {
	( (VisibleWidget)( e.next() ) ).rotate( gp, angle );
      }

    m_loop.rotate( gp, angle );

    bounds = null;
  }

  protected void drawSelection( Graphics g )
  {
    Color c = g.getColor();

    Rectangle r = getBounds();

    Color selCol = selectedColor();

    g.setColor( selCol );

    Polygon pg = new Polygon();
    GPoint cent = m_loop.getLocation().getCenter();
    int N = m_loop.getNumberOfPoints();

    for ( int i = 0; i < N; i++ )
      {
	GPoint pts = m_loop.getPointAt( i );
	int x = (int)( pts.x + (cent.x - pts.x) * 0.08);
	int y = (int)( pts.y + (cent.y - pts.y) * 0.08);
	pg.addPoint( x, y );
      }

    g.fillPolygon( pg );

    m_loop.draw( g );

    g.setColor( c );
  }

  public boolean crossing( WLine l )
  {
    GPolySegment poly = m_loop.getPolygon();

    if ( poly.contains( l.getLocation().getCenter() )
	 && !poly.contains( l.getHead() )
	 && !poly.contains( l.getTail() ) )
      return true;
    else
      return false;
  }

  public boolean includes( GPoint gp, double offset ) 
  {
    return (m_loop.includes( gp, offset ) || super.includes( gp, offset ));
  }

  public Rectangle getBounds()
  {
    // bounds is in WGroup:
    if ( bounds == null )
      bounds = m_loop.getBounds();

    return bounds;
  }

  public Object clone()
  {
    Object o = ( new WCircleGroup( m_loop, widgets ) );

    return o;
  }



}

