Advertisement
Guest User

Line Line Intersection

a guest
Jul 4th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.43 KB | None | 0 0
  1.             ///Returns the intersection of two Line2Ds that are known to intersect.
  2.             static member public IntersectionOf(a:Line2D, b:Line2D) =
  3.                
  4.                 //Returns A, B, C for: AX + BX = C
  5.                 //as a Treble(A, B, C)
  6.                 let GetABC(working:Line2D) =
  7.                     let A = working.End.Y - working.Start.Y
  8.                     let B = working.End.X - working.Start.X
  9.                     let C = (A * working.End.X) + (B * working.Start.Y)
  10.                     A, B, C
  11.  
  12.                 let lineA_data = GetABC a
  13.                 let lineB_data = GetABC b
  14.                
  15.                 //TODO: How to get data out of Tuples/Trebles without fun vars?
  16.  
  17.                 let GetDeterminant (a1, b1, c1) (a2, b2, c2) =
  18.                     (a1*b2) - (a2*b1)
  19.                
  20.                 let determinant =
  21.                     GetDeterminant lineA_data lineB_data
  22.  
  23.                 if determinant = float32(0) then
  24.                     raise(Exception("Lines were parallel!"))
  25.                
  26.                 let GetResultX (a1, b1, c1) (a2, b2, c2) d =
  27.                     ((b2*c1)-(b1*c2)) / d
  28.  
  29.                 let GetResultY (a1, b1, c1) (a2, b2, c2) d =
  30.                     ((a1*c2) - (a2*c1)) / d
  31.  
  32.                 new Vector2(
  33.                         GetResultX lineA_data lineB_data determinant,
  34.                         GetResultY lineA_data lineB_data determinant)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement