Friday, April 8, 2011

function with LINQ

Today I work on a problem to find if a line is intersected with a circle.

so f1 is y = ax + b and f2 is x^2 + y^2 - r^2 = 0

It is true you can use for-loop to solve this problem, but since I hate for-loop, i will use LINQ.

the input to f1 is { x }, if the line is intersected with a circle means there is (a,b) where f2(a,b) <=0. OK, the data input is {x}, and it is transformed by f1 into some { (x,y) } tuple sequence. If there is a tuple in the sequence makes f2 <=0, we can draw the conclusion that the line is intersected with a circle.

The pseudo-code is like:

{ x } |> seq.map f1 |> seq.exists f2(x,y) <=0

Let me give detailed code tomorrow

No comments: