I can't solve it:
You are given 8 integers:
The two lines are not parallel therefore divide plane into 4 pieces. Point (x, y) lies inside of one these pieces.
Problem:
Write a fast algorithm that will find a point with integer coordinates in the same piece as (x,y) that is closest to the cross point of the two given lines.
Note:
This is not a homework, this is old Euler-type task that I have absolutely no idea how to approach.
Update: You can assume that the 8 numbers on input are 32-bit signed integers. But you cannot assume that the solution will be 32 bit.
Update 2: Difficult case - when lines are almost parallel - is the heart of the problem
Update 3: Author of the problem states that the solution is linear O(n) algorithm. Where n is the size of the input (in bits). Ie: n = log(A) + log(B) + ... + log(y)
But I still can't solve it.
Please state complexities of published algorithms (even if they are exponential).
Set the two equations for y equal to each other. Solve for x. This will be the x-coordinate for the point of intersection. Use this x-coordinate and substitute it into either of the original equations for the lines and solve for y.
Point of intersection means the point at which two lines intersect. These two lines are represented by the equation a1x + b1y + c1= 0 and a2x + b2y + c2 = 0, respectively.
alt text http://imagebin.ca/img/yhFOHb.png
Diagram
After you find intersection of lines L1:Ax+By=C
and L2:ax+by=c
i.e. point A(x1,y1)
.
Define two more lines y = ceil(y1)
and y = floor(y1)
parallel to X-axis
and find their intersection with L1
and L2
i.e. Points B(x2,y2)
and C(x3,y3)
.
Then point you require is D
or E
whichever is closer to point A
. Similar procedure applies to other parts of the plane.
D ( ceil(x2), ceil(y1) ) E ( ceil(x3), floor(y1) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With