-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector4f.java
145 lines (118 loc) · 3.22 KB
/
Vector4f.java
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
137
138
139
140
141
142
143
144
145
public class Vector4f
{
private final float x;
private final float y;
private final float z;
private final float w;
public Vector4f(float x, float y, float z, float w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public float Length()
{
return (float)Math.sqrt(x * x + y * y + z * z + w * w);
}
public float Max()
{
return Math.max(Math.max(x, y), Math.max(z, w));
}
public float Dot(Vector4f r)
{
return x * r.GetX() + y * r.GetY() + z * r.GetZ() + w * r.GetW();
}
public Vector4f Cross(Vector4f r)
{
float x_ = y * r.GetZ() - z * r.GetY();
float y_ = z * r.GetX() - x * r.GetZ();
float z_ = x * r.GetY() - y * r.GetX();
return new Vector4f(x_, y_, z_, 0);
}
public Vector4f Normalized()
{
float length = Length();
return new Vector4f(x / length, y / length, z / length, w / length);
}
public Vector4f Rotate(Vector4f axis, float angle)
{
float sinAngle = (float)Math.sin(-angle);
float cosAngle = (float)Math.cos(-angle);
return this.Cross(axis.Mul(sinAngle)).Add( //Rotation on local X
(this.Mul(cosAngle)).Add( //Rotation on local Z
axis.Mul(this.Dot(axis.Mul(1 - cosAngle))))); //Rotation on local Y
}
// public Vector3f rotate(Quaternion rotation)
// {
// Quaternion conjugate = rotation.conjugate();
//
// Quaternion w = rotation.mul(this).mul(conjugate);
//
// return new Vector3f(w.getX(), w.getY(), w.getZ());
// }
public Vector4f Lerp(Vector4f dest, float lerpFactor)
{
return dest.Sub(this).Mul(lerpFactor).Add(this);
}
public Vector4f Add(Vector4f r)
{
return new Vector4f(x + r.GetX(), y + r.GetY(), z + r.GetZ(), w + r.GetW());
}
public Vector4f Add(float r)
{
return new Vector4f(x + r, y + r, z + r, w + r);
}
public Vector4f Sub(Vector4f r)
{
return new Vector4f(x - r.GetX(), y - r.GetY(), z - r.GetZ(), w - r.GetW());
}
public Vector4f Sub(float r)
{
return new Vector4f(x - r, y - r, z - r, w - r);
}
public Vector4f Mul(Vector4f r)
{
return new Vector4f(x * r.GetX(), y * r.GetY(), z * r.GetZ(), w * r.GetW());
}
public Vector4f Mul(float r)
{
return new Vector4f(x * r, y * r, z * r, w * r);
}
public Vector4f Div(Vector4f r)
{
return new Vector4f(x / r.GetX(), y / r.GetY(), z / r.GetZ(), w / r.GetW());
}
public Vector4f Div(float r)
{
return new Vector4f(x / r, y / r, z / r, w / r);
}
public Vector4f Abs()
{
return new Vector4f(Math.abs(x), Math.abs(y), Math.abs(z), Math.abs(w));
}
public String toString()
{
return "(" + x + ", " + y + ", " + z + ", " + w + ")";
}
public float GetX()
{
return x;
}
public float GetY()
{
return y;
}
public float GetZ()
{
return z;
}
public float GetW()
{
return w;
}
public boolean equals(Vector4f r)
{
return x == r.GetX() && y == r.GetY() && z == r.GetZ() && w == r.GetW();
}
}