Loading src/main/java/bouncingshape/Vector3D.java +29 −17 Original line number Diff line number Diff line package bouncingshape; import java.util.Objects; public class Vector3D { //矢量在 x y, z 轴上的分量 public float x, y, z; //构造函数,传入矢量的三个分量 public Vector3D(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } //把 x, y, z赋值为另一个 Vector3D 的 x, y, z public void set(Vector3D v) { this.x=v.x; this.y=v.y; Loading @@ -23,8 +23,21 @@ public class Vector3D { this.y=y; this.z=z; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Vector3D vector3D = (Vector3D) obj; return Float.compare(vector3D.x, x) == 0 && Float.compare(vector3D.y, y) == 0 && Float.compare(vector3D.z, z) == 0; } @Override public int hashCode() { return Objects.hash(x, y, z); } //与另一个矢量相加 public void add(Vector3D v) { this.x+=v.x; this.y+=v.y; Loading @@ -43,7 +56,7 @@ public class Vector3D { this.z += v.z * scaler; } //矢量减法 public void subtract(Vector3D v) { this.x-=v.x; this.y-=v.y; Loading @@ -56,7 +69,7 @@ public class Vector3D { z -= v.z * scaler; } //矢量点积, 结果代表两个矢量之间的相似程度 public float dot(Vector3D v2){ return this.x*v2.x + this.y*v2.y + this.z*v2.z; } Loading @@ -65,7 +78,7 @@ public class Vector3D { return this.x*x + this.y*y + this.z*z; } //矢量叉积,来求一个与这两个矢量都垂直的矢量 public void cross(Vector3D v1, Vector3D v2){ x = v1.y*v2.z - v1.z*v2.y; y = v1.z*v2.x - v1.x*v2.z; Loading @@ -76,12 +89,12 @@ public class Vector3D { return new Vector3D(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); } //返回矢量的长度 public float getLength() { return (float) Math.sqrt(x * x + y * y + z * z); } //将矢量单位化 public void unit(){ float length = getLength(); x = x/length; Loading @@ -89,14 +102,14 @@ public class Vector3D { z = z/length; } //将矢量乘以一个标量 public void scale(float scalar) { x*=scalar; y*=scalar; z*=scalar; } //绕 Y 轴旋转矢量,使其顺时针旋转指定角度 public void rotate_Y(int angle){ float sin = LookupTables.sin[angle]; float cos = LookupTables.cos[angle]; Loading @@ -113,7 +126,7 @@ public class Vector3D { z = -sin*old_X + cos*old_Z; } //绕 X 轴旋转矢量,使其顺时针旋转指定角度 public void rotate_X(int angle){ float sin = LookupTables.sin[angle]; float cos = LookupTables.cos[angle]; Loading @@ -130,7 +143,7 @@ public class Vector3D { z = -sin*old_Y + cos*old_Z; } //绕 Z 轴旋转矢量,使其顺时针旋转指定角度 public void rotate_Z(int angle){ float sin = LookupTables.sin[angle]; float cos = LookupTables.cos[angle]; Loading @@ -148,7 +161,6 @@ public class Vector3D { } //将Vector3D换成字符串 public String toString() { return "(" + x + ", " + y + ", " + z + ")"; } Loading Loading
src/main/java/bouncingshape/Vector3D.java +29 −17 Original line number Diff line number Diff line package bouncingshape; import java.util.Objects; public class Vector3D { //矢量在 x y, z 轴上的分量 public float x, y, z; //构造函数,传入矢量的三个分量 public Vector3D(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } //把 x, y, z赋值为另一个 Vector3D 的 x, y, z public void set(Vector3D v) { this.x=v.x; this.y=v.y; Loading @@ -23,8 +23,21 @@ public class Vector3D { this.y=y; this.z=z; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Vector3D vector3D = (Vector3D) obj; return Float.compare(vector3D.x, x) == 0 && Float.compare(vector3D.y, y) == 0 && Float.compare(vector3D.z, z) == 0; } @Override public int hashCode() { return Objects.hash(x, y, z); } //与另一个矢量相加 public void add(Vector3D v) { this.x+=v.x; this.y+=v.y; Loading @@ -43,7 +56,7 @@ public class Vector3D { this.z += v.z * scaler; } //矢量减法 public void subtract(Vector3D v) { this.x-=v.x; this.y-=v.y; Loading @@ -56,7 +69,7 @@ public class Vector3D { z -= v.z * scaler; } //矢量点积, 结果代表两个矢量之间的相似程度 public float dot(Vector3D v2){ return this.x*v2.x + this.y*v2.y + this.z*v2.z; } Loading @@ -65,7 +78,7 @@ public class Vector3D { return this.x*x + this.y*y + this.z*z; } //矢量叉积,来求一个与这两个矢量都垂直的矢量 public void cross(Vector3D v1, Vector3D v2){ x = v1.y*v2.z - v1.z*v2.y; y = v1.z*v2.x - v1.x*v2.z; Loading @@ -76,12 +89,12 @@ public class Vector3D { return new Vector3D(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); } //返回矢量的长度 public float getLength() { return (float) Math.sqrt(x * x + y * y + z * z); } //将矢量单位化 public void unit(){ float length = getLength(); x = x/length; Loading @@ -89,14 +102,14 @@ public class Vector3D { z = z/length; } //将矢量乘以一个标量 public void scale(float scalar) { x*=scalar; y*=scalar; z*=scalar; } //绕 Y 轴旋转矢量,使其顺时针旋转指定角度 public void rotate_Y(int angle){ float sin = LookupTables.sin[angle]; float cos = LookupTables.cos[angle]; Loading @@ -113,7 +126,7 @@ public class Vector3D { z = -sin*old_X + cos*old_Z; } //绕 X 轴旋转矢量,使其顺时针旋转指定角度 public void rotate_X(int angle){ float sin = LookupTables.sin[angle]; float cos = LookupTables.cos[angle]; Loading @@ -130,7 +143,7 @@ public class Vector3D { z = -sin*old_Y + cos*old_Z; } //绕 Z 轴旋转矢量,使其顺时针旋转指定角度 public void rotate_Z(int angle){ float sin = LookupTables.sin[angle]; float cos = LookupTables.cos[angle]; Loading @@ -148,7 +161,6 @@ public class Vector3D { } //将Vector3D换成字符串 public String toString() { return "(" + x + ", " + y + ", " + z + ")"; } Loading