\section{\Sec2. The algorithm}
% Begin
Like the Bentley-Ottman algorithm and many other algorithms in computational geometry, we use the ``sweeping line'' technique. Think of the line segments (which we from now on denote simply as ``segments'') residing in the plane as a vertical line sweeps across the plane from left to right. At any time, the sweep line intersects some subset of the segments and defines an {\it above/below ordering} on them. This ordering changes when endpoints of the segments are ``hit'' by the sweep line, inserting or deleting them from the ordering depending on whether the endpoint is a {\it left endpoint} or a {\it right endpoint.} (Vertical segments present a degenerate case of this definition, but in most cases we can arbitrarily denote one of the endpoints of a vertical segment as the ``left'' and the other as the ``right.'') The ordering also changes when the sweep line moves over an intersection point, where two adjacent lines switch position (see figure \Fig1).
\capfig 5cm{Figure \Fig1. Four positions of the sweep line with related orderings.}
Our algorithm, however, dynamically maintains {\it two} above/below orderings, one for the segments in \ss\ and another for those in \ts . Since there are no \ss -\ss\ or \ts -\ts\ intersections, these orderings only change when the sweep line encounters an endpoint of a segment, depicted in positions (1)-(3) of figure \Fig1. Note that these above/below orderings contain only those segments currently intersecting the sweep line. We refer to those segments as {\it active segments.} We can implement these orderings as balanced trees, with the leaves of a tree (representing segments in the ordering) doubly linked in a list. The orderings can then be maintained in $\Oh(\log n)$ time per insertion or deletion, and in $\Oh (n\log n)$ time overall as the sweep line processes all the endpoints. Furthermore, the doubly-linked list lets us traverse an ordering in constant time per ``step'' once an initial position has been given from which to start the traversal.
A segment $r\in\Sscr\uni\Tscr$ is thus inserted in the appropriate tree when the sweep line encounters its left endpoint, and deleted when we encounter the right one. Just before deleting $r$, we could in principle detect and report all its intersections with segments currently in the opposite tree. It is easy to see that, in this way, we would be sure to detect every \ss-\ts\ intersection exactly once. The problem with this idea is that the trivial implementation (test the segment $r$ against all segments in the other tree) could take $\Omega (n^2)$ time in the worst case, even if we had no intersections at all.
When the right endpoint of $s\in\Sscr$ is processed, we can easily locate in the \ts-ordering the two segments that are immediately above and below $s$. It would be nice if all segments intersected by $s$ were consecutive elements of the current \ts-ordering, starting with one of these two neighbors:
\capfig 4cm{Figure \Fig2. Easy intersection detection in an ideal world.}
If this were always the case, then all we would have to do is to start at these two segments and proceed away from $s$, both upwards and downwards in the \ts -ordering, testing each segment for intersection with $s$ and terminating the search at the first failure in each direction. The time required for this search would then be $\Oh (1)$ for each reported intersection (i.e., for each successful test), plus $\Oh (\log n)$ for each endpoint (this includes the time to locate the endpoint of $s$ among the segments of \ts and to perform the two final, failing tests of the search). The total time then would be the optimal one, $\Oh (n\log n + i).$
Unfortunately, there are situations for which the list \ts\ will contain ``shielding'' segments disjoint from $s$ mixing with the intersecting ones. For example, when the endpoint of $s1$ is hit in the figure below,
\capfig 4cm{hfil Figure \Fig3. How shielding segments mess things up.}
the \ts -ordering would contain segments $t1$ to $t7$ (in order from top to bottom); the segment $t3$ would be shielding $t1$ and $t2$ from $s1$, and $t6$ would be shielding $t7$.
A segment $r\in\Sscr\uni\Tscr$ can ``shield'' an \ss -\ts\ intersection, i.e., it can prevent its detection by the linear search described above, only if the left endpoint $p$ of $r$ sits in a {\it cone}, the region bounded above and below by two intersecting segments $s\in\Sscr$ and $t\in\Tscr$ and extending to the right of their intersection. We call this an \ss -\ts\ {\it cone} if the upper side of the cone is a segment in \ss\ and the lower side is a segment in \ts , otherwise we call it a \ts -\ss\ {\it cone.} Notice that cones are always formed by active segments. When inserting $p$, the algorithm detects and destroys every cone $(s,t)$ containing $p$ (respectively, every cone $(t,s)$ containing $p$) by splitting the lower segment of the cone $t$ (respectively, $s$) at the sweep line into two parts, and deleting the left part, while reporting any intersections of segments with the left part (see figure \Fig4).
\capfig 4cm{\hfil Figure \Fig4. The splitting procedure.}
After this operation, $r$ no longer shields any intersections. The above {\it splitting procedure} ensures that {\it no left endpoint of a currently active segment begins in an \ss - \ts\ cone.} This condition will be called the {\it Main Invariant,} and it implies that there are no shielding segments in the current orderings. Note that the splitting procedure ``readjusts'' the left endpoint of a segment forming the lower part of a cone. This new left endpoint may then be in another cone, and this cone must be subsequently split.
We now present some important facts about the Main Invariant and the adjustment procedure described above.
\lemma{\Th{\Sec2.1}}{If the Main Invariant has been maintained and we are about to insert a segment $r$ into its ordering, the left endpoint $p$ of $r$ cannot be enclosed in an \ss -\ts\ cone {\rm and} a \ts -\ss\ cone.}
\proof{Suppose not; let $(s,t)$ and $(t\pr,s\pr)$ be the cones enclosing $p$. Without loss of generality, let the intersection $(s,t)$ occur to the left of $(t\pr , s\pr )$. Since $s\pr$ is below $s$ at the current position of the sweep line, and $s$ and $s\pr$ do not intersect, we conclude that $s\pr$ is below $s$ at any earlier position of the sweep line where $s$ and $s\pr$ were active segments. Similarly, segment $t$ is below $t\pr$ whenever the sweep line intersects both of them.
\pp If $t\pr$ is above $s$ and $s\pr$ is below $t$, then intersection $(s,t)$ can be to the left of intersection $(t\pr,s\pr)$ only if $s$ intersects $s\pr$, or $t$ intersects $t\pr$, both of which are impossible. Therefore, either (i) $t\pr$ is between $s$ and $p$ at the sweep line (illustrated in figure \Fig5), or (ii) $s\pr$ is between $p$ and $t$ at the sweep line.
\capfig 4cm{\hfil Figure \Fig5.}
In case (i), $t\pr$ must intersect $s$, otherwise the left endpoint of $t\pr$ lies in the cone formed by $s$ and $t$, violating the Main Invariant. If $s\pr$ intersects $t\pr$ to the left of intersection $(s,t\pr)$, the intersection is either to the left of intersection $(s,t)$, or $s\pr$ intersects $s$, both of which are impossible. Therefore $s\pr$ intersects $t\pr$ to the right of intersection $(s,t\pr)$, in which case either $s$ intersects $s\pr$, or the left endpoint of $s\pr$ lies in the cone formed by $s$ and $t\pr$, violating the Main Invariant. Hence case (i) is impossible.
\pp Case (ii) is symmetric to case (i) and is also impossible. Thus $p$ cannot lie in an \ss-\ts\ cone and a \ts-\ss\ cone.}
\lemma{\Th{\Sec2.2}}{Assume the Main Invariant has been maintained, and we are about to insert a segment $r$ with left endpoint $p$. If in inserting $r$, $p$ lies in an \ss-\ts\ cone formed by $s$ and $t$, $p\pr$ is the point on $t$ just below $p$, and $p\pr$ is enclosed in a cone, it must be an \ss-\ts\ cone. (A symmetric lemma holds for \ts-\ss\ cones.)}
\proof{Suppose $p\pr$ is enclosed in a \ts-\ss\ cone formed by $t\pr$ and $s\pr$. We know $p$ must lie above $t\pr$, otherwise lemma \Th{\Sec2.1} is contradicted. Segment $t\pr$ must then be above $p\pr$, as in figure \Fig6:
\capfig 4cm{\hfil Figure \Fig6.}
Now for $p\pr$ to be in the cone formed by $t\pr$ and $s\pr$, $s\pr$ must either cross $s$, or the left endpoint of $s\pr$ must lie in the cone formed by $s$ and $t$, both of which are impossible.}
\lemma{\Th{\Sec2.3}}{Assume the Main Invariant has been maintained, and we are about to insert a segment $r$ with left endpoint $p$. If $p$ lies in an \ss-\ts\ cone, segment $s\pr$ is immediately above $p$ in the \ss-ordering, and $t\pr$ is immediately below $p$ in the \ts-ordering, then $p$ lies in a cone formed by $s\pr$ and $t\pr$. (Again, similarly for \ts-\ss\ cones.)}
\proof{If not, then $s\pr$ intersects $s$, $t\pr$ intersects $t$, or the Main Invariant has been violated by the left endpoints of $s\pr$ or $t\pr$. This means that cones enclosing $p$ must be ``close to'' $p$.}
\lemma{\Th{\Sec2.4}}{Assume the Main Invariant has been maintained, and we are about to insert a segment $r$ with left endpoint $p$. If $p$ lies in an \ss-\ts\ cone where $s$ is immediately above $p$ in the \ss-ordering and $t$ is immediately below $p$ in the \ts-ordering, $p\pr$ is the point on $t$ immediately below $p$, and $p\pr$ is not enclosed in a cone, that $p$ is enclosed only in that single \ss-\ts\ cone.}
\proof{Immediate.}
\lemma{\Th{\Sec2.5}}{Assume the Main Invariant has been maintained, and we are about to insert a segment $r$ with left endpoint $p$. If $p$ lies in an \ss-\ts\ cone formed by its immediate neighbors $s\in\Sscr$ and $t\in\Tscr$, then the set $\rset{s\pr \relv s\pr\hbox{\ intersects\ } t}$ forms a consecutive sequence of segments in the \ss-ordering.}
\proof{If the segments $\rset{s\pr\relv s\pr\hbox{\ intersects\ } t}$ are not consecutive, then the Main Invariant has been violated, since a shielding segment exists.}
If the Main Invariant is maintained when inserting segments, deleting segments (i.e., processing right endpoints) is easy: use the ``naive'' deletion algorithm depicted in figure \Fig2. To delete a segment $s$, find the immediate neighbors of the right endpoint of $s$ in the \ts-ordering, and then search upwards and downwards in the \ts-ordering, reporting intersections in each direction until segments are reached which do not intersect $s$.
Remember that in the above figure \Fig4, point $q\pr$ may now lie in a cone, although this was not true of point $q$. To destroy any potential cones enclosing $q\pr$, the immediate neighbors of $q\pr$ in the \ss- and \ts-orderings, by lemma \Th{\Sec2.2}, must be found. While this information takes $\Oh (\log n)$ time to compute for $p$, we shall see that by carefully following pointers, the same information can be computed for $q\pr$ in constant time.