开发者

Algorithm to find the lines bracketing one point

开发者 https://www.devze.com 2023-01-22 01:59 出处:网络
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 linesyi = ai*x + bi开发者_StackOverflow社区. The lines were ord

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开发者_StackOverflow社区. 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?


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:

Algorithm to find the lines bracketing one point

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号