=head1 NAME

IntersectTool -- find the intersection of geodesics

=head1 SYNOPSIS

B<IntersectTool> [ B<-c> | B<-n> | B<-i> | B<-o> |
[ B<-R> I<maxdist> ]
[ B<-e> I<a> I<f>] [ B<-E> ]
[ B<-w> ] [ B<-p> I<prec> ]
[ B<--comment-delimiter> I<commentdelim> ]
[ B<--version> | B<-h> | B<--help> ]
[ B<--input-file> I<infile> | B<--input-string> I<instring> ]
[ B<--line-separator> I<linesep> ]
[ B<--output-file> I<outfile> ]

=head1 DESCRIPTION

B<IntersectTool> finds the intersection of two geodesics I<X> and
I<Y>.  The geodesics may either be specified as a location and an
azimuth, I<latX> I<lonX> I<aziX>, or as the shortest geodesic segment
between two locations, I<latX1> I<lonX1> and I<latX2> I<lonX2>.  The
intersection is then specified as the displacements, I<x> and I<y>,
along the geodesics I<X> and I<Y> from the starting points to the
intersection.  In the case of the intersection of geodesic segments,
the starting point is first point specified for I<X> or I<Y>.

Usually this tool returns the I<closest> intersection defined as the
one that minimizes the "L1" distance, |I<x>| + |I<y>|.  However, it is
possible to specify an "origin" I<x0> and I<y0> when determining
closeness so that the intersection which minimizes |I<x> - I<x0>| +
|I<y> - I<y0>| is returned.

In the case of intersecting segments the origin is taken to be the
midpoints of the segments; I<x0> is half the distance from I<X1> to
I<X2>.  In addition a flag is returned specifying whether the
intersection is "within" the segments.

The tool also returns a "coincidence indicator" I<c>.  This is
typically 0.  However if the geodesics lie on top of one another at
the point of intersection, then I<c> is set to 1, if they are
parallel, and -1, if they are antiparallel.

B<IntersectTool> operates in one of three modes:

=over

=item 1.

With the B<-c> option (the default), B<IntersectTool> accepts lines on
the standard input containing I<latX> I<lonX> I<aziX> I<latY> I<lonY>
I<aziY>, specifying two geodesic lines I<X> and I<Y>, and prints the
location of the closest intersection I<x> I<y> I<c> on standard output.

=item 2.

With the B<-n> option, B<IntersectTool> accepts lines on the standard
input containing I<latX> I<lonX> I<aziX> I<aziY> I<aziY>, specifying a
point where two geodesic lines I<X> and I<Y> intersect, and prints the
location of the next closest intersection I<x> I<y> I<c> on standard
output.

=item 3.

With the B<-i> option, B<IntersectTool> accepts lines on the standard
input containing I<latX1> I<lonX1> I<latX2> I<lonX2> I<latY1> I<lonY1>
I<latY2> I<lonY2>, specifying two geodesic segments I<X1>-I<X2> and
I<Y1>-I<Y2>, and prints I<x> I<y> I<c> I<k> on standard output.  Here
I<k> is a flag in [-4,4] specifying whether the intersection is within
the segments (0) or not (non-zero).  I<x> and I<y> give the distances
from I<X1> and I<Y1> respectively.  I<k> is set to 3 I<kx> + I<ky>
where I<kx> = -1 if I<x> < 0, 0 if 0 <= I<x> <= I<sx>, 1 if I<sx> <
I<x>, and similarly for I<ky>; I<sx> is the length of the segment
I<X1>-I<X2>.

=item 4.

With the B<-o> option, B<IntersectTool> accepts lines on the standard
input containing I<latX> I<lonX> I<aziX> I<latY> I<lonY> I<aziY> I<x0>
I<y0>, specifying two geodesic lines I<X> and I<Y> and two offsets,
and prints I<x> I<y> I<c> on standard output where [I<x>, I<y>] is the
intersection closest to [I<x0>, I<y0>].

=back

=head1 OPTIONS

=over

=item B<-c>

find the closest intersection (see 1 above).

=item B<-n>

find the intersection closest to a given intersection (see 2 above).

=item B<-i>

find the intersection of two geodesic segments (see 3 above).

=item B<-o>

find the closest intersection with an offset.

=item B<-R> I<maxdist>

modifies the four modes to return all the intersections within an L1
distance, I<maxdist>, of the relevant origin: [0, 0] for B<-c> and
B<-n>, the midpoints of the segments for B<-i>, and [I<x0>, I<y0>] for
B<-o>.  For each intersection, I<x> I<y> I<c> I<z> is printed on
standard output.  Here I<z> is the L1 distance of the intersection
from the origin and the intersections are sorted by the distances.  A
line "nan nan 0 nan" is added after the intersections, so that the
output can be associated with the correct lines of the input.  The
number of intersections scales as (I<maxdist>/(pi I<a>))^2.

=item B<-C>

check the intersections.  For each computed intersection, print on
B<standard error> a line I<latX> I<lonX> I<latY> I<lonY> I<sXY> giving
the computed positions of the intersections points on I<X> and I<Y>
and the distance between them.  If B<-w> is specified, the longitude
is given before the latitude.

=item B<-e> I<a> I<f>

specify the ellipsoid via the equatorial radius, I<a> and
the flattening, I<f>.  Setting I<f> = 0 results in a sphere.  Specify
I<f> E<lt> 0 for a prolate ellipsoid.  A simple fraction, e.g., 1/297,
is allowed for I<f>.  By default, the WGS84 ellipsoid is used, I<a> =
6378137 m, I<f> = 1/298.257223563.

=item B<-E>

use "exact" algorithms (based on elliptic integrals) for the geodesic
calculations.  These are more accurate than the (default) series
expansions for |I<f>| E<gt> 0.02.

=item B<-w>

on input, longitude precedes latitude (except that on input this can
be overridden by a hemisphere designator, I<N>, I<S>, I<E>, I<W>).

=item B<-p> I<prec>

set the output precision to I<prec> (default 3); I<prec> is the
precision relative to 1 m.  See L</PRECISION>.

=item B<--comment-delimiter> I<commentdelim>

set the comment delimiter to I<commentdelim> (e.g., "#" or "//").  If
set, the input lines will be scanned for this delimiter and, if found,
the delimiter and the rest of the line will be removed prior to
processing and subsequently appended to the output line (separated by a
space).

=item B<--version>

print version and exit.

=item B<-h>

print usage and exit.

=item B<--help>

print full documentation and exit.

=item B<--input-file> I<infile>

read input from the file I<infile> instead of from standard input; a file
name of "-" stands for standard input.

=item B<--input-string> I<instring>

read input from the string I<instring> instead of from standard input.
All occurrences of the line separator character (default is a semicolon)
in I<instring> are converted to newlines before the reading begins.

=item B<--line-separator> I<linesep>

set the line separator character to I<linesep>.  By default this is a
semicolon.

=item B<--output-file> I<outfile>

write output to the file I<outfile> instead of to standard output; a
file name of "-" stands for standard output.

=back

=head1 INPUT

B<IntersectTool> measures all angles in degrees and all lengths in
meters.  On input angles (latitude, longitude, azimuth) can be as
decimal degrees or degrees, minutes, seconds.  For example, C<40d30>,
C<40d30'>, C<40:30>, C<40.5d>, and C<40.5> are all equivalent.  By
default, latitude precedes longitude for each point (the B<-w> flag
switches this convention); however either may be given first by
appending (or prepending) I<N> or I<S> to the latitude and I<E> or
I<W> to the longitude.  Azimuths are measured clockwise from north;
however this may be overridden with I<E> or I<W>.

For details on the allowed formats for angles, see the C<GEOGRAPHIC
COORDINATES> section of GeoConvert(1).

=head1 PRECISION

I<prec> gives precision of the output with I<prec> = 0 giving 1 m
precision, I<prec> = 3 giving 1 mm precision, etc.  I<prec> is the
number of digits after the decimal point for lengths.  The latitude
and longitude printed to standard error with the B<-C> option are
given in decimal degrees with I<prec> + 5 digits after the decimal
point.  The minimum value of I<prec> is 0 and the maximum is 10.

=head1 ERRORS

An illegal line of input will print an error message to standard output
beginning with C<ERROR:> and causes B<IntersectTool> to return an exit code
of 1.  However, an error does not cause B<IntersectTool> to terminate;
following lines will be converted.

=head1 ACCURACY

This tool will give nearly full double precision accuracy for |I<f>|
E<lt> 0.02.  If the B<-E> option is given, full accuracy is achieved
for -1/4 E<lt> f E<lt> 1/5.  The tool had not been tested outside this
range.

=head1 EXAMPLES

A vessel leaves Plymouth 50N 4W on a geodesic path with initial
heading 147.7W.  When will it first cross the equator?

   echo 50N 4W 147.7W 0 0 90 | IntersectTool -c -p 0 -C

   6058049 -3311253 0
   0.00000 -29.74549 -0.00000 -29.74549 0

Answer: after 6058km at longitude 29.7W.  When will it cross the date
line, longitude 180E?  Here we need to use B<-R> because there a
closer intersection on the prime meridian:

   echo 50N 4W 147.7W 0 180 0 | IntersectTool -c -p 0 -C -R 2.6e7

   -494582 14052230 0 14546812
   53.69260 0.00000 53.69260 0.00000 0
   19529110 -5932344 0 25461454
   -53.51867 180.00000 -53.51867 180.00000 0
   nan nan 0 nan
   nan nan nan nan nan

We want the second result: after 19529 km at latitude 53.5S.

=head1 SEE ALSO

GeoConvert(1), GeodSolve(1).

This solution for intersections is described in C. F. F. Karney,
I<Geodesic intersections>, J. Surveying Eng. B<150>(3), 04024005:1-9
(2024), DOI: L<https://doi.org/10.1061/JSUED2.SUENG-1483>; preprint
L<https://arxiv.org/abs/2308.00495>.  It is based on the work of
S. Baseldga and J. C. Martinez-Llario, I<Intersection and
point-to-line solutions for geodesics on the ellipsoid>,
Stud. Geophys. Geod. B<62>, 353-363 (2018); DOI:
L<https://doi.org/10.1007/s11200-017-1020-z>;

=head1 AUTHOR

B<IntersectTool> was written by Charles Karney.

=head1 HISTORY

B<IntersectTool> was added to GeographicLib,
L<https://geographiclib.sourceforge.io>, in version 2.3.
