/Users/lyon/j4p/src/graphics/raytracers/raytracer/tracer/Vec3f.java

1    package graphics.raytracers.raytracer.tracer; 
2     
3    class Vec3f { 
4        float x = 0; 
5        float y = 0; 
6        float z = 0; 
7     
8        public Vec3f(float ix, float iy, float iz) { 
9            x = ix; 
10           y = iy; 
11           z = iz; 
12       } 
13    
14       public Vec3f(Vec3f v) { 
15           x = v.x; 
16           y = v.y; 
17           z = v.z; 
18       } 
19    
20       public void set(float nx, float ny, float nz) { 
21           x = nx; 
22           y = ny; 
23           z = nz; 
24       } 
25    
26       public float getLength() { 
27           return 
28                   (float) Math.sqrt(x * x + y * y + z * z); 
29       } 
30    
31       public void normalize() { 
32           float length = getLength(); 
33    
34           try { 
35               x = x / length; 
36               y = y / length; 
37               z = z / length; 
38           } catch (ArithmeticException e) { 
39               System.out.println(e.getMessage()); 
40               e.printStackTrace(); 
41           } 
42       } 
43    
44       public float dotProduct(Vec3f v) { 
45           return ((x * v.x + y * v.y + z * v.z)); 
46       } 
47    
48       // this = this crosses v 
49       public void crossProduct(Vec3f v) { 
50           float tmpx = y * v.z - z * v.y, 
51                   tmpy = z * v.x - x * v.z, 
52                   tmpz = x * v.y - y * v.x; 
53           x = tmpx; 
54           y = tmpy; 
55           z = tmpz; 
56       } 
57    
58       public void mult(float factor) { 
59           x = x * factor; 
60           y = y * factor; 
61           z = z * factor; 
62       } 
63    
64       public void add(Vec3f v) { 
65           x = x + v.x; 
66           y = y + v.y; 
67           z = z + v.z; 
68       } 
69    
70       // subtracts v from this vector 
71       public void sub(Vec3f v) { 
72           x = x - v.x; 
73           y = y - v.y; 
74           z = z - v.z; 
75       } 
76    
77       public String toString() { 
78           String res = new String("[" + x + ", " + y + ", " + z + "]"); 
79           return res; 
80       } 
81   } 
82