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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	WMScrew.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	Jul 98
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:     Mechanical engineering drawing recognition
//
// HISTORY:     Jul 98  archon  created
//
//              Dec 98  archon
//              Integrated new rec.geo changes.
//
////////////////////////////////////////////////////////////////////////////////

package rec.meche;

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

public class WMScrew extends WPart
{
  public WMScrew( GPoint center, double angle, double size, Cloneable data )
  {
    super( center, angle, size, data );
  }

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

  public Object clone()
  {
    return( new WMScrew( getCenter(), getAngle(), size(), getData() ) );
  }

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

  protected GShape[] create( double size, Cloneable data )
  {
    final double headWidth = size / 2.0;
    final double headLength = size / 5.0;
    final double tailWidth = size / 5.0;
    final double tailLength = size * 1.5;
    final double threadSeparation = 5.0;

    Assert.check( headWidth >= tailWidth,
      "screw heads much be at least as wide as screw tails" );

    int numberOfThreads = (int)Math.floor( Math.max( 0.0,
      tailLength / threadSeparation ) );

    GShape[] shapes = new GShape[7 + numberOfThreads];

    // Create screw head.

    shapes[0] = new GSegment( new GPoint( 0.0, 0.0 ),
                              new GPoint( headWidth, 0.0 ) );
    shapes[1] = new GSegment( new GPoint( 0.0, 0.0 ),
                              new GPoint( 0.0, headLength ) );
    shapes[2] = new GSegment( new GPoint( 0.0, headLength ),
                              new GPoint( headWidth,
                                          headLength ) );
    shapes[3] = new GSegment( new GPoint( headWidth, 0.0 ),
                              new GPoint( headWidth,
                                          headLength ) );

    // Create screw tail.

    double firstX = (headWidth - tailWidth) / 2.0;
    double secondX = (headWidth + tailWidth) / 2.0;
    double lowestY = headLength + tailLength;

    shapes[4] = new GSegment( new GPoint( firstX, headLength ),
                              new GPoint( firstX, lowestY ) );
    shapes[5] = new GSegment( new GPoint( secondX, headLength ),
                              new GPoint( secondX, lowestY ) );
    shapes[6] = new GSegment( new GPoint( firstX, lowestY ),
                              new GPoint( secondX, lowestY ) );

    // Create the threads.

    if( numberOfThreads > 0 ) {

      double currentY = headLength + threadSeparation;

      for( int i = 7; i < 7 + numberOfThreads; ++i ) {
        shapes[i] = new GSegment( new GPoint( firstX, currentY ),
                                  new GPoint( secondX, currentY ) );
        currentY += threadSeparation;
      }
    }

    return( shapes );
  }
}

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