math.ml 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. open Base
  2. type pos = { x : int ; y : int }
  3. and dim = { w : int ; h : int }
  4. and aabb = { bx : int ; by : int ; bw : int ; bh : int }
  5. module Pos = struct
  6. type t = pos
  7. let zero = { x = 0 ; y = 0 }
  8. let to_string {x;y} =
  9. Printf.sprintf "<%d,%d>" x y
  10. let equal a b = Int.(a.x = b.x && a.y = b.y)
  11. end
  12. module Dim = struct
  13. type t = dim
  14. let zero = { w = 0 ; h = 0 }
  15. let to_string {w;h} =
  16. Printf.sprintf "<%dx%d>" w h
  17. let equal a b = Int.(a.w = b.w && a.h = b.h)
  18. end
  19. module AABB = struct
  20. type t = aabb
  21. let zero = { bx = 0 ; by = 0 ; bw = 0 ; bh = 0 }
  22. let make {x;y} {w;h} =
  23. { bx = x ; by = y
  24. ; bw = w ; bh = h }
  25. let to_string {bx;by;bw;bh} =
  26. Printf.sprintf "<%d,%d %dx%d>" bx by bw bh
  27. let equal a b = Int.(a.bx = b.bx && a.by = b.by
  28. && a.bw = b.bw && a.bh = b.bh)
  29. let contains {bx;by;bw;bh} {x;y} =
  30. x >= bx && x <= bx + bw && y >= by && y <= by + bh
  31. let center {bx;by;bw;bh} {w;h} =
  32. { bx = max bx (bx + (bw - w) / 2)
  33. ; by = max by (by + (bh - h) / 2)
  34. ; bw = min bw w
  35. ; bh = min bh h }
  36. end