/**
  *
  * See the end of the file for the log of changes.
  *
  * $Author: mtsezgin $
  * $Date: 2000/06/03 01:52:33 $
  * $Revision: 1.2 $
  * $Headers$
  * $Id: Ground.java,v 1.2 2000/06/03 01:52:33 mtsezgin Exp $
  * $Name:  $
  * $Locker:  $
  * $Source: /mit/mtsezgin/CVS/Repository/REC/Ground.java,v $
  *
  **/


import java.awt.Color;
import java.awt.Graphics;

import java.util.Vector;

/**
  *
  * A basic Ground object with shading and edges.
  *
  **/

class      Ground
implements Rotatable,
           NonTerminal
{
    /**
    *
    * Original body of the shade.
    *
    **/
    private Shade shade;
    
    /**
    *
    * Original edge of the ground object.
    *
    **/
    private Polygon edge;
    
    /**
    *
    * Modified edge.
    *
    **/
    private Polygon new_edge;
    
    
    /**
    *
    * children
    *
    **/
    private Vector children;

    /**
    *
    * Graphics context for this Geometric object.
    *
    **/
    public Graphics graphics;
    
    
    /**
    *
    * The constructor.
    *
    **/    
    Ground( Shade shade, Polygon edge )
    {
        this.shade     = shade;
        this.edge      = edge;
        this.new_edge  = new Polygon();
        
        Rectangle rectangular_bounds = shade.getRectangularBounds();
        Polygon   polygonal_bounds   = shade.getPolygonalBounds();
        
        int min_index = 0;
        int npoints   = polygonal_bounds.npoints;
        for ( int i = 0; i<edge.npoints-1; i++ ) {
            double min_area = rectangular_bounds.area(); // Some large number
            for ( int j = 0; j<polygonal_bounds.npoints; j++ ) {
                Polygon tmp = new Polygon();
                tmp.addPoint( polygonal_bounds.xpoints[j], 
                              polygonal_bounds.ypoints[j] );
                tmp.addPoint( polygonal_bounds.xpoints[(j+1)%npoints], 
                              polygonal_bounds.ypoints[(j+1)%npoints] );
                tmp.addPoint( edge.xpoints[i],
                              edge.ypoints[i] );
                tmp.addPoint( edge.xpoints[(i+1)%npoints],
                              edge.ypoints[(i+1)%npoints] );
                if ( tmp.getRectangularBounds().area() < min_area ) {
                    min_index = j;
                    min_area = tmp.getRectangularBounds().area();
                }
            }
            new_edge.addPoint( polygonal_bounds.xpoints[min_index], 
                               polygonal_bounds.ypoints[min_index] );
            new_edge.addPoint( polygonal_bounds.xpoints[(min_index+1)%
                               polygonal_bounds.npoints], 
                               polygonal_bounds.ypoints[(min_index+1)%
                               polygonal_bounds.npoints] );
        }
        
        children = new Vector();
        
        children.addElement( shade );
        children.addElement( edge  );
    }

    /**
    *
    * Returns the angle of the Ground WRT the x axis in radians.
    * 
    **/
    public double
    getAngle()
    {
        return shade.getAngle();
    }


    /**
    *
    * Draw the Ground
    *
    **/
    public void
    paint()
    {
        if ( graphics == null ) // Draw only if g
            return;             // is valid
        graphics.setColor( Color.black );
        paint( graphics );
    }


    /**
    *
    * Draw the Ground
    *
    **/
    public void
    paint( Graphics g )
    {
        shade.paint( graphics );
        new_edge.paint( graphics );
    }
    
    
    /**
    *
    * This method is used to paint the original data points that
    * forms this GeometricObject
    *
    **/
    public void
    paintOriginal( Graphics g )
    {
        shade.paintOriginal( g );
        edge.paintOriginal( g );
    }


       /**
    *
    * Set graphics context for this Ground object.
    * Must be set at least once before doing any drawing.
    *
    **/
    public void
    setGraphicsContext( Graphics g )
    {
        graphics = g;
    }


    /**
    *
    * Implement Paintable
    *
    **/    
    public String
    getType()
    {
        return "ground";
    }
    
    
    /**
    *
    * This method should return the Rectangular bounds of this 
    * non-terminal object.
    *
    **/
    public Rectangle
    getRectangularBounds()
    {
        return shade.getRectangularBounds();
    }
    
    
    /**
    *
    * This method should return the polygonal array bounds of this 
    * non-terminal object. This is a rough approximation of the
    * constituents of the object. 
    *
    **/
    public Polygon[]
    getPolygonalBoundsArray()
    {
        Polygon[] polygonal_bounds_array = new Polygon[1];
        
        polygonal_bounds_array[0] = shade.getPolygonalBounds();
        
        return polygonal_bounds_array;
    }
    
    
    /**
    *
    * This method should return a vector containing the children of 
    * this object. Children are the Terminal or NonTerminal 
    * constituents of this object.
    *
    **/
    public Vector
    getChildren()
    {
        return children;
    }
}


/** 
  * 
  * $Log: Ground.java,v $
  * Revision 1.2  2000/06/03 01:52:33  mtsezgin
  * *** empty log message ***
  *
  * Revision 1.1  2000/05/26 20:49:46  mtsezgin
  *
  * This class represents the ground object found in mechanical
  * diagrams. (Unfortunately added to the system late).
  *
  * Revision 1.2  2000/04/20 03:59:52  mtsezgin
  * *** empty log message ***
  *
  * Revision 1.1  2000/04/13 06:16:02  mtsezgin
  *
  * Just started, and for now it filters out Shade objects using the timing data.
  *
  *  
  **/
  
