SHARE
TWEET

Untitled

a guest Oct 21st, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (* auto eval 4.2 *)
  2.  
  3.  
  4. (* 1. *)
  5. type t_point_vector = {x : float; y : float};; (* type vector & type
  6. point didn't has any difference *)
  7. type t_ball = {pos : t_point_vector; speed : t_point_vector};;
  8.  
  9. (* 2. *)
  10.  
  11. let gravity : t_point_vector = {x = 0.; y = -9.80665};;
  12.  
  13.  
  14. (* 3. *)
  15.  
  16. let sum_vector(vector_1, vector_2 : t_point_vector * t_point_vector) :
  17. t_point_vector =
  18.    {x = vector_1.x +. vector_2.x; y = vector_1.y +. vector_2.y}
  19. ;;
  20.  
  21. let init_ball(height, speed : float * t_point_vector) : t_ball =
  22.   let init_point : t_point_vector = {x = 0.; y = height} in {pos =
  23. init_point; speed = speed}
  24. ;;
  25.  
  26. let rec compute_next_speed(ball, t : t_ball * int) : t_point_vector =
  27.   if t = 0
  28.   then ball.speed
  29.   else
  30.     sum_vector(compute_next_speed(ball, t-1), gravity)
  31. ;;
  32.  
  33. (* 4. *)
  34.  
  35. let rec compute_next_pos(ball , t: t_ball * int) : t_point_vector =
  36.   if t = 0
  37.   then ball.pos
  38.   else
  39.     let speed : t_point_vector = compute_next_speed(ball, t-1) in
  40.     sum_vector(compute_next_pos(ball, t-1), {x = speed.x; y = speed.y})
  41. ;;
  42.  
  43. (* 5. *)
  44.  
  45. let rec compute_end_point(ball, t : t_ball * int) : t_point_vector =
  46.   let pos : t_point_vector = compute_next_pos(ball, t) in
  47.   if pos.y < 0.
  48.   then compute_next_pos(ball, t)
  49.   else
  50.     compute_end_point(ball, t+1)
  51. ;;
  52.  
  53. compute_end_point({pos = {x = 0.; y = 1000.}; speed = {x = 0.; y = 0.}}, 10);;
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