85d3cc34a79c98e0274befc0681884edbcae0463
[mandelwow.git] / support / vec3.rs
1 extern crate glutin;
2
3 use std::f32;
4 use std::ops::Add;
5 use std::ops::AddAssign;
6 use std::ops::Sub;
7 use std::ops::Mul;
8
9 #[derive(Default, PartialEq, Debug, Clone, Copy)]
10 pub struct Vec3 (pub f32, pub f32, pub f32);
11
12 impl Add for Vec3 {
13     type Output = Vec3;
14     fn add(self, other: Vec3) -> Vec3 {
15         Vec3(self.0 + other.0, self.1 + other.1, self.2 + other.2)
16     }
17 }
18
19 impl AddAssign for Vec3 {
20     fn add_assign(&mut self, other: Vec3) {
21         *self = Vec3(self.0 + other.0, self.1 + other.1, self.2 + other.2)
22     }
23 }
24
25 impl Sub for Vec3 {
26     type Output = Vec3;
27     fn sub(self, other: Vec3) -> Vec3 {
28         Vec3(self.0 - other.0, self.1 - other.1, self.2 - other.2)
29     }
30 }
31
32 impl Mul<f32> for Vec3 {
33     type Output = Vec3;
34     fn mul(self, f: f32) -> Vec3 {
35         Vec3(self.0 * f, self.1 * f, self.2 * f)
36     }
37 }
38
39 pub fn norm(v: &Vec3) -> Vec3 {
40     let len = (v.0 * v.0 + v.1 * v.1 + v.2 * v.2).sqrt();
41     Vec3(v.0 / len, v.1 / len, v.2 / len)
42 }