-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3d.java
141 lines (135 loc) · 2.58 KB
/
Vector3d.java
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
import java.util.ArrayList;
public class Vector3d implements JSONSerializable{
public double x,y,z;
public Vector3d(){
x=0;
y=0;
z=0;
}
public Vector3d(double nx,double ny, double nz){
x=nx;
y=ny;
z=nz;
}
public Vector3d(final Vector3d old){
x=old.x;
y=old.y;
z=old.z;
}
public Vector3d(final Vector3i old){
x=old.x;
y=old.y;
z=old.z;
}
public Vector3d(final ArrayList<Number> v){
jsonDeserialize(v);
}
public Vector3d abs(){
if (x<0) x=-x;
if (y<0) y=-y;
if (z<0) z=-z;
return this;
}
public Vector3d add(final Vector3d v){
x+=v.x;
y+=v.y;
z+=v.z;
return this;
}
public Vector3d add(final Vector3i v){
x+=v.x;
y+=v.y;
z+=v.z;
return this;
}
public Vector3d add(final double f){
x+=f;
y+=f;
z+=f;
return this;
}
public Vector3d sub(final Vector3d v){
x-=v.x;
y-=v.y;
z-=v.z;
return this;
}
public Vector3d sub(final Vector3i v){
x-=v.x;
y-=v.y;
z-=v.z;
return this;
}
public Vector3d multiply(final double f){
x*=f;
y*=f;
z*=f;
return this;
}
public Vector3d multiply(final Vector3d v){
x*=v.x;
y*=v.y;
z*=v.z;
return this;
}
public Vector3d round(){
x=Math.round(x);
y=Math.round(y);
z=Math.round(z);
return this;
}
public Vector3d blend(double alphaThis, Vector3d other){
x=alphaThis*x + (1-alphaThis)*other.x;
y=alphaThis*y + (1-alphaThis)*other.y;
z=alphaThis*z + (1-alphaThis)*other.z;
return this;
}
public Vector3d cloneBlend(double alphaThis, Vector3d other){
return clone().blend(alphaThis, other);
}
public Vector3d blend(double alphaThis, Vector3i other){
x=alphaThis*x + (1-alphaThis)*other.x;
y=alphaThis*y + (1-alphaThis)*other.y;
z=alphaThis*z + (1-alphaThis)*other.z;
return this;
}
public Vector3d cloneBlend(double alphaThis, Vector3i other){
return clone().blend(alphaThis, other);
}
public Vector3i toVec3i(){
return new Vector3i(this);
}
public double length(){
return Math.sqrt(x*x+y*y+z*z);
}
public Vector3d clone(){
return new Vector3d(this);
}
public Vector3d projectOnX(){
return new Vector3d(x, 0, 0);
}
public Vector3d projectOnY(){
return new Vector3d(0, y, 0);
}
public Vector3d projectOnZ(){
return new Vector3d(0, 0, z);
}
public void assign(Vector3d v){
x = v.x;
y = v.y;
z = v.z;
}
public Object jsonSerialize() {
return new double[]{x,y,z};
}
public void jsonDeserialize(Object obj) {
ArrayList<Number> aln = (ArrayList<Number>)obj;
x=aln.get(0).doubleValue();
y=aln.get(1).doubleValue();
z=aln.get(2).doubleValue();
}
@Override
public String toString() {
return "("+x+", "+y+", "+z+")";
}
}