Guest User

Untitled

a guest
Jul 20th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. use std;
  2.  
  3. type vec2 = {x: float, y: float};
  4. type vec3 = {x: float, y: float, z: float};
  5.  
  6. iface vector_math<T> {
  7. fn length() -> float;
  8. fn normalize() -> T;
  9. }
  10.  
  11. impl vec3_math of vector_math<vec3> for vec3 {
  12. fn length() -> float {
  13. ret float::sqrt(self.x*self.x + self.y*self.y + self.z*self.z);
  14. }
  15. fn normalize() -> vec3 {
  16. let l = 1. / self.length();
  17. ret {x: self.x*l, y: self.y*l, z: self.z*l};
  18. }
  19. }
  20.  
  21. fn length<T, I: vector_math<T> >(a: I) -> float { a.length() }
  22. fn normalize<T, I: vector_math<T> >(a: I) -> T { a.normalize() }
  23.  
  24. fn main() {
  25. let v = length({x: 10., y: 10., z: 5.});
  26. }
Advertisement
Add Comment
Please, Sign In to add comment