Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 0.58 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }