-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.java
More file actions
37 lines (36 loc) · 840 Bytes
/
Vector.java
File metadata and controls
37 lines (36 loc) · 840 Bytes
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
27
28
29
30
31
32
33
34
35
36
37
import java.lang.Math.*;
public class Vector {
double x, y, z;
public Vector() {}
public Vector(double i, double j, double k) {
x = i;
y = j;
z = k;
}
public double magnitude() {
return Math.sqrt(x * x + y * y + z * z);
}
public void normalise() {
double mag = magnitude();
if (mag != 0) {
x /= mag;
y /= mag;
z /= mag;
}
}
public double dot(Vector a) {
return x * a.x + y * a.y + z * a.z;
}
public Vector sub(Vector a) {
return new Vector(x - a.x, y - a.y, z - a.z);
}
public Vector add(Vector a) {
return new Vector(x + a.x, y + a.y, z + a.z);
}
public Vector mul(double d) {
return new Vector(d * x, d * y, d * z);
}
public void print() {
System.out.println("x=" + x + ", y=" + y + ", z=" + z);
}
}