Вектор в пространстве
- public class VectorG {
- public float x,y,z;
- public VectorG(float x, float y, float z) {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public void sum(float x, float y, float z) {
- this.x += x;
- this.y += y;
- this.z += z;
- }
- public void sum(VectorG vec) {
- x += vec.x;
- y += vec.y;
- y += vec.y;
- }
- public void not() {
- x = -x;
- y = -y;
- z = -z;
- }
- public static VectorG sum(VectorG v1, VectorG v2) {
- return new VectorG(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
- }
- public static VectorG not(VectorG vec) {
- return new VectorG(-vec.x, -vec.y, -vec.z);
- }
- public void set(float x, float y, float z) {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public void set(float[] coords) {
- x = coords[0];
- y = coords[1];
- z = coords[2];
- }
- public float[] get() {
- return new float[]{x,y,z};
- }
- public float getValue() {
- return (float) Math.sqrt(x * x + y * y + z * z);
- }
- public void setValue(float val) {
- if (getValue() != 0) {
- float k = val / getValue();
- scale(k);
- }
- }
- public void scale(float k) {
- x *= k;
- y *= k;
- z *= k;
- }
- public void scale(float x, float y, float z) {
- this.x *= x;
- this.y *= y;
- this.z *= z;
- }
- public Object clone() {
- return new VectorG(x, y, z);
- }
- public float angle(VectorG vec) {
- float a = getValue();
- float b = vec.getValue();
- float c = sum(this, not(vec)).getValue();
- return (float) Math.toDegrees(Math.acos((a * a + (b + c) * (b - c)) / (2.0 * a * b)));
- }
- public float cosin(VectorG vec) {
- float a = getValue();
- float b = vec.getValue();
- float c = sum(this, not(vec)).getValue();
- return (a * a + (b + c) * (b - c)) / (2.0f * a * b);
- }
- public void reflect(VectorG normal) {
- VectorG normalForProcess =(VectorG) normal.clone();
- if (angle(normalForProcess) >= 90) normalForProcess.not();
- normalForProcess.setValue(getValue() * cosin(normalForProcess));
- normalForProcess.scale(2);
- sum(normalForProcess);
- }
- public boolean isPerpendicular(VectorG vec) {
- return MathUtil.eq(0, cosin(vec));
- }
- public boolean isOpposite(VectorG vec) {
- return cosin(vec) < 0;
- }
- public void setDirection(float xy, float xz) {
- float value=getValue();
- float yr=(float) Math.sin(Math.toRadians(xy)) * value;
- float xr=(float) Math.cos(Math.toRadians(xy)) * (float)Math.cos(Math.toRadians(xz)) * value;
- float zr=(float) Math.cos(Math.toRadians(xy)) * (float)Math.sin(Math.toRadians(xz)) * value;
- set(xr, yr, zr);
- }
- private static class MathUtil {
- private static float eps=(float)1e-5;
- public static boolean eq(float a, float b) {
- return Math.abs(a - b) <= eps;
- }
- }
- }