Subversion Repositories AndroidProjects

Rev

Rev 835 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
835 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
        {
1001 chris 16
                set(_x, _y, _z);
17
        }
18
 
19
        public void set(float _x, float _y, float _z)
20
        {
835 chris 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 boolean 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
 
126
 
127
 
128