//////////////////////////////////////////////////////////////////////////////// 
// 
// step2.cpp - 
// 
// 1.                        - Intersect2Line
// 2.                        - Intersect2Curve
// 3.                  - IntersectLineSegArc
// 4.                    - TanLinePointCircle
// 5.                   - TanLineAngCircle
// 6.                          - RotatePoint
// 7.                        - SymmetryPoint
// 8.      - Couplin2Lines
// 9.                        - Perpendicular
// 
//////////////////////////////////////////////////////////////////////////////// 
#include "stdafx.h"
#include <afxdllx.h> 
#include "resource.h"

#include <libtool.h>
#include <ldefin2d.h>
#include <ksConstants.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


//-------------------------------------------------------------------------------
//       DLL
// ---
static AFX_EXTENSION_MODULE StepDLL = { NULL, NULL };


//-------------------------------------------------------------------------------
//   
//    DLL
// ---
extern "C" int APIENTRY
DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved )
{
  UNREFERENCED_PARAMETER( lpReserved );

  if ( dwReason == DLL_PROCESS_ATTACH )
  {
    TRACE0( "DLL Initializing!" );
 
    if ( !AfxInitExtensionModule( StepDLL, hInstance ) )
      return 0;

    new CDynLinkLibrary( StepDLL );
  }
  else if ( dwReason == DLL_PROCESS_DETACH )
  {
    TRACE0( "DLL Terminating!" );
    AfxTermExtensionModule( StepDLL );
  }
  return 1;
}


//-------------------------------------------------------------------------------
//   
// ---
unsigned int WINAPI LIBRARYID()
{
  return IDR_LIBID;
}


//-------------------------------------------------------------------------------
//      
// ---
int WINAPI LibToolBarId( int barType, //    ( 0 -  , 1 -    ) 
                         int index )  //  
{
	if ( !barType )
    return !index ? COMPACT_BAR1 : -1;
	else 
		return -1; 
}

//-----------------------------------------------------------------------------
//       
// ---
int WINAPI LibraryBmpBeginID( unsigned int bmpSizeType )
{
  int res = 0;
  switch ( bmpSizeType )
  {
    case ksBmp1616: res = 1000; break;
    case ksBmp2424: res = 2000; break;
//    case ksBmp3232: res = 3000; break;
	  case ksBmp4848: res = 4000; break;
  }
  return res;
}

//-------------------------------------------------------------------------------
//   
// ---
void Intersect2Line();
void Intersect2Curve();
void IntersectLineSegArc();
void TanLinePointCircle();
void TanLineAngCircle();
void RotatePoint();
void SymmetryPoint();
void Couplin2Lines();
void Perpendicular();


//-------------------------------------------------------------------------------
//   
// ---
void WINAPI LIBRARYENTRY( unsigned int comm )
{
  if ( ksGetCurrentDocument( 1 ) ) //   2D 
  {
    switch ( comm )
    {
      case 1 : Intersect2Line();      break; //  
      case 2 : Intersect2Curve();     break; //  
      case 3 : IntersectLineSegArc(); break; //    
      case 4 : TanLinePointCircle();  break; //   
      case 5 : TanLineAngCircle();    break; //   
      case 6 : RotatePoint();         break; //  
      case 7 : SymmetryPoint();       break; //  
      case 8 : Couplin2Lines();       break; //     
      case 9 : Perpendicular();       break; // 
    }
  }
  else
    Message( "   \n  /" );
}


//-------------------------------------------------------------------------------
//  
// ---
void Intersect2Line()
{
  //  
  Line( 10, 10, 0  );
  Line( 15, 5,  90 );

  int count;   //  
  double x, y; //  
 
  //      
  IntersectLinLin( 10, 10,   //    
                   0,        //   
                   15, 5,    //    
                   90,       //   
                   &count,   //   
                   &x, &y ); //  

  //   
  if ( count )
    Point( x, y, 0 );

  //  
  char buf[255];
  sprintf( buf, "count = %d, x = %4.2f, y = %4.2f", count, x, y );
  Message( buf );
}


//-------------------------------------------------------------------------------
//  
// ---
void Intersect2Curve()
{
  Bezier( 0, 0 ); //   
    Point( 0,  10, 0 ); //     
    Point( 10, 20, 0 );
    Point( 20, 10, 0 );
    Point( 10, 0,  0 );
    Point( 0,  10, 0 );
  reference rBezier1 = EndObj(); //  EndObj       

  Mtr( 0, 10, 0, 1 ); //    ( 10   OY )
    Bezier( 0, 0 );   
      Point( 0,  10, 0 ); //     
      Point( 10, 20, 0 );
      Point( 20, 10, 0 );
      Point( 10, 0,  0 );
      Point( 0,  10, 0 );
    reference rBezier2 = EndObj(); 
  DeleteMtr(); //    
 
  int count;         //  
  double x[2], y[2]; //   

  //  2- ,    : , 
  // , , , , , , 
  IntersectCurvCurv( rBezier1, //    
                     rBezier2, 
                     &count,   //   
                     x, y,     //   
                     2 );      //     

  //   
  for ( int i = 0; i < count; i++ )
    Point( x[i], y[i], 0 );
 
  //  
  char buf[255];
  sprintf( buf, "count = %d, x[0] = %4.2f, y[0] = %4.2f, x[1] = %4.2f, y[1] = %4.2f", 
           count, x[0], y[0], x[1], y[1] );
  Message( buf );
}


