练习:几何图形

我们将为三维几何图形创建几个实用函数,将点表示为 [f64;3]。函数签名由您自行确定。

  1. // Calculate the magnitude of a vector by summing the squares of its coordinates
  2. // and taking the square root. Use the `sqrt()` method to calculate the square
  3. // root, like `v.sqrt()`.
  4. fn magnitude(...) -> f64 {
  5. todo!()
  6. }
  7. // Normalize a vector by calculating its magnitude and dividing all of its
  8. // coordinates by that magnitude.
  9. fn normalize(...) {
  10. todo!()
  11. }
  12. // Use the following `main` to test your work.
  13. fn main() {
  14. println!("Magnitude of a unit vector: {}", magnitude(&[0.0, 1.0, 0.0]));
  15. let mut v = [1.0, 2.0, 9.0];
  16. println!("Magnitude of {v:?}: {}", magnitude(&v));
  17. normalize(&mut v);
  18. println!("Magnitude of {v:?} after normalization: {}", magnitude(&v));
  19. }