\input TR10GBWideFormat \begfile{GBb10.tex of October 17, 1983 5:47 pm --- Stolfi} \chapter{10}{Convex Tracings} % Additional macros \def\Thei{1} \def\Theii{2} \def\Theiii{3} \def\Theiv{4} \def\Thev{5} \def\Thevi{6} \def\Thevii{7} \def\Theviii{8} \def\Theix{9} \def\Thex{10} \def\Thexi{11} \def\Thexii{12} \def\Thexiii{13} \def\Thexiv{14} \def\Thexv{15} \def\Thexvi{16} \def\Thexvii{17} \def\Thexviii{18} \def\Thexix{19} \def\Thexx{20} \def\Thexxi{21} \def\Thexxii{22} \def\Thexxiii{23} \def\Defi{1} \def\Eqii{2} \def\Eqiii{3} \def\Algi{1} \def\Algii{2} \def\PP{{\char P}} \def\CC{{\char C}} \def\Col{\mathop{\char C\char o\char l}} \def\Det{\mathop{\char D\char e\char t}} \def\The{\bug} In chapters 3 and 4 of last year's notes we studied convex figures and polygons from the classical point of view. In this and the following chapters we will have a second look at convexity and related issues, using the machinery we have just developed for dealing with polygonal tracings. As we shall see, the car trip analogy and its related formal concepts (states, tangents, moves, turns, and so forth) are quite helpful in the description and verification of the classical algorithms. Above all, we will see that the concept of convolution is a very powerful tool for generalizing many classical algorithms and for developing new ones. \section{10.1. Continuity of tracings}\par Let's first state a few lemmas that are valid for polygonal tracings in general and will be quite useful in the sequel. These properties are easy to justify intuitively by imagining the states of a tour as being traversed by a car that moves in a continuous fashion through time. This analogy can be made precise by constructing explicitly for every tour $P$ a continuous function that gives the state $P(t)$ of the car at each instant $t$. \mfig 4cm Let $P$ be the tour $\langle s0\,s0\pr\,s1\,\ldots\,s{n-1}\,s{n-1}\pr\rangle$; then $P(t)$ is a function with period $n$, such that $P(i)=si$, $P(i+{1\over2})=si\pr$ for all integer $i$. Intuitively, it means that from time $i$ to $i+{1\over2}$ the car is moving, and from time $i+{1\over2} $ to $i+1$ it is turning. The integer and half-integer values of $t$ are the {\it critical instants} of the tour; the value of $P(t)$ for all other instants can be defined by straightforward interpolation. That is, the velocity of the car is constant during a move, and so is its angular velocity during a turn. \endmfig \digress{Actually, the exact form of this function is not important, as long as it traverses periodically all states of $P$ in a sufficiently ``well-behaved'' way. The above definition makes the ride quite uncomfortable, as the acceleration at the critical instants is infinite. By using a slightly more elaborate interpolation scheme, we could have obtained finite acceleration (indeed, finite derivatives of all orders) at all instants. However, the simple scheme above will suffice for our needs.} For this chapter, it is more convenient to consider turns and moves of a tracing as being closed at the beginning and open at the end, i.e. as being $[si\to si\pr)$ and $[si\pr\to s{i+1})$, respectively. Accordingly, we redefine the moving states as including the beginning (but not the end) of every move, and similarly for the turning states. The {\it moving} and {\it turning instants} are therefore those $t$'s satisfying $i\leq tyn$ stop: $p$ is above the chain.} \step{2}{ Set $i\gets 0$, $j\gets n$.} \step{3}{{\small (At this point we know that $yi\leq y\leq yj$.)} While $j-i>1$, repeat the following step:} \begblock \step{4}{Let $k\gets \lfloor (i+j)/2\rfloor$. If $yb-c$, set $d\gets c$ and $c\gets (a+d)/2$; otherwise, set $d\gets (b+c)/2$.} \comm{Now $af(b)$.} \step{5}{If $f(c)>f(d)$ then set $b\gets d$; else set $a\gets c$ and $c\gets d$.} \comm {This eliminates at least ${1\over4}$ of the interval $[a\sp b)$.} \endblock \step{6}{Stop: the maximum is in the interval $[a\sp b)$.} } The loop in step 2 eventually terminates, since after at most two permutations the maximum of $f(a)$, $f(b)$ and $f(c)$ will be $f(c)$. It is easy to see that the assignments in step 5 preserve the invariant of step 3. The figure below ilustrates those assignments for two typical cases. \figspace 3cm To find the points of a convex polygon at maximum and minimum distance from a line $\lscr$, we may take $f(t)$ to be the dot product of the position $\po P(t)$ with a vector $d\pr$ perpendicular to $\lscr$ and pointing to its left. In the case when $\lscr$ is the $x$-axis, of course, $f(t)$ is simply the $y$-coordinate of $\po P(t)$. Since we know that maximum and minimum of $f(t)$ is attained at a vertex, we can restrict the search to integer instants only, and stop whenever the error is less than $1$. It may be instructive to consider in detail how would we code algorithm {\Algii} with the above changes. The program we show next is written in a fictitious Algol-like language, that should be readable without much difficulty by anyone conversant in PASCAL or ALGOL. Note the use of $\prg{mod}$ in the indexing of the matrix $v$. The given function $f(p)$ should compute the signed distance from the point $p$ to the desired line, or the dot product of $p$ with the perpendicular direction vector $d\pr$. \vskip10pt plus10pt {\prog Find-Maximum: procedure with arguments (v: array[0..n) of point, f: function from (p:point) into real) = begin a: integer \pgets 0; b: integer \pgets n; c: integer \pgets (a + b)/2; while f(v[c mod n]) < f(v[a mod n]) or f(v[c mod n]) < f(v[b mod n]) do t \pgets a; a \pgets c; c \pgets b; b \pgets t+n od; while b - a > 1 do if b - c > c - a then d \pgets (b + c)/2 else d \pgets c; c \pgets (a + d + 1)/2 fi; if f(v[d mod n]) > f(v[c mod n]) then b \pgets c else a \pgets c; c \pgets d fi od end. \endprog} Algorithm {\Algii} gives us also a method for testing whether a line $\lscr$ intersects a convex polygon $P$. All we have to do is to find the vertices of $P$ with maximum and minimum signed distance from $\lscr$; then the latter will intercept $P$ if and only if these distances have opposite signs, i.e if and only if the two extrema are on opposite sides of $\lscr$. If that is the case, they are a proof that $\lscr$ meets the interior of $P$. If both extrema lie on the same side of $\lscr$, then the tangent to $P$ parallel to $\lscr$ and passing through the extremum that is closest to $\lscr$ separates it from $P$. \figspace 3cm \section{10.5. Polygon intersection via convolution} We will now apply our convolution machinery to the development of an algorithm for testing convex polygon intersection. Several further examples of the use of convolutions in computational geometry problems will be given later on in the notes. As we already remarked, the fundamental theorem tells us that two convex polygons $P$ and $Q$ intersect, if and only if the origin is inside $P\ast Q^{-I}$. Let $p$ and $q$ denote the sizes of $P$ and $Q$ respectively. We already know how to build the convolution in time $O(p+q)$, and to test for point inclusion in the result in time $O(\lg (p+q))$; it follows that we can test for the intersection of $P$ and $Q$ in time $O(p+q)$. There may be a temptation to rest on our laurels at this point, since, after all, don't we have to look at each edge of $P$ and $Q$ to know if they intersect? Suprisingly, the answer is no. We will see how, by the clever use of binary search ideas, we can test for intersection in time only $O(\lg (p+q)) = O(\lg p + \lg q)$ (why are these equal?) using the standard representations for $P$ and $Q$. Before we proceed towards this goal, let us remark that there are applications where $P$ and $Q$ may be freely movable around the plane by translation, and for each placement of $P$ and $Q$ we may want to know whether they intersect. This is the case, for example, when $P$ and $Q$ represent solid bodies moving around on the plane. (Although we have not discussed this, the same ideas work in three-dimensional space as well, where this collision avoidance problem is very important.) Now for this situation the above solution is exactly what we want: $P^x$ will intersect $Q^y$ iff $O\in P^x\ast (Q^y)^{-I} = P^x\ast Q^{-I-y}$. But the latter inclusion is equivalent to $y-x\in P\ast Q^{-I}$. So, after computing the convolution $P\ast Q^{-I}$ just once, in time $O(p+q)$, we can test for intersection in logarithmic time for each placement of $P$ and $Q$. \figspace 4cm The central idea in the more efficient algorithms we are about to describe is that we don't need to compute the entire convolution in order to test for point inclusion against it. Rather, we will emulate a point inclusion algorithm while computing the parts of the convolution that we need to test against only if we need to. It is important to understand this general strategy, as it is applicable to problems other than the one we are currently discussing, such as common tangent computations, distance determination, etc. Suppose, for example, that we desire to test on what side of the move $e$ of $P\ast Q^{-I}$ the origin lies. Suppose further that we know that $e$ comes from edge $f$ of $P$. As we know, $e$ is then the vector sum $f+v$ for some vertex $v$ of $Q$. If can determine $v$, then we can place $e$ in the same position in the plane it will occupy in the convolution. Since the latter will be a frequent operation, we will call it ``placing $e$ in the convolution''. To find $v$, we simply have locate $e$ according to slope in the edge cycle of $Q$. We already know how to do this by binary search in time $O(\lg q)$, as in section 10.4.3. Thus we can place an edge in the convolution and test a point against it in total time only $O(\max\set{\lg p,\lg q}) = O(\lg(p+q))$. For the pragmatic reasons enumerated earlier, we choose to emulate the cartesian point inclusion algorithm. Recall that this algorithm is based on discriminating a point against a monotone chain. For a convex polygon $P$, we defined its left ($P{L}$) and right ($P{R}$) shadows and showed that $p\in P$ if and only if $p\in P{L}$ and $p\in P{R}$. The relationship between shadows and intersection is given by the following lemma. \lemma{\Thexviii}{The intersection $P\inte Q$ is non-empty if and only if both intersections $P{L}\inte Q{R}$ and $P{R}\inte Q{L}$ are non-empty.} \proof{In one direction the lemma is clear. A point $x$ belonging to both $P$ and $Q$ belongs to all four of $P{L}, P{R}, Q{L},$ and $Q{R}$. To prove the converse, suppose that $P$ and $Q$ are disjoint. By lemma 3.18 we know that they possess a separating line $\lscr$. If $\lscr$ is horizontal, then clearly both $P{L}\inte Q{R}$ and $P{R}\inte Q{L}$ are empty. If not, let $P$ be the polygon to its left, and $Q$ the one to its right. Obviously $P{L}\inte Q{R} = \empty$ and we are done.} Thus it suffices to be able to test the intersection of a left shadow $L$ with a right shadow $R$. By the convolution theorem this is equivalent to testing whether the origin belongs to $L\ast R^{-I}$. Note that $R^{-I}$ is a {\it left} shadow, and that the convolution of two left shadows is also a left shadow. This is then the problem we will consider: given two left shadows $A$ and $B$, discriminate the origin against $A\ast B$. We assume that each of $A$ and $B$ is given to us as an array containing the coordinates of its vertices and arranged in decreasing $y$ order. To follow now the suggestion given above, we wish to emulate the process of point discrimination against a monotone chain (algorithm 1) by running it on the implicitly known monotone chain $A\ast B$. Let $a$ and $b$ denote respectively the sizes of $A$ and $B$, and let $n=a+b$. The first step of this algorithm is to discriminate the origin against the median edge of $A\ast B$. When we know a chain explicitly, finding its median edge is a constant time operation. But how do we find the median edge of $A\ast B$? It is actually possible to find which edge of $A$ or $B$ would be the median edge of the convolution in $O(\lg n)$ tests, but we leave this as an exercise for the reader. The reason we wish to discriminate against the middle edge of $A\ast B$ is that it allows us to eliminate at least half the edges of the convolution after an edge comparison. The logarithmic time bound of the discrimination algorithm, however, depends only on being able to eliminate some fixed fraction of the edges after each comparison, so we can afford to be less demanding. If we assume without loss of generality that $a\geq b$, then a discrimination against the middle edge of $A$ in the convolution will allow us to get rid of at least half the edges of $A$, or a quarter of the total. To do this discrimination, we must place this edge in the convolution, or equivalently, locate it according to its slope in the list of edges of $B$. A binary search will do this in $O(\lg n)$ time. This implies that we can run an emulation of algorithm 1 in $O(\lg n)$ stages of cost $O(\lg n)$ each, or total cost $O(\lg^2 n)$. Is this the best we can do? We will show that we can actually discriminate the origin against the convolution $A\ast B$ in time only $O(\lg n)$. To this end let us first look at another restatement of the definition of convolution: \lemma{\Thexix}{Let $A$ and $B$ be two left shadows bounded on the right by two $y$-monotone chains with high endpoints $hA$, $lA$ and low endpoints $hB$, $lB$ respectively. Each interleaving of the edges of $A$ and $B$ can be viewed as a monotone chain from $hA+hB$ to $lA+lB$. Among all these chains the convolution $A\ast B$ is the righmost one, in the sense that no other interleaving extends at any $y$ value further to the right than the convolution.} \proof{It will be instructive to consider the following argument. Consider any arbitrary interleaving of the edges of $A$ and $B$, as shown below, defining a chain $C$. \figspace 4cm \pp Suppose there are two consecutive edges $e$ and $f$, such that they are out of order in slope. Then $e$ and $f$ cannot both belong to $A$ or $B$, so without loss of generality let us assume that $e$ belongs to $A$ and $f$ belongs to $B$. Now consider the chain $D$ obtained by swapping the order of $e$ and $f$ in $C$. Then clearly $D$ is everywhere to the right of $C$, and it has one less pair of edges out of slope order. We can keep applying this transformation till no pair of adjacent edges is out of order. The resulting chain will be the convolution $A\ast B$, and it will be to the right of all other chains.} \mfig 6cm For the faster algorithm we cannot afford to spend time $O(\lg n)$ in placing an edge of the convolution. Instead we wish to find a constant time test that allows us to get rid of some fraction of the edges of $A$ or $B$. Let $e$ denote the median edge of $A$, and $f$ the median edge of $B$. Again, without loss of generality, we can assume that $e$ precedes $f$ in slope. Let also $AH$, $AL$, $BH$, $BL$ denote respectively the high and low halves of $A$ and $B$ obtained after edges $e$ and $f$ are removed. Consider now the chain $D$ which is the concatenation of $AH\ast BH$ with $e$, $f$, and $AL\ast BL$ in this order. We can place $e$ and $f$ in $D$ in constant time. In the convolution $C$ the edges $e$ and $f$ must occur as shown at right. \endmfig Consider now the partition of the plane define by edges $e$ and $f$ as shown below. \figspace 4cm If the origin $O$ falls in region {\it Left}, then clearly $O$ is to the left of the convolution, and we are finished. If $O$ falls in region {\it Above}, then what can we conclude? In discriminating against the convolution, all edges of $BL$ will be below $O$, so they can be discarded without affecting the outcome of the discrimination. Similarly, if $O$ falls in region {\it Below}, then $O$ will be below all edges of $AH$ in the convolution, so these edges can be discarded. Therefore by this constant time test we have been able to reduce ourselves to an equivalent problem where either chain $A$, or chain $B$, has been halved. After $O(\lg a)+O(\lg b) = O(\lg n)$ such steps the upper or lower half of either $A$ or $B$ must become empty, so at that point one of $A$ or $B$ has no more than two edges left. It is now possible to complete the discrimination in $O(\lg n)$ further time. For suppose that it is $B$ that has been reduced to one or two edges. The location of these edges of $B$ among the slope ordering of the edges of $A$ can be computed in $O(\lg n)$ steps. Once that is done, we can discriminate $O$ against all the edges of $A$, as they appear in the convolution, in total time $O(\lg n)$, since we can now place each edge of $A$ in the convolution in constant time. To complete the discrimination we may at most need to test against the one or two remaining edges of $B$. Thus we have been able to discriminate $O$ against $A\ast B$ in total cost $O(\lg n)$. So we have proved: \theorem{\Thexx}{It is possible to discriminate a point $p$ against the convolution of two left shadows of size $O(n)$ in time $O(\lg n)$.} \theorem{\Thexxi}{It is possible to test if two convex tracings of size $O(n)$ intersect in total time $O(\lg n)$.} The last result, as stated, is somewhat unsatisfactory, since we may want to know more than whether our two convex tracings $P$ and $Q$ intersect. For example, we may want to know of a common point if they do intersect, or a separating line if don't. The point, or line, respectively are {\it witnesses} to the intersection, or non-intersection, of $P$ and $Q$. On the other hand it is not reasonable to expect the same algorithm to produce the complete description of the intersection $P\inte Q$ and stay witin the $O(\lg n)$ time bound, since, as we will shortly see, this decription can take $O(p+q) = O(n)$ space to write down. Suppose first that the algorithm responds that $P$ and $Q$ do not intersect, and let as before $P{L}$, $P{R}$, $Q{L}$, $Q{R}$ denote the left and right shadows of $P$ and $Q$ respectively. Then at some comparison it was discovered that $O$ lies above both $P$ and $Q$, or below both $P$ and $Q$, or to the left of $PR\ast QL^{-I}$, or to the right of $PL\ast QR^{-I}$. All four cases are essentially analogous, so let us suppose that some edge test showed $O$ to be to the right of the convolution of $PL$ and $QR^{-I}$. That in fact was a test against some edge $e$ of either $P$ or $Q$. \mfig 6cm Let $v$ be the point where $e$ intersects the negative $y$ axis. Point $v$ must be of the form $p-q$, where $p$ is a point of the boundary of $PL$ and $q$ is a point of the boundary of $QR^{-I}$. (Furthermore, $p$ and $q$ can be computed from $v$ in constant time). There is a line $\lscr$ parallel to $e$ and separating $O$ from $PL\ast QR^{-I}$, for example the line through the point $(p-q)/2$. It is easy to check that $\lscr$, translated by $q$, is in fact a separating line of $P$ and $Q$. We have now in fact rederived lemma 3.18 proved earlier, namely that two disjoint convex polygons in the plane have a separating line parallel to a side of one of them. \endmfig \vfill\eject It is also interesting to do the same when $P$ and $Q$ do intersect. Then our algorithm establishes that $O$ is to the left of $PL\ast QR^{-I}$ by some edge test against an edge $e$. The point where $e$ intersects the $x$ axis is of the form $pe-qe$, and it is easy to check that this implies that the line segment from $qe$ on the left to $pe$ on the right is in $PL\inte QR$. Similarly, $O$ is shown to be to the right of $PR\ast QL^{-I}$ via an edge test against some edge $f$. This now leads to a line segment from $pf$ on the left to $qf$ on the right that is in $PR\inte QL$. We leave it to the reader to show that the segments $pepf$ and $qeqf$ do in fact intersect, and that their intersection point is a point in $P\inte Q$. \figspace 4cm \par\vfill\eject \section{JUNK:} \subsection {Common tangents} [ ] tangents from a point to a polygon; linear algorithm discuss bimodality of intercept on separator [ ] distance between antipodal (= opposite?) tangents [ ] maximally apart antipodal tangents a. touch the polygon exactly once each b. line joining points of contact is perpendicular to the tangents c. length of that segment equals the diameter of the set [ ] same true for minimally apart antipodal tangents [ ] two disjoint convex polygons P and Q; consider antipodal tangents, one to P and one to Q [ ] if (p,q) is the distance from P to Q, then the tangents at p and q are antipodal and are perpendicular to P and Q [ ] a. disjoint convex sets have a separating line b. even one parallel to a side of one of them (Hobby argument) [ ] Take two disjoint convex polygons and wlog rotate the plane so that their separating line is vertical [ ] Use car language; discuss states at leftmost and rightmost points [ ] existence of exactly two outer common tangents by intecept on separator argument existence of exactly two inner common tangents [ ] computing the outer common tangents; **give clean linear algorithm and proof [ ] give $log^2$ algorithm based on separator intercept give log algorithm based on predicates RA: A is to the right of b RB: B is to the right of a and cutting up the product space discuss L-shape binary search in detail with invariants maintained [ ] new observation that eliminates L-search [ ] remark on always following rightmost car; prove that when we switch from one tracing to the next, the car moves forwards. Thus result is a convex tracing, the convex hull. [ ] discuss leftmost car; concept of monostrofic (??) tracing. Remark that common tangent computation algorithms apply also to monostrofic tracings. [ ] derive Jarvis's algorithm for CH from same ideas on n cars spinning on n points. [ ] delop carefully log search algorithm for bimodal function zero crossings, or maxima and minima apply to get log algorithm for tangents \par\vfill\eject\end