/Users/lyon/j4p/src/graphics/raytracers/tracer/geometry/Vector3d.java
|
1 package graphics.raytracers.tracer.geometry;
2
3 final public class Vector3d {
4 private double x, y, z;
5
6 public Vector3d(double a, double b, double c) {
7 x = a;
8 y = b;
9 z = c;
10 }
11
12 public Vector3d(Vector3d a) {
13 x = a.x;
14 y = a.y;
15 z = a.z;
16 }
17
18 public Vector3d() {
19 x = 0.0;
20 y = 0.0;
21 z = 0.0;
22 }
23
24 public static Vector3d add(Vector3d a, Vector3d b) {
25 return new Vector3d(a.x + b.x, a.y + b.y, a.z + b.z);
26 }
27
28 public static Vector3d adds(double s, Vector3d a, Vector3d b) {
29 return new Vector3d(s * a.x + b.x, s * a.y + b.y, s * a.z + b.z);
30 }
31
32 public static Vector3d sub(Vector3d a, Vector3d b) {
33 return new Vector3d(a.x - b.x, a.y - b.y, a.z - b.z);
34 }
35
36 public static Vector3d mult(Vector3d a, Vector3d b) {
37 return new Vector3d(a.x * b.x, a.y * b.y, a.z * b.z);
38 }
39
40 public static Vector3d cross(Vector3d a, Vector3d b) {
41 return
42 new Vector3d(a.y * b.z - a.z * b.y,
43 a.z * b.x - a.x * b.z,
44 a.x * b.y - a.y * b.x);
45 }
46
47 public static double dot(Vector3d a, Vector3d b) {
48 return a.x * b.x + a.y * b.y + a.z * b.z;
49 }
50
51 public static Vector3d comb(double a, Vector3d A, double b, Vector3d B) {
52 return
53 new Vector3d(a * A.x + b * B.x,
54 a * A.y + b * B.y,
55 a * A.z + b * B.z);
56 }
57
58 public void scale(double t) {
59 x *= t;
60 y *= t;
61 z *= t;
62 }
63
64 public void negate() {
65 x = -x;
66 y = -y;
67 z = -z;
68 }
69
70 public double normalize() {
71 double len;
72 len = Math.sqrt(x * x + y * y + z * z);
73 if (len > 0.0) {
74 x /= len;
75 y /= len;
76 z /= len;
77 }
78 return len;
79 }
80
81 public String toString() {
82 return "<" + x + "," + y + "," + z + ">";
83 }
84
85 public double getX() {
86 return x;
87 }
88
89 public void setX(double x) {
90 this.x = x;
91 }
92
93 public double getY() {
94 return y;
95 }
96
97 public void setY(double y) {
98 this.y = y;
99 }
100
101 public double getZ() {
102 return z;
103 }
104
105 public void setZ(double z) {
106 this.z = z;
107 }
108 }
109