//-------------------------------------------------------------------------------
//    
// ---
void IntersectLineSegArc()
{
  //  
  LineSeg( 0, 40, 100, 40, 1 );

  //       
  ArcByPoint( 50, 40, 20, 30, 40, 70, 40, 1, 1 );

  int count;         //  
  double x[2], y[2]; //   

  //       
  IntersectLinSArc( 0,   40,                 //   
                    100, 40,                 //   
                    50,  40,                 //  
                    20,                      //  
                    Angle( 50, 40, 30, 40 ), //     
                    Angle( 50, 40, 70, 40 ), 
                    1,                       //  
                    &count,                  //   
                    x, y );                  //   

  //   
  for ( int i = 0; i < count; i++ )
    Point( x[i], y[i], 0 );

  //  
  char buf[255];
  sprintf( buf, "count = %d, x[0] = %4.2f, y[0] = %4.2f, x[1] = %4.2f, y[1] = %4.2f", 
           count, x[0], y[0], x[1], y[1] );
  Message( buf );
}


//-------------------------------------------------------------------------------
//   
// ---
void TanLinePointCircle()
{
  //  
  Point( 10, 50, 0 );
 
  //  
  Circle( 50, 10, 40, 1 );

  int count;         //  
  double x[2], y[2]; //   
 
  //      ,    
  TanLinePointCircle( 10, 50,      //   
                      50, 10, 40,  //     
                      &count,      //  
                      x, y );      //   

  //     
  for ( int i = 0; i < count; i++ )
  {
    Point( x[i], y[i], 0 );
    Line( 10, 50, Angle( 10, 50, x[i], y[i] ) );
  }

  //  
  char buf[255];
  sprintf( buf, "count = %d, x[0] = %4.2f, y[0] = %4.2f, x[1] = %4.2f, y[1] = %4.2f", 
           count, x[0], y[0], x[1], y[1] );
  Message( buf );
}


//-------------------------------------------------------------------------------
//   
// ---
void TanLineAngCircle()
{
  //  
  LineSeg( 0, 40, 100, 40, 1 );
 
  //  
  Circle( 50, 10, 40, 1 );

  double x[2], y[2]; //  
 
  //      ,    
  TanLineAngCircle( 50, 10, 40, //     
                    45,         //   
                    x, y );   //  
 
  //     
  Point( x[0], y[0], 0 );
  Line( x[0], y[0], 45 );

  //  
  char buf[255];
  sprintf( buf, "x = %4.2f, y = %4.2f", x[0], y[0] );
  Message( buf );
}


//-------------------------------------------------------------------------------
//  
// ---
void RotatePoint()
{
  //  
  Point( 60, 50, 0 );

  //  
  Point( 50, 50, 1 );

  double x, y; //   

  //    
  Rotate( 60, 50,   //  
          50, 50,   //  
          180,      //  
          &x, &y ); //  

  //   
  Point( x, y, 2 );
 
  //  
  char buf[255];
  sprintf( buf, "x = %4.2f, y = %4.2f", x, y );
  Message( buf );
}


//-------------------------------------------------------------------------------
//  
// ---
void SymmetryPoint()
{
  //  
  Point( 55, 60, 0 );

  //  
  LineSeg( 60, 50, 50, 50, 1 );

  double x, y; //   

  //   ,    
  Symmetry( 55, 60,   //  
            60, 50,   //   
            50, 50,   //   
            &x, &y ); //  
 
  //   
  Point( x, y, 1 );
 
  //  
  char buf[255];
  sprintf( buf, "x = %4.2f, y = %4.2f", x, y );
  Message( buf );
}


//-------------------------------------------------------------------------------
//     
// ---
void Couplin2Lines()
{
  //  
  Line( 100, 100, 45 );
  Line( 100, 100, -45 );

  double rad = 20; //  
  int count;       //  
  CON con[4];      //      (  CON :
                   //   ,    ) 
 
  //   ,    
  CouplingLineLine( 100, 100, 45,  //  
                    100, 100, -45, //  
                    rad,           //  
                    &count,        //  
                    con );         //     

  //      
  for ( int i = 0; i < count; i++ )
  {
    Circle( con[i].xc, con[i].yc, rad, 2 );
    Point( con[i].x1, con[i].y1, i );
    Point( con[i].x2, con[i].y2, i );
  }

  //  
  char buf[255];
  sprintf( buf, "count = %d, con[0].x1 = %4.2f, con[0].y1 = %4.2f,\ncon[0].x2 = %4.2f, con[0].y2 = %4.2f ...", 
           count, con[0].x1, con[0].y1, con[0].x2, con[0].y2 );
  Message( buf );
}


//-------------------------------------------------------------------------------
// 
// ---
void Perpendicular()
{
  //  
  Point( 50, 50, 0 );

  //  
  LineSeg( 60, 10, 100, 10, 1 );

  double x, y; //     
 
  //      
  Perpendicular( 50, 50,   //    
                 60, 10,   //    
                 100, 10,  //    
                 &x, &y ); //      

  //      
  Point( x, y, 1 );

  //   
  char buf[255];
  sprintf( buf, "x = %4.3f, y = %4.3f", x, y );
  Message( buf );
}
