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


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

import java.util.Vector;

/**
  *
  * A basic Cross with a two perpendicular lines.
  *
  **/

class      Cross
implements Rotatable,
           NonTerminal
{
    /**
    *
    * Original l2.
    *
    **/
    private Line original_l2;
    
    /**
    *
    * First line of the cross.
    *
    **/
    private Line l1;
    
    /**
    *
    * Modified l2 to be perpendicular to l1.
    *
    **/
    private Line l2;

    /**
    *
    * children
    *
    **/
    private Vector children;

    /**
    *
    * Graphics context for this Geometric object.
    *
    **/
    public Graphics graphics;
    
    
    /**
    *
    * The constructor.
    *
    **/    
    Cross( Line l1, Line l2 )
    {
        this.l1          = l1;
        this.l2          = new Line();
        this.original_l2 = l2;

        double center_x = (l1.x1 + l1.x2)/2;
        double center_y = (l1.y1 + l1.y2)/2;
        double dx       = (l1.x1 - l1.x2)/2;
        double dy       = (l1.y1 - l1.y2)/2;
        
        this.l2.x1 = center_x + dy;
        this.l2.y1 = center_y - dx;
        this.l2.x2 = center_x - dy;
        this.l2.y2 = center_y + dx;

        children = new Vector();
        
        children.addElement( l1 );
        children.addElement( l2 );
    }

    /**
    *
    * Returns the angle of the motor WRT the x axis in radians.
    * This should be made more general by adding an angle field 
    * to the class.Take the angle on the positive x side. Angle 
    * is always between 0 and PI/2;
    *
    **/
    public double
    getAngle()
    {
        double dy = Math.abs((l1.y1 - l1.y2)/2);
        if ( dy == 0.0 )
            return 0.0;
            
        // Take the angle on the positive x side.
        // Angle is always between 0 and PI/2;
        return Math.atan2( dy, 1 );
    }


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


    /**
    *
    * Draw the Cross
    *
    **/
    public void
    paint( Graphics g )
    {
        l1.paint( g );
        l2.paint( g );
    }


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


    /**
    *
    * Implement Paintable 
    *
    **/    
    public String
    getType()
    {
        return "cross";
    }
    
    
    /**
    *
    * This method is used to paint the original data points that
    * forms this GeometricObject
    *
    **/
    public void
    paintOriginal( Graphics g )
    {
        l1.paintOriginal( g );
        original_l2.paintOriginal( g );
    }
    
    
    /**
    *
    * This method should return the Rectangular bounds of this 
    * non-terminal object.
    *
    **/
    public Rectangle
    getRectangularBounds()
    {
        return (l1.getRectangularBounds()).union( l1.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[2];
        
        polygonal_bounds_array[0] = l1.getPolygonalBounds();
        polygonal_bounds_array[1] = l2.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;
    }
}
