Fix some approximation issues.
[master-thesis.git] / Parasitemia / ParasitemiaCore / Utils.fs
1 module ParasitemiaCore.Utils
2
3 open System
4
5 open Types
6
7 let inline roundInt v = v |> round |> int
8
9 let inline dprintfn fmt =
10 Printf.ksprintf System.Diagnostics.Debug.WriteLine fmt
11
12 let inline lineFromTwoPoints (p1: PointF) (p2: PointF) : Line =
13 let a = (p1.Y - p2.Y) / (p1.X - p2.X)
14 let b = -(p2.X * p1.Y - p1.X * p2.Y) / (p1.X - p2.X)
15 Line(a, b)
16
17 let inline pointFromTwoLines (l1: Line) (l2: Line) : PointF =
18 let x = -(l1.B - l2.B) / (l1.A - l2.A)
19 let y = -(l2.A * l1.B - l1.A * l2.B) / (l1.A - l2.A)
20 PointF(x, y)
21
22 let inline linePassThroughSegment (l: Line) (p1: PointF) (p2: PointF) : bool =
23 let p = pointFromTwoLines l (lineFromTwoPoints p1 p2)
24 sign (p.X - p1.X) <> sign (p.X - p2.X)
25
26 let inline squaredDistanceTwoPoints (p1: PointF) (p2: PointF) =
27 (p1.X - p2.X) ** 2.f + (p1.Y - p2.Y) ** 2.f
28
29 let inline distanceTwoPoints (p1: PointF) (p2: PointF) =
30 squaredDistanceTwoPoints p1 p2 |> sqrt
31
32 let countCells (cells: Cell list) : int * int =
33 cells |> List.fold (fun (total, infected) { cellClass = cellClass } ->
34 match cellClass with
35 | HealthyRBC -> (total + 1, infected)
36 | InfectedRBC -> (total + 1, infected + 1)
37 | Peculiar -> (total, infected)) (0, 0)