unit Step22;
//******************************************************************/
//*               (c)                      */
//*                -, 1991, 1996                     */
//*                 -   5.x                      */
//******************************************************************/
//*                                              */
//*                              */
//*
//* 1.                        - Intersect2Line       */
//* 2.                        - Intersect2Curve      */
//* 3.                  - IntersectLineSegArc  */
//* 4.                    - TanLinePointAndCircle*/
//* 5.                   - TanLineCircle        */
//* 6.                          - RotatePoint          */
//* 7.                        - SymmetryPoint        */
//* 8.      - Couplin2Lines        */
//* 9.                        - BuildPerpendicular   */
//*                                                                */
//******************************************************************/

interface

 procedure  LIBRARYENTRY( comm: WORD  ); Pascal;
 function   LIBRARYID   : Cardinal;      Pascal;
 function   LibToolBarId( barType : Integer; index : Integer ) : Integer; stdcall;

//-------------------------------------------------------------------------------
//   
// ---
 procedure  Intersect2Line;
 procedure  Intersect2Curve;
 procedure  IntersectLineSegArc;
 procedure  TanLinePointAndCircle;
 procedure  TanLineCircle;
 procedure  RotatePoint;
 procedure  SymmetryPoint;
 procedure  Couplin2Lines;
 procedure  BuildPerpendicular;

implementation
 uses  Sysutils, LibTool, Ltdefine;

const
 IDR_LIBID    = 2000; //  
 COMPACT_BAR1 = 2001; //   


//----------------------------------------------------------------------------------------
//   
//---
function   LIBRARYID : Cardinal;  pascal;
begin
  Result := IDR_LIBID;
end;

//----------------------------------------------------------------------------------------
//   
//---
procedure  LIBRARYENTRY( comm : Word  );  pascal;
begin
  if ( ksGetCurrentDocument( 1 ) <> 0 ) then
    case comm of
      1: Intersect2Line;        // 
      2: Intersect2Curve;       // 
      3: IntersectLineSegArc;   //   
      4: TanLinePointAndCircle; //  
      5: TanLineCircle;         //  
      6: RotatePoint;           // 
      7: SymmetryPoint;         // 
      8: Couplin2Lines;         //    
      9: BuildPerpendicular;    //
    end {case}
  else
    ksMessage( '   ' +#13#10+ '  /' );

end;

//-------------------------------------------------------------------------------
//      
// ---
function LibToolBarId( barType : Integer; //    ( 0 -  , 1 -    )
                       index   : Integer ) : Integer; stdcall; //  
begin
	if barType = 0 then
    if index = 0 then
      Result := COMPACT_BAR1
    else
     Result := -1
	else
		Result := -1;
end;

//----------------------------------------------------------------------------------------
//   
//---
procedure Intersect2Line;
var
  count: Integer; //  
  x, y : Double;  //  
  buf  : String;  //   
begin
  //  
  Line( 10,10, 0 );
  Line( 15, 5, 90 );

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

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

  //  
  buf := Format( 'count=%d xp=%6.2f yp=%6.2f ', [count, x, y ] );
  ksMessage( PChar(buf) );
end;

//----------------------------------------------------------------------------------------
//  
//---
procedure Intersect2Curve;
var
  rBezier1, rBezier2 : Reference;          //    
  i,                                       //  
  count    : Integer;                      //  
  xp, yp   : packed array[1..2] of Double; //   
  buf      : String;                       //   
begin
  Bezier(0, 0);           //   
    Point(20,  0, 0);     //     
    Point(10, 20, 0);
    Point(20, 40, 0);
    Point(30, 20, 0);
    Point(20,  0, 0);
  rBezier1 := 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 );
    rBezier2 := EndObj();
  DeleteMtr();            //    

  //  2- ,    : ,
  // , , , , , 
  IntersectCurvCurv( rBezier1, //    
                     rBezier2,
                     count,    //   
                     @xp, @yp, //   
                     2 );      //     

  //  
  buf := Format('count=%d', [count]);
  ksMessage(PChar(buf));

  for i := 1 to count do
  begin
    //   
    Point(xp[i], yp[i], 2);
    buf := Format('xp[%d]=%4.2f yp[%d]=%4.2f', [i, xp[i], i, yp[i]]);
    ksMessage(PChar(buf));
  end;

end;


//----------------------------------------------------------------------------------------
//    
//---
procedure IntersectLineSegArc;
var
  i, count : Integer; //  
  xp, yp   : Double2; //   
  buf      : String;
  a1,a2    : Double;  //     
begin
  // 
  LineSeg( 0, 40, 100, 40, 1);
  //       
  ArcByPoint( 50, 40, 20, 30, 40, 70, 40, 1, 1 );

  a1 := Angle( 50, 40, 30, 40 );  //     
  a2 := Angle( 50, 40, 70, 40 );

  IntersectLinSArc( 0, 40,        //   
                    100, 40,      //   
                    50, 40,       //  
                    20,           //  
                    a1, a2,       //     
                    1,            //  
                    count, xp, yp );

  //   
  for  i := 1 to  count do
    Point( xp[i], yp[i], 0 );

  //  
  buf := Format('count=%d xp[1]=%4.2f yp[1]=%4.2f xp[2]=%4.2f yp[2]=%4.2f',
                 [ count, xp[1], yp[1], xp[2], yp[2] ]);
  ksMessage( PChar(buf) );

end;

//----------------------------------------------------------------------------------------
//   
//---
procedure TanLinePointAndCircle;
var
  i, count : Integer; //  
  xp, yp   : Double2; //   
  buf      : String;
begin

  Point ( 10, 50, 3);      //
  Circle( 50, 10, 40, 1 ); //

  //      ,    
  TanLinePointCircle( 10, 50,     //   
                      50, 10, 40, //     
                      count,      //  
                      xp, yp );   //  

  //     
  for  i := 1 to  count do
  begin
    Point( xp[i], yp[i], 0 );
    Line( 10, 50, Angle( 10, 50, xp[i], yp[i] ) );
  end;

  //  
  buf := Format('count=%d xp[1]=%4.2f yp[1]=%4.2f xp[2]=%4.2f yp[2]=%4.2f',
                 [ count,xp[1],yp[1],xp[2],yp[2] ]);
  ksMessage( PChar(buf) );
end;

//----------------------------------------------------------------------------------------
//   
//---
procedure TanLineCircle;
var
  i      : Integer;
  xp, yp : Double2;
  buf    : String;

begin

  //  
  Circle( 50, 10, 40, 1 );

  //      ,    
  TanLineAngCircle( 50, 10,            // 
                    40,                // 
                    -45,               //  
                    xp, yp );          //  

  //     
  for i:= 1 to 2 do
  begin
    Point(xp[i],yp[i],1);
    Line(xp[i],yp[i], -45 );
  end;

  //  
  buf := Format('xp[1]=%4.2f yp[1]=%4.2f xp[2]=%4.2f yp[2]=%4.2f',
                 [ xp[1], yp[1], xp[2], yp[2] ]);
  ksMessage( PChar(buf) );

end;

//----------------------------------------------------------------------------------------
//  
//---
procedure RotatePoint;
var
  x, y  : Double; //   
  buf   : String;

begin
  //  
  Point( 60, 50, 0 );

  //     
  Point( 50, 50, 1 );

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

  //   
  Point( x, y, 2 );

  //  
  buf := Format('x=%4.2f y=%4.2f', [ x, y ]);
  ksMessage( PChar(buf) );

end;

//----------------------------------------------------------------------------------------
//  
//---
procedure SymmetryPoint;
var
  x, y : Double; //   
  buf  : String;

begin

  //  
  Point( 55, 60, 0 );

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

  Symmetry( 55, 60,   // 
            60, 50,   //   
            50, 50,   //   
            x, y );   //

  //   
  Point( x, y, 1 );

  //  
  buf := Format('x=%4.2f y=%4.2f', [ x, y ]);
  ksMessage( PChar(buf) );

end;

//----------------------------------------------------------------------------------------
//     
//---
procedure Couplin2Lines;
var
  i, count : Integer; //  
  con      : CON4;    //      (  CON :
                      //   ,    )
  rad      : Double;  //  
  buf      : String;
begin
  rad := 20;

  //  
  Line( 100, 100, 45 );
  Line( 100, 100, -45 );

  CouplingLineLine( 100, 100,  45, // 
                    100, 100, -45, // 
                    rad,           //  
                    count,         //  
                    con );         //     

  for i := 1 to  4 do
  begin
    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 );
  end;

  //  
  buf := Format( 'count = %d, con[1].x1 = %4.2f, con[1].y1 = %4.2f,' + #13#10 + 'con[1].x2 = %4.2f, con[1].y2 = %4.2f',
                  [count, (con[1].x1), con[1].y1, con[1].x2, con[1].y2] );
  ksMessage( PChar(buf) );
end;

//----------------------------------------------------------------------------------------
// 
//---
procedure BuildPerpendicular;
var
  x, y  : Double; //     
  buf   : String;
begin

//  
  Point( 50, 50, 0 );

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

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

  //      
  Point( x, y, 1 );

  //   
  buf := Format('x=%4.2f y=%4.2f', [ x, y ]);
  ksMessage( PChar(buf) );

end;

end.
