Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* auto eval 4.2 *)
- (* 1. *)
- type t_point_vector = {x : float; y : float};;
- (* type vector & type point didn't has any difference *)
- type t_ball = {pos : t_point_vector; speed : t_point_vector};;
- (* the ball have a position and a speed (due to gravity) *)
- (* 2. *)
- let gravity : t_point_vector = {x = 0.; y = -9.80665};;
- (* the gravity is a vector *)
- (* 3. *)
- let sum_vector(vector_1, vector_2 : t_point_vector * t_point_vector) :
- t_point_vector =
- {x = vector_1.x +. vector_2.x; y = vector_1.y +. vector_2.y}
- ;;
- (* 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 *)
- let init_ball(height, speed : float * t_point_vector) : t_ball =
- let init_point : t_point_vector = {x = 0.; y = height} in {pos =
- init_point; speed = speed}
- ;;
- (* assign the speed and the height to a ball *)
- let rec compute_next_speed(ball, t : t_ball * int) : t_point_vector =
- if t = 0
- then ball.speed
- else
- sum_vector(compute_next_speed(ball, t-1), gravity)
- ;;
- (* a recursive function who add 't' times gravity to the speed *)
- (* 4. *)
- let rec compute_next_pos(ball , t: t_ball * int) : t_point_vector =
- if t = 0
- then ball.pos
- else
- sum_vector(compute_next_pos(ball, t-1), compute_next_speed(ball, t))
- ;;
- (* give the position after 't' seconds, due to speed changes *)
- (* 5. *)
- let rec compute_end_point(ball, t : t_ball * int) : t_point_vector =
- let pos : t_point_vector = compute_next_pos(ball, t) in
- if pos.y < 0.
- then compute_next_pos(ball, t)
- else
- compute_end_point(ball, t+1)
- ;;
- compute_end_point({pos = {x = 0.; y = 100.}; speed = {x = 10.; y = 10.}}, 0);;
- (* exemple for a ball *)
- #trace compute_end_point
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement