How to resolve the algorithm Find the intersection of two lines step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find the intersection of two lines step by step in the F# programming language

Table of Contents

Problem Statement

Find the point of intersection of two lines in 2D.

The 1st line passes though   (4,0)   and   (6,10) . The 2nd line passes though   (0,3)   and   (10,7) .

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find the intersection of two lines step by step in the F# programming language

Source code in the fsharp programming language

(*
Find the point of intersection of 2 lines.
Nigel Galloway May 20th., 2017
*)
type Line={a:float;b:float;c:float} member N.toS=sprintf "%.2fx + %.2fy = %.2f" N.a N.b N.c
let intersect (n:Line) g = match (n.a*g.b-g.a*n.b) with
                           |0.0 ->printfn "%s does not intersect %s" n.toS g.toS
                           |ng  ->printfn "%s intersects %s at x=%.2f y=%.2f" n.toS g.toS ((g.b*n.c-n.b*g.c)/ng) ((n.a*g.c-g.a*n.c)/ng)
let fn (i,g) (e,l) = {a=g-l;b=e-i;c=(e-i)*g+(g-l)*i}
intersect (fn (4.0,0.0) (6.0,10.0)) (fn (0.0,3.0) (10.0,7.0))
intersect {a=3.18;b=4.23;c=7.13} {a=6.36;b=8.46;c=9.75}


  

You may also check:How to resolve the algorithm Merge and aggregate datasets step by step in the Prolog programming language
You may also check:How to resolve the algorithm Anti-primes step by step in the C++ programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Ada programming language
You may also check:How to resolve the algorithm A+B step by step in the Shiny programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the JavaScript programming language