views:

136

answers:

1

I have following task:

In the program we should draw lines on a bit mapped display. An array of n pairs of reals (ai,bi) defined the n lines yi = ai*x + bi. The lines were ordered in the x-interval [0, 1] in the sense that yi < yi+1 for all values of i between 0 and n-2 and for all values of x in [0, 1]

Less formally, the lines don't touch in the vertical slab. Given a point (x,y), where 0 < x < 1, we want to determine two lines that bracket the point.

How can we solve this problem quickly?

+1  A: 
Function bracket( Real x, Real y, Array a[1..n],b[1..n] of Reals): Returns void 
{
  Integer i = 1;

  While (i<=n && (a[i] * x + b[i]) <= y, i++)

  If (i==1 || i == n+1) 
                       { Print("Not bracket exists");
                         Exit()
                       }
  If (a[i] * x + b[i]) == y)
                       { Print("Point lies on line",i);
                         Exit()
                       }
  Print("Point between lines ", i-1, " and ", i);
}  

There is, however a slight catch. See the following picture:

alt text

Would you say that point F is "bracketed" by the two lines in [0,1]x[0,1] ?? What is the correct answer in this case?

belisarius
Maybe a binary search would be faster?
Dialecticus
@Dialecticus Perhaps I misunderstood the OP in ("How can we solve this problem quickly?"). If the "quickly" means O(logn) you are right, If "quickly" means in just one instruction, the "while" loop above will do. Never saw "quickly" before used in this context :)
belisarius