| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.geo.math; |
| 27 |
|
| 28 |
public final class Vector3D { |
| 29 |
|
| 30 |
|
| 31 |
protected double x; |
| 32 |
|
| 33 |
protected double y; |
| 34 |
|
| 35 |
protected double z; |
| 36 |
|
| 37 |
|
| 38 |
public static final Vector3D ORIGIN = new Vector3D(0.0, 0.0, 0.0); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public Vector3D(double x, double y, double z) { |
| 47 |
this.x = x; |
| 48 |
this.y = y; |
| 49 |
this.z = z; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public Vector3D(Vector3D a) { |
| 56 |
this.x = a.x; |
| 57 |
this.y = a.y; |
| 58 |
this.z = a.z; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public Vector3D add(Vector3D a) { |
| 67 |
return (new Vector3D(x + a.x, y + a.y, z + a.z)); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public Vector3D addU(Vector3D a) { |
| 76 |
x += a.x; |
| 77 |
y += a.y; |
| 78 |
z += a.z; |
| 79 |
return (this); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public Vector3D cross(Vector3D a) { |
| 86 |
return (new Vector3D(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public double dot(Vector3D a) { |
| 93 |
return (x * a.x + y * a.y + z * a.z); |
| 94 |
} |
| 95 |
|
| 96 |
public void negate() { |
| 97 |
x = -x; |
| 98 |
y = -y; |
| 99 |
z = -z; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public boolean equals(Object a) { |
| 106 |
if (!(a instanceof Vector3D)) |
| 107 |
return (false); |
| 108 |
return ((x == ((Vector3D) a).x) && (x == ((Vector3D) a).y) && (x == ((Vector3D) a).z)); |
| 109 |
} |
| 110 |
|
| 111 |
public Vector3D mirror(Vector3D a) { |
| 112 |
return (this.add(a.mul(-2.0 * a.dot(this)))); |
| 113 |
} |
| 114 |
|
| 115 |
public double getX() { return x; } |
| 116 |
public double getY() { return y; } |
| 117 |
public double getZ() { return z; } |
| 118 |
|
| 119 |
public double getCoordinate(int index) { |
| 120 |
|
| 121 |
switch (index) { |
| 122 |
case 0: return x; |
| 123 |
case 1: return y; |
| 124 |
case 2: return z; |
| 125 |
default: throw new IllegalArgumentException("illegal coordinates:" + index); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public double modulus() { |
| 134 |
return (Math.sqrt(x * x + y * y + z * z)); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public double mod2() { |
| 142 |
return (x * x + y * y + z * z); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public Vector3D mul(double a) { |
| 149 |
return (new Vector3D(x * a, y * a, z * a)); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public Vector3D mulU(double a) { |
| 158 |
x *= a; |
| 159 |
y *= a; |
| 160 |
z *= a; |
| 161 |
return (this); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public Vector3D sub(Vector3D a) { |
| 168 |
return (new Vector3D(x - a.x, y - a.y, z - a.z)); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public Vector3D subU(Vector3D a) { |
| 175 |
x -= a.x; |
| 176 |
y -= a.y; |
| 177 |
z -= a.z; |
| 178 |
return (this); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public Vector3D normalized() { |
| 185 |
return (this.mul(1.0 / this.modulus())); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public Vector3D normalize() { |
| 192 |
double im = 1.0 / this.modulus(); |
| 193 |
this.x *= im; |
| 194 |
this.y *= im; |
| 195 |
this.z *= im; |
| 196 |
return (this); |
| 197 |
} |
| 198 |
|
| 199 |
public String toString() { |
| 200 |
return ("Vector3D[" + x + "," + y + "," + z + "]"); |
| 201 |
} |
| 202 |
|
| 203 |
} |