You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1.9 KiB

project_motion

Instructions

For this exercise you will have to create a projectile motion.

A structure called Object will be provided which will have all variables that are essential for the projectile physics. (distance, velocity, height, time)

You must implement :

  • A function throw_object that will initialize the Object with a given velocity and height.
  • The trait Iterator with the .next() in which,the next position of the object after 1 second, must be calculated. It will return an Option with the Object or it will return None if the object already reached the floor.

Notions

Expected Function


#[derive(Debug, Clone, PartialEq)]
pub struct Object {
    pub distance: f32,
    pub velocity: f32,
    pub height: f32,
    pub time: f32,
}

impl Object {
    pub fn throw_object(velocity: f32, height: f32) -> Object {}
}

impl Iterator for Object {
  // next
}

Usage

Here is a program to test your function

use project_motion::*;

fn main() {
    let mut obj = Object::throw_object(50.0, 150.0);
    println!("{:?}", obj.next());
    println!("{:?}", obj.next());
    println!("{:?}", obj.next());
    println!("{:?}", obj.next());
    println!("{:?}", obj.next());
    println!("{:?}", obj.next());
}

And its output:

student@ubuntu:~/project_motion/test$ cargo run
Some(Object { distance: 50.0, velocity: 50.0, height: 145.1, time: 1.0 })
Some(Object { distance: 100.0, velocity: 50.0, height: 125.5, time: 2.0 })
Some(Object { distance: 150.0, velocity: 50.0, height: 81.4, time: 3.0 })
Some(Object { distance: 200.0, velocity: 50.0, height: 3.0, time: 4.0 })
None
None
student@ubuntu:~/project_motion/test$