Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 200 | chris | 1 | package com.gebauz.Bauzoid.math; |
| 2 | |||
| 3 | public class Vector3 |
||
| 4 | { |
||
| 5 | |||
| 6 | public float x; |
||
| 7 | public float y; |
||
| 8 | public float z; |
||
| 9 | |||
| 10 | public Vector3() |
||
| 11 | { |
||
| 12 | } |
||
| 13 | |||
| 14 | public Vector3(float _x, float _y, float _z) |
||
| 15 | { |
||
| 16 | x = _x; |
||
| 17 | y = _y; |
||
| 18 | z = _z; |
||
| 19 | } |
||
| 20 | |||
| 21 | public void normalize() |
||
| 22 | { |
||
| 23 | float len = length(); |
||
| 24 | if (len != 0.0f) |
||
| 25 | { |
||
| 26 | x /= len; |
||
| 27 | y /= len; |
||
| 28 | z /= len; |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | public float length() |
||
| 33 | { |
||
| 34 | float squared = squaredLength(); |
||
| 35 | return (float)Math.sqrt(squared); |
||
| 36 | } |
||
| 37 | |||
| 38 | public void setLength(float newLength) |
||
| 39 | { |
||
| 40 | normalize(); |
||
| 41 | x *= newLength; |
||
| 42 | y *= newLength; |
||
| 43 | z *= newLength; |
||
| 44 | } |
||
| 45 | |||
| 46 | public boolean isZero() |
||
| 47 | { |
||
| 48 | return ((x == 0.0f) && (y == 0.0f) && (z == 0.0f)); |
||
| 49 | } |
||
| 50 | |||
| 51 | public float squaredLength() |
||
| 52 | { |
||
| 53 | return (x*x + y*y + z*z); |
||
| 54 | } |
||
| 55 | |||
| 56 | public void addVector(Vector3 v) |
||
| 57 | { |
||
| 58 | x += v.x; |
||
| 59 | y += v.y; |
||
| 60 | z += v.z; |
||
| 61 | } |
||
| 62 | |||
| 63 | public void subtractVector(Vector3 v) |
||
| 64 | { |
||
| 65 | x -= v.x; |
||
| 66 | y -= v.y; |
||
| 67 | z -= v.z; |
||
| 68 | } |
||
| 69 | |||
| 70 | public float dotProduct(Vector3 v) |
||
| 71 | { |
||
| 72 | return dotProduct(this, v); |
||
| 73 | } |
||
| 74 | |||
| 75 | static public float dotProduct(Vector3 a, Vector3 b) |
||
| 76 | { |
||
| 77 | return (a.x*b.x + a.y*b.y + a.z*b.z); |
||
| 78 | } |
||
| 79 | |||
| 80 | public Vector3 crossProduct(Vector3 v) |
||
| 81 | { |
||
| 82 | return crossProduct(this, v); |
||
| 83 | } |
||
| 84 | |||
| 85 | static public Vector3 crossProduct(Vector3 a, Vector3 b) |
||
| 86 | { |
||
| 87 | return new Vector3( |
||
| 88 | (a.y * b.z) - (a.z * b.y), |
||
| 89 | (a.z * b.x) - (a.x * b.z), |
||
| 90 | (a.x * b.y) - (a.y * b.x)); |
||
| 91 | } |
||
| 92 | |||
| 93 | static public float distance(Vector3 a, Vector3 b) |
||
| 94 | { |
||
| 95 | Vector3 c = new Vector3(b.x - a.x, b.y - a.y, b.z - a.z); |
||
| 96 | return c.length(); |
||
| 97 | } |
||
| 98 | |||
| 99 | static public Vector3 add(Vector3 a, Vector3 b) |
||
| 100 | { |
||
| 101 | return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z); |
||
| 102 | } |
||
| 103 | |||
| 104 | static public Vector3 subtract(Vector3 a, Vector3 b) |
||
| 105 | { |
||
| 106 | return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z); |
||
| 107 | } |
||
| 108 | |||
| 109 | static public float length(float x, float y, float z) |
||
| 110 | { |
||
| 111 | return (float)Math.sqrt(x*x + y*y + z*z); |
||
| 112 | } |
||
| 113 | |||
| 114 | } |
||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 |