SHARE
TWEET

Untitled

a guest Oct 21st, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. (* auto eval 4.2 *)
  3.  
  4.  
  5. (* 1. *)
  6. type t_point_vector = {x : float; y : float};;
  7. (* type vector & type point didn't has any difference *)
  8. type t_ball = {pos : t_point_vector; speed : t_point_vector};;
  9. (* the ball have a position and a speed (due to gravity) *)
  10.  
  11. (* 2. *)
  12.  
  13. let gravity : t_point_vector = {x = 0.; y = -9.80665};;
  14. (* the gravity is a vector *)
  15.  
  16.  
  17. (* 3. *)
  18.  
  19. let sum_vector(vector_1, vector_2 : t_point_vector * t_point_vector) :
  20. t_point_vector =
  21.    {x = vector_1.x +. vector_2.x; y = vector_1.y +. vector_2.y}
  22. ;;
  23. (* sum_vector is a function used to add vector 1 and vector 2, can be used to add vector to position or position to position *)
  24.  
  25. let init_ball(height, speed : float * t_point_vector) : t_ball =
  26.   let init_point : t_point_vector = {x = 0.; y = height} in {pos =
  27. init_point; speed = speed}
  28. ;;
  29. (* assign the speed and the height to a ball *)
  30.  
  31. let rec compute_next_speed(ball, t : t_ball * int) : t_point_vector =
  32.   if t = 0
  33.   then ball.speed
  34.   else
  35.     sum_vector(compute_next_speed(ball, t-1), gravity)
  36. ;;
  37. (* a recursive function who add 't' times gravity to the speed *)
  38.  
  39. (* 4. *)
  40.  
  41. let rec compute_next_pos(ball , t: t_ball * int) : t_point_vector =
  42.   if t = 0
  43.   then ball.pos
  44.   else
  45.     sum_vector(compute_next_pos(ball, t-1), compute_next_speed(ball, t))
  46. ;;
  47. (* give the position after 't' seconds, due to speed changes *)
  48.  
  49. (* 5. *)
  50.  
  51. let rec compute_end_point(ball, t : t_ball * int) : t_point_vector =
  52.   let pos : t_point_vector = compute_next_pos(ball, t) in
  53.   if pos.y < 0.
  54.   then compute_next_pos(ball, t)
  55.   else
  56.     compute_end_point(ball, t+1)
  57. ;;
  58.  
  59.  
  60. compute_end_point({pos = {x = 0.; y = 100.}; speed = {x = 10.; y = 10.}}, 0);;
  61. (* exemple for a ball *)
  62.  
  63. #trace compute_end_point
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top