📄 vector3d.java
字号:
package myprojects.graph3dapp;
class Vector3d {
public double x, y, z, h; //(x, y, z)通常表示坐标,h用于齐次坐标相乘,并保持为1
Vector3d() {
x = 0; y = 0;
z = 0; h = 1;
}
Vector3d(double x, double y, double z) {
this.x = x; this.y = y;
this.z = z; this.h = 1;
}
Vector3d(double vector[]) {
x = vector[0]; y = vector[1];
z = vector[2]; h = 1;
}
Vector3d(Vector3d v) {
this.x = v.x; this.y = v.y;
this.z = v.z; this.h = 1;
}
public void add(Vector3d v) {
x += v.x;
y += v.y;
z += v.z;
}
public static Vector3d add(Vector3d v0, Vector3d v1) {
return new Vector3d(v0.x+v1.x, v0.y+v1.y, v0.z+v1.z);
}
public void sub(Vector3d v) {
x -= v.x;
y -= v.y;
z -= v.z;
}
public static Vector3d sub(Vector3d v0, Vector3d v1) {
return new Vector3d(v0.x-v1.x, v0.y-v1.y, v0.z-v1.z);
}
public void negate() {
x = -x; y = -y;
z = -z;
}
public static Vector3d negate(Vector3d v) {
return new Vector3d(-v.x, -v.y, -v.z);
}
public double dot(Vector3d v) {
return x*v.x + y*v.y + z*v.z;
}
public void cross(Vector3d v) {
x = y*v.z - z*v.y;
y = z*v.x - x*v.z;
z = x*v.y - y*v.x;
}
public static Vector3d cross(Vector3d v0, Vector3d v1) {
return new Vector3d(v0.y*v1.z - v0.z*v1.y,
v0.z*v1.x - v0.x*v1.z,
v0.x*v1.y - v0.y*v1.x);
}
public double length() {
return (double)Math.sqrt(x*x + y*y + z*z);
}
public void normalize() {
double length = length();
x /= length; y /= length; z /= length;
}
public static Vector3d normalize(Vector3d v) {
double length = v.length();
return new Vector3d(v.x/length, v.y/length, v.z/length);
}
public void scale(double s) {
x *= s; y *= s; z *= s;
}
public static Vector3d scale(Vector3d v, double s) {
return new Vector3d(v.x*s, v.y*s, v.z*s);
}
public void mulMatrix(Matrix3d m) {
Vector3d vr = new Vector3d();
vr.x = x*m.matrix[0][0] + y*m.matrix[1][0] +
z*m.matrix[2][0] + h*m.matrix[3][0];
vr.y = x*m.matrix[0][1] + y*m.matrix[1][1] +
z*m.matrix[2][1] + h*m.matrix[3][1];
vr.z = x*m.matrix[0][2] + y*m.matrix[1][2] +
z*m.matrix[2][2] + h*m.matrix[3][2];
vr.h = x*m.matrix[0][3] + y*m.matrix[1][3] +
z*m.matrix[2][3] + h*m.matrix[3][3];
if(vr.h < 0.0005f) vr.h = 0.0005f;
x = vr.x/vr.h; y = vr.y/vr.h;
z = vr.z/vr.h; h = 1;
}
public static Vector3d mulMatrix(Vector3d v, Matrix3d m) {
Vector3d vr = new Vector3d();
vr.x = v.x*m.matrix[0][0] + v.y*m.matrix[1][0] +
v.z*m.matrix[2][0] + v.h*m.matrix[3][0];
vr.y = v.x*m.matrix[0][1] + v.y*m.matrix[1][1] +
v.z*m.matrix[2][1] + v.h*m.matrix[3][1];
vr.z = v.x*m.matrix[0][2] + v.y*m.matrix[1][2] +
v.z*m.matrix[2][2] + v.h*m.matrix[3][2];
vr.h = v.x*m.matrix[0][3] + v.y*m.matrix[1][3] +
v.z*m.matrix[2][3] + v.h*m.matrix[3][3];
if(vr.h < 0.0005f) vr.h = 0.0005f;
vr.x /= vr.h; vr.y /= vr.h;
vr.z /= vr.h; vr.h = 1;
return vr;
}
public String toString() {
return String.valueOf(x)+", "+String.valueOf(y)+", "+
String.valueOf(z)+", "+String.valueOf(h);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -