forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3.java
More file actions
136 lines (109 loc) · 3.1 KB
/
Vector3.java
File metadata and controls
136 lines (109 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.github.mikephil.charting.matrix;
/**
* Simple 3D vector class. Handles basic vector math for 3D vectors.
*/
public final class Vector3 {
public float x;
public float y;
public float z;
public static final Vector3 ZERO = new Vector3(0, 0, 0);
public static final Vector3 UNIT_X = new Vector3(1, 0, 0);
public static final Vector3 UNIT_Y = new Vector3(0, 1, 0);
public static final Vector3 UNIT_Z = new Vector3(0, 0, 1);
public Vector3() {
}
public Vector3(float[] array)
{
set(array[0], array[1], array[2]);
}
public Vector3(float xValue, float yValue, float zValue) {
set(xValue, yValue, zValue);
}
public Vector3(Vector3 other) {
set(other);
}
public final void add(Vector3 other) {
x += other.x;
y += other.y;
z += other.z;
}
public final void add(float otherX, float otherY, float otherZ) {
x += otherX;
y += otherY;
z += otherZ;
}
public final void subtract(Vector3 other) {
x -= other.x;
y -= other.y;
z -= other.z;
}
public final void subtractMultiple(Vector3 other, float multiplicator)
{
x -= other.x * multiplicator;
y -= other.y * multiplicator;
z -= other.z * multiplicator;
}
public final void multiply(float magnitude) {
x *= magnitude;
y *= magnitude;
z *= magnitude;
}
public final void multiply(Vector3 other) {
x *= other.x;
y *= other.y;
z *= other.z;
}
public final void divide(float magnitude) {
if (magnitude != 0.0f) {
x /= magnitude;
y /= magnitude;
z /= magnitude;
}
}
public final void set(Vector3 other) {
x = other.x;
y = other.y;
z = other.z;
}
public final void set(float xValue, float yValue, float zValue) {
x = xValue;
y = yValue;
z = zValue;
}
public final float dot(Vector3 other) {
return (x * other.x) + (y * other.y) + (z * other.z);
}
public final Vector3 cross(Vector3 other) {
return new Vector3(y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x);
}
public final float length() {
return (float) Math.sqrt(length2());
}
public final float length2() {
return (x * x) + (y * y) + (z * z);
}
public final float distance2(Vector3 other) {
float dx = x - other.x;
float dy = y - other.y;
float dz = z - other.z;
return (dx * dx) + (dy * dy) + (dz * dz);
}
public final float normalize() {
final float magnitude = length();
// TODO: I'm choosing safety over speed here.
if (magnitude != 0.0f) {
x /= magnitude;
y /= magnitude;
z /= magnitude;
}
return magnitude;
}
public final void zero() {
set(0.0f, 0.0f, 0.0f);
}
public final boolean pointsInSameDirection(Vector3 other) {
return this.dot(other) > 0;
}
}