function [ p4, flag ] = line_exp_perp_2d ( p1, p2, p3 )

%*****************************************************************************80
%
%% line_exp_perp_2d() computes a line perpendicular to a line and through a point.
%
%  Discussion:
%
%    The explicit form of a line in 2D is:
%
%      ( P1, P2 ) = ( (X1,Y1), (X2,Y2) ).
%
%    The input point P3 should NOT lie on the line (P1,P2).  If it
%    does, then the output value P4 will equal P3.
%
%    P1-----P4-----------P2
%            |
%            |
%           P3
%
%    P4 is also the nearest point on the line (P1,P2) to the point P3.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 July 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real P1(1,2), P2(1,2), two points on the line.
%
%    real P3(1,2), a point (presumably not on the
%    line (P1,P2)), through which the perpendicular must pass.
%
%  Output:
%
%    real P4(1,2), a point on the line (P1,P2),
%    such that the line (P3,P4) is perpendicular to the line (P1,P2).
%
%    logical FLAG: true if the value could not be computed.
%
  bot = sum ( ( p2(1,1:2) - p1(1,1:2) ).^2 );

  if ( bot == 0.0 )
    p4(1,1:2) = Inf;
    flag = true;
  end
%
%  (P3-P1) dot (P2-P1) = Norm(P3-P1) * Norm(P2-P1) * Cos(Theta).
%
%  (P3-P1) dot (P2-P1) / Norm(P3-P1)^2 = normalized coordinate T
%  of the projection of (P3-P1) onto (P2-P1).
%
  t = sum ( ( p1(1,1:2) - p3(1,1:2) ) ...
    .* ( p1(1,1:2) - p2(1,1:2) ) ) / bot;

  p4(1,1:2) = p1(1,1:2) + t * ( p2(1,1:2) - p1(1,1:2) );
  flag = false;

  return
end
