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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	Space.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	06-XX-98
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Location approximation
//
// HISTORY:     11-15-98 archon
//              Now aware of new rec.geo package
//              Made mutable
//
////////////////////////////////////////////////////////////////////////////////

package rec.loc;

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

/**
 * A Space is a mutable representation of the area between two concentric
 *     circles.
 */
public class Space
{
  public Space( double radius )
  {
    this( GPoint.ORIGIN, radius );
  }

  public Space( double innerRadius, double outerRadius )
  {
    this( GPoint.ORIGIN, innerRadius, outerRadius );
  }

  public Space( GPoint center, double radius )
  {
    this( center, 0.0, radius );
  }

  public Space( GPoint center, double innerRadius, double outerRadius )
  {
    Assert.check( innerRadius >= 0.0, "inner radius cannot be negative" );

    Assert.check( outerRadius >= 0.0, "inner radius cannot be negative" );

    Assert.check( innerRadius <= outerRadius,
		  "inner radius cannot be bigger than outer radius" );

    m_Center = new GPoint( center );
    m_InnerRadius = innerRadius;
    m_OuterRadius = outerRadius;
  }

  public Space( Space s )
  {
    this( s.m_Center, s.m_InnerRadius, s.m_OuterRadius );
  }

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

  public void setCenter( GPoint p )
  {
    m_Center = new GPoint( p );
  }

  public GPoint getCenter()
  {
    return( m_Center );
  }

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

  public Slice getSlice( double beginDegree, double endDegree )
  {
    return( new Slice( this, beginDegree, endDegree ) );
  }

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

  public boolean intersects( Space s )
  {
    double d = Geometry.distance( m_Center , s.m_Center );

    return( (d >= m_InnerRadius - s.m_OuterRadius
	     && d <= m_OuterRadius + s.m_InnerRadius)
	    ||
	    (d >= m_InnerRadius - s.m_OuterRadius
	     && d <= m_OuterRadius + s.m_InnerRadius) );
  }

  public boolean contains( Locatable l )
  {
    double d = Geometry.distance( m_Center, l.getLocation().getCenter() );

    double r = l.getLocation().getRadius();

    return( d >= m_InnerRadius - r && d <= m_OuterRadius + r );
  }

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

  private GPoint m_Center;
  private double m_InnerRadius;
  private double m_OuterRadius;
}

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