Rev 782 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 782 | chris | 1 | using System; |
| 2 | using System.Collections.Generic; |
||
| 3 | using System.Linq; |
||
| 4 | using System.Text; |
||
| 5 | using System.Threading.Tasks; |
||
| 6 | |||
| 7 | namespace BauzoidNET.math |
||
| 8 | { |
||
| 787 | chris | 9 | public class Vector3 |
| 782 | chris | 10 | { |
| 11 | public float x; |
||
| 12 | public float y; |
||
| 13 | public float z; |
||
| 14 | |||
| 15 | public Vector3() |
||
| 16 | { |
||
| 17 | } |
||
| 18 | |||
| 19 | public Vector3(float _x, float _y, float _z) |
||
| 20 | { |
||
| 21 | x = _x; |
||
| 22 | y = _y; |
||
| 23 | z = _z; |
||
| 24 | } |
||
| 25 | |||
| 26 | public void normalize() |
||
| 27 | { |
||
| 28 | float len = length(); |
||
| 29 | if (len != 0.0f) |
||
| 30 | { |
||
| 31 | x /= len; |
||
| 32 | y /= len; |
||
| 33 | z /= len; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | public float length() |
||
| 38 | { |
||
| 39 | float squared = squaredLength(); |
||
| 40 | return (float)Math.Sqrt(squared); |
||
| 41 | } |
||
| 42 | |||
| 43 | public void setLength(float newLength) |
||
| 44 | { |
||
| 45 | normalize(); |
||
| 46 | x *= newLength; |
||
| 47 | y *= newLength; |
||
| 48 | z *= newLength; |
||
| 49 | } |
||
| 50 | |||
| 51 | public bool isZero() |
||
| 52 | { |
||
| 53 | return ((x == 0.0f) && (y == 0.0f) && (z == 0.0f)); |
||
| 54 | } |
||
| 55 | |||
| 56 | public float squaredLength() |
||
| 57 | { |
||
| 58 | return (x * x + y * y + z * z); |
||
| 59 | } |
||
| 60 | |||
| 61 | public void addVector(Vector3 v) |
||
| 62 | { |
||
| 63 | x += v.x; |
||
| 64 | y += v.y; |
||
| 65 | z += v.z; |
||
| 66 | } |
||
| 67 | |||
| 68 | public void subtractVector(Vector3 v) |
||
| 69 | { |
||
| 70 | x -= v.x; |
||
| 71 | y -= v.y; |
||
| 72 | z -= v.z; |
||
| 73 | } |
||
| 74 | |||
| 75 | public float dotProduct(Vector3 v) |
||
| 76 | { |
||
| 77 | return dotProduct(this, v); |
||
| 78 | } |
||
| 79 | |||
| 80 | static public float dotProduct(Vector3 a, Vector3 b) |
||
| 81 | { |
||
| 82 | return (a.x * b.x + a.y * b.y + a.z * b.z); |
||
| 83 | } |
||
| 84 | |||
| 85 | public Vector3 crossProduct(Vector3 v) |
||
| 86 | { |
||
| 87 | return crossProduct(this, v); |
||
| 88 | } |
||
| 89 | |||
| 90 | static public Vector3 crossProduct(Vector3 a, Vector3 b) |
||
| 91 | { |
||
| 92 | return new Vector3( |
||
| 93 | (a.y * b.z) - (a.z * b.y), |
||
| 94 | (a.z * b.x) - (a.x * b.z), |
||
| 95 | (a.x * b.y) - (a.y * b.x)); |
||
| 96 | } |
||
| 97 | |||
| 98 | static public float distance(Vector3 a, Vector3 b) |
||
| 99 | { |
||
| 100 | Vector3 c = new Vector3(b.x - a.x, b.y - a.y, b.z - a.z); |
||
| 101 | return c.length(); |
||
| 102 | } |
||
| 103 | |||
| 104 | static public Vector3 add(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 Vector3 subtract(Vector3 a, Vector3 b) |
||
| 110 | { |
||
| 111 | return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z); |
||
| 112 | } |
||
| 113 | |||
| 114 | static public float length(float x, float y, float z) |
||
| 115 | { |
||
| 116 | return (float)Math.Sqrt(x * x + y * y + z * z); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** Return a copy of this Vector3. */ |
||
| 120 | public Vector3 copy() |
||
| 121 | { |
||
| 122 | return new Vector3(x, y, z); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |