function point_triangle_orientation_test ( )

%*****************************************************************************80
%
%% point_triangle_orientation_test() tests point_triangle_orientation().
%
%  Discussion:
%
%    The program
%    * reads a triangle T (defined by three points),
%    * determines the orientation of the triangle;
%    * computes and prints the length of the edges;
%    * reads a point P
%    * computes the signed distance from P to each line
%      (not the line segment!)
%    * reports whether the point is INSIDE, ON or OUTSIDE the triangle.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 July 2023
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'point_triangle_orientation_test():\n' );
  fprintf ( 1, '  Define a point P, \n' );
  fprintf ( 1, '  Define a triangle T with vertices P1, P2, P3.\n' );
  fprintf ( 1, '  Determine the orientation of the point with respect to T.\n' );
  fprintf ( 1, '\n' );

  for i = 1 : 5

    p1 = randn ( 2, 1 );
    p2 = randn ( 2, 1 );
    p3 = randn ( 2, 1 );

    fprintf ( 1, '\n' );
    fprintf ( 1, '  P1 = (%g,%g)\n', p1 );
    fprintf ( 1, '  P2 = (%g,%g)\n', p2 );
    fprintf ( 1, '  P3 = (%g,%g)\n', p3 );
%
%  Compute the edges.
%
    e1 = p2 - p1;
    e2 = p3 - p2;
    e3 = p1 - p3;
%
%  Compute the unit normal vectors to each edge.
%  MATLAB's NORM function returns the square root of the sum
%  of the squares of the entries of a vector.
%
    e1_nv = [ - e1(2); e1(1) ] / norm ( e1 );
    e2_nv = [ - e2(2); e2(1) ] / norm ( e2 );
    e3_nv = [ - e3(2); e3(1) ] / norm ( e3 );
%
%  Orientation test.
%  The signed distance of V3 from the line from V1 to V2 must be positive.
%
    test = e1_nv' * ( p3 - p1 );

    fprintf ( 1, '\n' );
    fprintf ( 1, '  Orientation test value = %f\n', test );

    if ( test < 0.0 )
      fprintf ( 1, '  The triangle is clockwise oriented.\n' );
      fprintf ( 1, '  Correct the orientation!\n' );
      temp = p1;
      p1 = p3;
      p3 = temp;
      temp = e1;
      e1 = e3;
      e3 = temp;
      temp = e1_nv;
      e1_nv = e3_nv;
      e3_nv = temp;
    elseif ( test == 0.0 )
      fprintf ( 1, '  Error!  The triangle is degenerate!\n' );
      continue
    else
      fprintf ( 1, '  The triangle is counter-clockwise oriented.\n' );
    end
%
%  Edge lengths.
%
    fprintf ( 1, '\n' );
    fprintf ( 1, '  Edge1 = p2-p1, length = %f\n', norm ( e1 ) );
    fprintf ( 1, '  Edge2 = p3-p2, length = %f\n', norm ( e2 ) );
    fprintf ( 1, '  Edge3 = p1-p3, length = %f\n', norm ( e3 ) );
    fprintf ( 1, '\n' );

    for j = 1 : 5

      p = randn ( 2, 1 );
%
%  Compute the signed distance from the point to the line that
%  includes each edge.
%
      p_e1_dist = e1_nv' * ( p - p1 );
      p_e2_dist = e2_nv' * ( p - p2 );
      p_e3_dist = e3_nv' * ( p - p3 );
      if ( p_e1_dist < 0.0 || p_e2_dist < 0.0 || p_e3_dist < 0.0 )
        fprintf ( 1, '  The point (%g,%g) is OUTSIDE the triangle\n', p );
      elseif ( p_e1_dist == 0.0 | p_e2_dist == 0.0 | p_e3_dist == 0.0 )
        fprintf ( 1, '  The point (%g,%g) is ON the triangle\n', p );
      else
        fprintf ( 1, '  The point (%g,%g) is INSIDE the triangle.\n', p );
      end

    end

  end
 
  return
end

