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