Repository navigation
Expand file tree
/
Copy pathVector3D.java
More file actions
110 lines (80 loc) · 2.03 KB
/
Copy pathVector3D.java
File metadata and controls
110 lines (80 loc) · 2.03 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
import javafx.geometry.Point3D;
import java.awt.geom.Point2D;
import java.io.Serializable;
public class Vector3D implements Serializable{
double x;
double y;
double z;
public Vector3D(Point3D v){
this.x=v.getX();
this.y=v.getY();
this.z=v.getZ();
}
public Vector3D(Point2D v){
this.x=v.getX();
this.y=0;
this.z=v.getY();
}
public Vector3D(double x, double y, double z){
this.x=x;
this.y=y;
this.z=z;
}
public Vector3D(){
this(0,0,0);
}
public double x(){
return x;
}
public double y(){
return y;
}
public double z(){
return z;
}
public double norm(){
return Math.sqrt(this.dotProduct(this));
}
public Vector3D multiply(double alpha){
return new Vector3D(x*alpha,y*alpha,z*alpha);
}
public boolean isNulVector(){
return x==0 && y==0 && z==0;
}
public Vector3D normalize(){
return (isNulVector())?this:this.multiply(1/norm());
}
public void setTo(double x,double y, double z){
this.x=x;
this.y=y;
this.z=z;
}
public String toString(){
return ("x="+x+" y="+y+" z="+z);
}
public Vector3D add(Vector3D f){
return new Vector3D(x+f.x(),y+f.y(),z+f.z());
}
public Vector3D subtract(Vector3D f){
return this.add(f.multiply(-1));
}
public double dotProduct(Vector3D f){
return x()*f.x()+y()*f.y()+z()*f.z();
}
public Vector3D crossProduct(Vector3D f){
double a=y*f.z()-z*f.y();
double b=f.x()*z-f.z()*x;
double c=x*f.y()-y*f.x();
Point3D p=new Point3D(a,b,c);
return new Vector3D(p);
}
public Vector3D posColinear(double norm){
return isNulVector()?this:multiply(norm/this.norm());
}
public Vector3D negColinear(double norm){
return posColinear(-norm);
}
public double distance(Vector3D f){
return this.subtract(f).norm();
}
}