forked from regelneef/necessities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3.java
More file actions
173 lines (137 loc) · 3.98 KB
/
Vector3.java
File metadata and controls
173 lines (137 loc) · 3.98 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package kdx7214.necessities;
public class Vector3 implements Comparable<Vector3> {
protected double x, y, z ;
public Vector3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Vector3(int x, int y, int z) {
this.x = (double) x ;
this.y = (double) y ;
this.z = (double) z ;
}
public Vector3(float x, float y, float z) {
this.x = (double) x ;
this.y = (double) y ;
this.z = (double) z ;
}
public Vector3(Vector3 v) {
this.x = v.x ;
this.y = v.y ;
this.z = v.z ;
}
public Vector3() {
this.x = this.y = this.z = 0.0 ;
}
public double getX() {
return x ;
}
public double getY() {
return y ;
}
public double getZ() {
return z ;
}
public void setX(double x) {
this.x = x ;
}
public void setY(double y) {
this.y = y ;
}
public void setZ(double z) {
this.z = z ;
}
public void setX(int x) {
this.x = (double) x ;
}
public void setY(int y) {
this.y = (double) y ;
}
public void setZ(int z) {
this.z = (double) z ;
}
public void setX(float x) {
this.x = (double) x ;
}
public void setY(float y) {
this.y = (double) y ;
}
public void setZ(float z) {
this.z = (double) z ;
}
public Vector3 add(Vector3 other) {
return new Vector3(this.x + other.x, this.y + other.y, this.z + other.z) ;
}
public Vector3 add(Vector3... others) {
double newX = x, newY = y, newZ = z ;
for (int i = 0; i < others.length; ++i) {
newX += others[i].x ;
newY += others[i].y ;
newZ += others[i].z ;
}
return new Vector3(newX, newY, newZ) ;
}
public Vector3 subtract(Vector3 other) {
return new Vector3(x - other.x, y - other.y, z - other.z) ;
}
public Vector3 subtract(Vector3... others) {
double newX = x, newY = y, newZ = z ;
for (int i = 0; i < others.length; ++i) {
newX -= others[i].x ;
newY -= others[i].y ;
newZ -= others[i].z ;
}
return new Vector3(newX, newY, newZ) ;
}
public Vector3 multiply(Vector3 other) {
return new Vector3(x * other.x, y * other.y, z * other.z) ;
}
public Vector3 scale(double amount) {
return new Vector3(x * amount, y * amount, z * amount) ;
}
public Vector3 scale(float amount) {
return new Vector3(x * (double)amount, y * (double)amount, z * (double)amount) ;
}
public Vector3 scale(int amount) {
return new Vector3(x * (double)amount, y * (double)amount, z * (double)amount) ;
}
public double length() {
return Math.sqrt(x * x + y * y + z * z) ;
}
public double distance(Vector3 other) {
double tempx, tempy, tempz ;
tempx = other.x - x ;
tempy = other.y - y ;
tempz = other.z - z ;
return Math.sqrt(tempx * tempx + tempy * tempy + tempz * tempz) ;
}
public Vector3 normalize() {
return scale(1.0 / length()) ;
}
public double dot(Vector3 other) {
return x * other.x + y * other.y + z * other.z ;
}
public Vector3 cross(Vector3 other) {
return new Vector3(
y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x
) ;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Vector3)) {
return false ;
}
Vector3 other = (Vector3) obj ;
return (other.x == this.x) && (other.y == this.y) && (other.z == this.z) ;
}
@Override
public int compareTo(Vector3 other) {
if (y != other.y) return Double.compare(y, other.y) ;
if (z != other.z) return Double.compare(z, other.z) ;
if (x != other.x) return Double.compare(x, other.x) ;
return 0;
}
} // public class Vector3