PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Root-branch-php-utl/SWIG/Examples/ocaml/shapes/example_prog.ml

#
OCaml | 76 lines | 64 code | 11 blank | 1 comment | 0 complexity | 9664221e673510e46e9e5fca08bb8de4 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. (* example_prog.ml *)
  2. open Swig ;;
  3. open Example ;;
  4. let side_length (ax,ay) (bx,by) =
  5. sqrt (((bx -. ax) ** 2.0) +. ((by -. ay) ** 2.0)) ;;
  6. let triangle_area a_pt b_pt c_pt =
  7. let a = (side_length a_pt b_pt)
  8. and b = (side_length b_pt c_pt)
  9. and c = (side_length c_pt a_pt) in
  10. let s = (a +. b +. c) /. 2.0 in
  11. sqrt (s *. (s -. a) *. (s -. b) *. (s -. c)) ;;
  12. let point_in_triangle (pta,ptb,ptc) x y =
  13. let delta = 0.0000001 in (* Error *)
  14. let ptx = (x,y) in
  15. begin
  16. let a_area = triangle_area pta ptb ptx
  17. and b_area = triangle_area ptb ptc ptx
  18. and c_area = triangle_area ptc pta ptx
  19. and x_area = triangle_area pta ptb ptc in
  20. let result = (abs_float (a_area +. b_area +. c_area -. x_area)) < delta
  21. in
  22. result
  23. end ;;
  24. let triangle_class pts ob meth args =
  25. match meth with
  26. "cover" ->
  27. (match args with
  28. C_list [ x_arg ; y_arg ] ->
  29. let xa = x_arg as float
  30. and ya = y_arg as float in
  31. (point_in_triangle pts xa ya) to bool
  32. | _ -> raise (Failure "cover needs two double arguments."))
  33. | _ -> (invoke ob) meth args ;;
  34. let dist (ax,ay) (bx,by) =
  35. let dx = ax -. bx and dy = ay -. by in
  36. sqrt ((dx *. dx) +. (dy *. dy))
  37. let waveplot_depth events distance pt =
  38. (List.fold_left (+.) 0.0
  39. (List.map
  40. (fun (x,y,d) ->
  41. let t = dist pt (x,y) in
  42. ((sin t) /. t) *. d)
  43. events)) +. distance
  44. let waveplot_class events distance ob meth args =
  45. match meth with
  46. "depth" ->
  47. (match args with
  48. C_list [ x_arg ; y_arg ] ->
  49. let xa = x_arg as float
  50. and ya = y_arg as float in
  51. (waveplot_depth events distance (xa,ya)) to float
  52. | _ -> raise (Failure "cover needs two double arguments."))
  53. | _ -> (invoke ob) meth args ;;
  54. let triangle =
  55. new_derived_object
  56. new_shape
  57. (triangle_class ((0.0,0.0),(0.5,1.0),(1.0,0.6)))
  58. '() ;;
  59. let waveplot =
  60. new_derived_object
  61. new_volume
  62. (waveplot_class [ 0.01,0.01,3.0 ; 1.01,-2.01,1.5 ] 5.0)
  63. '() ;;
  64. let _ = _draw_shape_coverage '(triangle, 60, 20) ;;
  65. let _ = _draw_depth_map '(waveplot, 60, 20) ;;