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