function alpha = circle_segment_rotation_from_chord ( r, c, p1, p2 )

%*****************************************************************************80
%
%% circle_segment_rotation_from_chord() computes the rotation of a circle segment.
%
%  Discussion:
%
%    Begin with a circle of radius R.  Choose two points P1 and P2 on the
%    circle, and draw the chord P1:P2.  This chord divides the circle
%    into two pieces, each of which is called a circle segment.
%    Consider one of the pieces.  The "angle" of this segment is the angle 
%    P1:C:P2, where C is the center of the circle.  Let Q be the point on 
%    the chord P1:P2 which is closest to C.  The "height" of the segment
%    is the distance from Q to the perimeter of the circle.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    12 January 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real R, the radius of the circle.
%    0 < R.
%
%    real C(2,1), the center of the circle.
%
%    real P1(2,1), P2(2,1), the ends of the chord.
%    Warning! If P1 = P2, we can't tell whether the segment is the whole
%    circle or none of it!
%
%  Output:
%
%    real ALPHA, the rotation of the circle segment.
%    0 <= ALPHA < 2 * PI.
%

%
%  Destroy all row vectors!
%
  c = c(:);
  p1 = p1(:);
  p2 = p2(:);
%
%  Compute the radial vectors V1 and V2.
%
  v1(1:2,1) = p1(1:2,1) - c(1:2,1);
  v2(1:2,1) = p2(1:2,1) - c(1:2,1);

  rho1 = atan2 ( v1(2), v1(1) );
  rho2 = atan2 ( v2(2), v2(1) );
%
%  Force RHO2 to be bigger than RHO1.
%
  while ( rho2 <= rho1 )
    rho2 = rho2 + 2.0 * pi;
  end
%
%  Compute THETA.
%
  theta = rho2 - rho1;
%
%  ALPHA is RHO1, plus half of the angular distance between P1 and P2.
%
  alpha = rho1 + 0.5 * theta;

  while ( 2.0 * pi <= alpha )
    alpha = alpha - 2.0 * pi;
  end

  return
end
