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 then
linesyi = ai*x + bi开发者_StackOverflow社区
. The lines were ordered in thex
-interval[0, 1]
in the sense thatyi < yi+1
for all values ofi
between0
andn-2
and for all values ofx
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:
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?
精彩评论