Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std;
- type vec2 = {x: float, y: float};
- type vec3 = {x: float, y: float, z: float};
- iface vector_math<T> {
- fn length() -> float;
- fn normalize() -> T;
- }
- impl vec3_math of vector_math<vec3> for vec3 {
- fn length() -> float {
- ret float::sqrt(self.x*self.x + self.y*self.y + self.z*self.z);
- }
- fn normalize() -> vec3 {
- let l = 1. / self.length();
- ret {x: self.x*l, y: self.y*l, z: self.z*l};
- }
- }
- fn length<T, I: vector_math<T> >(a: I) -> float { a.length() }
- fn normalize<T, I: vector_math<T> >(a: I) -> T { a.normalize() }
- fn main() {
- let v = length({x: 10., y: 10., z: 5.});
- }
Advertisement
Add Comment
Please, Sign In to add comment