1
2
3
4
5 package math.geom3d;
6
7 import math.geom3d.transform.AffineTransform3D;
8
9
10
11
12
13 public class Vector3D {
14
15
16
17
18 protected double x = 1;
19 protected double y = 0;
20 protected double z = 0;
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public final static double dotProduct(Vector3D v1, Vector3D v2) {
35 return v1.getX()*v2.getX()+v1.getY()*v2.getY()+v1.getZ()*v2.getZ();
36 }
37
38
39
40
41
42
43 public final static Vector3D crossProduct(Vector3D v1, Vector3D v2) {
44 return new Vector3D(v1.y*v2.z-v1.z*v2.y, v1.z*v2.x-v1.x*v2.z, v1.x*v2.y
45 -v1.y*v2.x);
46 }
47
48
49
50
51
52
53 public final static boolean isColinear(Vector3D v1, Vector3D v2) {
54 return Vector3D.crossProduct(v1.getNormalizedVector(),
55 v2.getNormalizedVector()).getNorm()<Shape3D.ACCURACY;
56 }
57
58
59
60
61
62
63 public final static boolean isOrthogonal(Vector3D v1, Vector3D v2) {
64 return Vector3D.dotProduct(v1.getNormalizedVector(), v2
65 .getNormalizedVector())<Shape3D.ACCURACY;
66 }
67
68
69
70
71
72 public Vector3D() {
73 this(1, 0, 0);
74 }
75
76
77 public Vector3D(double x, double y, double z) {
78 this.x = x;
79 this.y = y;
80 this.z = z;
81 }
82
83
84
85
86 public Vector3D(Point3D point) {
87 this(point.getX(), point.getY(), point.getZ());
88 }
89
90
91
92
93 public Vector3D(Point3D point1, Point3D point2) {
94 this(point2.getX()-point1.getX(), point2.getY()-point1.getY(), point2.getZ()-point1.getZ());
95 }
96
97
98
99
100 public double getX() {
101 return x;
102 }
103
104 public void setX(double x) {
105 this.x = x;
106 }
107
108 public double getY() {
109 return y;
110 }
111
112 public void setY(double y) {
113 this.y = y;
114 }
115
116 public double getZ() {
117 return z;
118 }
119
120 public void setZ(double z) {
121 this.z = z;
122 }
123
124 public void setVector(double x, double y, double z) {
125 this.x = x;
126 this.y = y;
127 this.z = z;
128 }
129
130
131
132
133
134
135
136
137 public Vector3D plus(Vector3D v) {
138 return new Vector3D(x+v.x, y+v.y, z+v.z);
139 }
140
141
142
143
144
145 public Vector3D minus(Vector3D v) {
146 return new Vector3D(x-v.x, y-v.y, z-v.z);
147 }
148
149
150
151
152 public Vector3D times(double k) {
153 return new Vector3D(k*x, k*y, k*z);
154 }
155
156
157
158
159
160
161
162
163
164
165 public Vector3D getOpposite() {
166 return new Vector3D(-x, -y, -z);
167 }
168
169
170
171
172
173
174 public double getNorm() {
175 return Math.hypot(Math.hypot(x, y), z);
176 }
177
178
179
180
181
182 public double getLength() {
183 return getNorm();
184 }
185
186
187
188
189
190
191
192 public double getNormSq() {
193 return x*x+y*y+z*z;
194 }
195
196
197
198
199 public void normalize() {
200 double r = this.getNorm();
201 this.x = this.x/r;
202 this.y = this.y/r;
203 this.z = this.z/r;
204 }
205
206
207
208
209
210 public Vector3D getNormalizedVector() {
211 double r = this.getNorm();
212 return new Vector3D(this.x/r, this.y/r, this.z/r);
213 }
214
215
216
217
218
219
220
221
222 public Vector3D transform(AffineTransform3D trans) {
223 double[] tab = trans.getCoefficients();
224 return new Vector3D(x*tab[0]+y*tab[1]+z*tab[2], x*tab[4]+y*tab[5]+z
225 *tab[6], x*tab[8]+y*tab[9]+z*tab[10]);
226 }
227
228
229
230
231 @Override
232 public boolean equals(Object obj) {
233 if (!(obj instanceof Vector3D))
234 return false;
235
236 Vector3D v = (Vector3D) obj;
237 if (Math.abs(x-v.x)>Shape3D.ACCURACY)
238 return false;
239 if (Math.abs(y-v.y)>Shape3D.ACCURACY)
240 return false;
241 if (Math.abs(z-v.z)>Shape3D.ACCURACY)
242 return false;
243 return true;
244 }
245
246 }