3
Information Processing Letters 40 (1991) 137-139 North-Holland 8 November 1991 ukherjee and George Electrical, Computer, and Systems Engineering, Rensselaer Polytechnic Institute, Troy, hW 12180, USA Communicated by S.G. Akl Received 1 June 1990 Revised 21 August 1991 Keywords: Computational geometry, integer grid, continued fractions We consider the following problem: given two intersecting straight lines in R2 with rational slopes and intercepts, and a quadrant formed by these two lines, find the point with integer-valued coordinates in the given quad- rant, which is nearest to the point of intersec- tion of the two lines. We present an algorithm that finds the re- quired point in O(log N) time. Here N is the largest integer among the numerators and de- nominators of the rational numbers representing the slopes of the lines. We assume that integer division and floor functions are constant-time op- erations in our model of computation. Let the given lines be y =a,x +b, and y = a2x + b,, where a,, a2, b,, 6, are rational num- bers, and a, z a2. The coordinates of the inter- section point (x’, y ‘) are given as rational num- bers by x’= (b, -b,)/(a,-a,), Y’ = (a$, - a2W/(a2 -ad. If the angle of the given quadrant is equal to or greater than r/2, then the required point can be trivially obtained by checking among points WI, LY'D, WI, TY’I), Wl, LY’D, and (ix ‘1, f y ‘1). We solve below for the case where the angle of the quadrant is acute. Without loss of generality consider the quad- rant ((x, Y): a,x + 6, <Y < a2x + 62) with a, < a2 and a2 > 0. Any other acute quad- rant can be mapped to it by rotations of r/2 and/or reflections about the x or y axis. Case 1. If a, G 0, then the required point lies on either the horizontal line y = 1 y ‘1, or the line y = 1 y ‘1. Its x-coordinate is given by If both terms inside the min function are equal, then the required point is the one nearer to (x’, y’). Exam 1. Given lines y = -(3/4)x + 7/8 and y =(I x + l/4. We have (x’, y’) = (l/2, l/2), 1 y ’ ] = 0, and [ y ‘I= 1. The corresponding x-coor- dinates are 7/6 and 3/2. The x-coordinate of the ired point is obtained as min(r7/6], 13/21) = 0th contender points (2, 0) and i2, 1) are equidistant to the intersection point (see Fig. l(a)), so we may choose either. 0020-0190/91/$03.50 0 1991 - Elsevier Science Publishers B.V. All rights reserved 137

Constrained integer approximation to planar line intersection

Embed Size (px)

Citation preview

Page 1: Constrained integer approximation to planar line intersection

Information Processing Letters 40 (1991) 137-139 North-Holland

8 November 1991

ukherjee and George Electrical, Computer, and Systems Engineering, Rensselaer Polytechnic Institute, Troy, hW 12180, USA

Communicated by S.G. Akl Received 1 June 1990 Revised 21 August 1991

Keywords: Computational geometry, integer grid, continued fractions

We consider the following problem:

given two intersecting straight lines in R2 with rational slopes and intercepts, and a quadrant formed by these two lines, find the point with integer-valued coordinates in the given quad- rant, which is nearest to the point of intersec- tion of the two lines.

We present an algorithm that finds the re- quired point in O(log N) time. Here N is the largest integer among the numerators and de- nominators of the rational numbers representing the slopes of the lines. We assume that integer division and floor functions are constant-time op- erations in our model of computation.

Let the given lines be y =a,x +b, and y = a2x + b,, where a,, a2, b,, 6, are rational num- bers, and a, z a2. The coordinates of the inter- section point (x’, y ‘) are given as rational num- bers by

x’= (b, -b,)/(a,-a,),

Y’ = (a$, - a2W/(a2 -ad.

If the angle of the given quadrant is equal to or greater than r/2, then the required point can be trivially obtained by checking among points

WI, LY'D, WI, TY’I), Wl, LY’D, and

(ix ‘1, f y ‘1). We solve below for the case where the angle of the quadrant is acute.

Without loss of generality consider the quad- rant

((x, Y): a,x + 6, <Y < a2x + 62)

with a, < a2 and a2 > 0. Any other acute quad- rant can be mapped to it by rotations of r/2 and/or reflections about the x or y axis.

Case 1. If a, G 0, then the required point lies on either the horizontal line y = 1 y ‘1, or the line y = 1 y ‘1. Its x-coordinate is given by

If both terms inside the min function are equal, then the required point is the one nearer to (x’, y’).

Exam 1. Given lines y = -(3/4)x + 7/8 and

y =(I x + l/4. We have (x’, y’) = (l/2, l/2), 1 y ’ ] = 0, and [ y ‘I= 1. The corresponding x-coor- dinates are 7/6 and 3/2. The x-coordinate of the

ired point is obtained as min(r7/6], 13/21) = 0th contender points (2, 0) and i2, 1) are

equidistant to the intersection point (see Fig. l(a)), so we may choose either.

0020-0190/91/$03.50 0 1991 - Elsevier Science Publishers B.V. All rights reserved 137

Page 2: Constrained integer approximation to planar line intersection

Volume 40, Number 3 INFORMATION PROCESSING LETTERS 8 November 1991

(4

1. a, + a2 - la, 9, a, + a, - l.a,l;

2. bp -b,/a,, b, + -b,/a,, swda,, a,), a, + l/a,, a, + l/a,;

This algorithm simplifies the problem in each iteration and eventually yields Case 2 with a2 2 ia,1 > q We then find the solution point as described in Case 2, and back-transform the solu- tion point to the original domain.

0)

Fig. 1. Points A and A’ are both solutions of Example 1 (Case 1). Point B is the solution point of Example 2 (Case 2). Point C, the solution point after the first reduction of the original problem in Example 3, is mapped by Step I into point D, and then by Step 2 to point E (which is the same as point B). (The quadrant of the original problem in Example 3 is too

narrow to display at the same scale.)

Case 2. Now consider the case a2 >/ [a,l > a, > 0. Subtracting [a, 1 from the slopes transforms the ;:rablem to Case 1. We find the required point as in Case 1 and then transform it back to the original domain. If two points qualify for the minimum x-coordinates in the transformed prob- lem (i.e., in Case 0, we transform back the con- tender point with the smaller y-coordinate. (This is not necessarily the solution point of Case 1. For example, consider y = (19/l 1)x + 81/l 10 and y = (42/19)x + 47/95.)

Ie 2. Given lines y = (5/4)x + 7/8 and y = (5/2)x + l/4. Subtracting [5/41= 2 from the slopes gives the lines y = -(3/4)x + 7/8 and y=(1/2)x+ l/4. Th ese are the same lines as in Example 1 (see Fig. l(a)). Transforming back the solution point (2, 0) of Example 1, we obtain the required solution point (2, 4).

Case 3. The remaining case of interest is [a,1 > a2 > a, > 0. We reduce this case to Case 2 as follows.

xample 4. Given lines (47/14)x + l/4 and (41/12)x + l/24.

At Step 1 of the first cycle we subtract 3 from each slope. After Step 2 we obtain the new set of lines (12/5)x - l/10 and (14/5)x - 7/10.

In the second cycle we subtract 2 from both slopes. After the second step we obtain the new set of lines (5/4)x + 7/8 and (5/2)x + l/4. These lines satisfy the condition for Case 2 (Fig. l(b)).

As in E,xample 2, we find the solution for the reduced problem as (2, 4). Transforming this so- lution back to the original domain, we obtain the solution as (10, 34).

We obtain the correct result, because each step maps the solution point in some domain to the solution point in the next reduced domain. Step 1 does not change the x-coordinate of the required point (m, n), but only reduces its y-co- ordinate by [a, 1 m. The second step reflects the plane about the line y =x. This step only switches the x and the y-coordinates of the solution point.

The iterations of Step 1 and Step 2 generate the continued fraction expansion of the slopes. In Example 3,

1 47/14 = 3 + -

2 -t $z

138

Page 3: Constrained integer approximation to planar line intersection

Volume 40, Number 3 INFORMATION PROCESSING LETTERS 8 November 1991

and

1 41/12 = 3 + -

2+&’

Unless a, = a*., the corresponding terms will dif- fer by an integer at some stage of their continued fraction expansions. Thus eventually they will sat- isfy the conditions for Case 2. Since both the slopes a 1 and a2 are rational numbers, their contitsued fraction expansions will terminate after a finite number of steps. In the worst case, the number of terms in the continued fraction expan- sion is O(log N ), where N is the largest integer among the numerators and denominators of the rational numbers representing the slopes a, and a2 [l]. If integer division and floor function are constant-time operations, the algorithm has O(log N) run-time in the worst case. We do not know of any algorithm that has a better perfor- mance. We conjecture that our algorithm is opti- mal.

The problem can be extended to d hyper- planes in IV? In 3-D, three planes intersecting at a point define an open pyramid. We are required

to find the nearest point with integer-valued co- ordinates inside the pyramid. Although we have been able to generalize the notion of the 2-D transformations, the solution point in a given domain does not necessarily map to the solution point in the reduced domain. This makes it diffi- cult to extend the method even to 3-D.

We are grateful to the anonymous referee who suggested considerable simplification in our nota- tions and improvements in wording. This work was partially supported by the National Science Foundation Information, Robotics, and Systems grant IRI-8704718, and by a New York State Science and Technology Foundation grant to the Rensselaer Center for Advanced Technology.

ference

[l] D.E. Knuth, Tire Art of Computer Programming - Seminu- merical Algorithms, Vol. Ii (Addison-Wesley, Reading, MA, 1981).

139