-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector4.ts
168 lines (135 loc) · 4.51 KB
/
vector4.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { ReadonlySimpleVector4, SimpleVector4 } from "./simple-vector4";
export class Vector4 implements SimpleVector4 {
public constructor(
public x = 0,
public y = 0,
public z = 0,
public w = 0,
) {
}
public static get ZERO(): Vector4 {
return new Vector4(0, 0, 0, 0);
}
public static get ONE(): Vector4 {
return new Vector4(1, 1, 1, 1);
}
public static fromArray(val: [number, number, number, number] | Float32Array): Vector4 {
return new Vector4(val[0], val[1], val[2], val[3]);
}
public static from(valA: number, valB = valA, valC = valB, valD = valC): Vector4 {
return new Vector4(valA, valB, valC, valD);
}
public get avg(): number {
return (this.x + this.y + this.z + this.w) / 4;
}
public get length(): number {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
}
public static equals(vecA: ReadonlySimpleVector4, vecB: ReadonlySimpleVector4): boolean {
if (vecA === vecB) {
return true;
}
return vecA.x === vecB.x && vecA.y === vecB.y && vecA.z === vecB.z && vecA.w === vecB.w;
}
public static equalsApproximately (vecA: ReadonlySimpleVector4, vecB: ReadonlySimpleVector4, EPSILON = 0.0000001): boolean {
if (vecA === vecB) {
return true;
}
const equal = (a:number, b: number): boolean => Math.abs(a - b) <= EPSILON * Math.max(1, Math.abs(a), Math.abs(b));
return equal(vecA.x, vecB.x) && equal(vecA.y, vecB.y) && equal(vecA.z, vecB.z) && equal(vecA.w, vecB.w);
}
public static min(vecA: ReadonlySimpleVector4, vecB: ReadonlySimpleVector4): Vector4 {
return new Vector4(
Math.min(vecA.x, vecB.x),
Math.min(vecA.y, vecB.y),
Math.min(vecA.z, vecB.z),
Math.min(vecA.w, vecB.w),
);
}
public static max(vecA: ReadonlySimpleVector4, vecB: ReadonlySimpleVector4): Vector4 {
return new Vector4(
Math.max(vecA.x, vecB.x),
Math.max(vecA.y, vecB.y),
Math.max(vecA.z, vecB.z),
Math.max(vecA.w, vecB.w),
);
}
public static dist(vecA: ReadonlySimpleVector4, vecB: ReadonlySimpleVector4): number {
return Math.sqrt(
Math.pow(vecA.x - vecB.x, 2) +
Math.pow(vecA.y - vecB.y, 2) +
Math.pow(vecA.z - vecB.z, 2) +
Math.pow(vecA.w - vecB.w, 2),
);
}
public static normalize<T extends SimpleVector4>(vec: T): T {
const length = Math.sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w);
vec.x /= length;
vec.y /= length;
vec.z /= length;
vec.w /= length;
return vec;
}
public static isVector(item: any): item is SimpleVector4 {
return item && !isNaN(item.x) && !isNaN(item.y) && !isNaN(item.z) && !isNaN(item.w);
}
public toArray(): [number, number, number, number] {
return [this.x, this.y, this.z, this.w];
}
public getNormalized(): SimpleVector4 {
return this.clone().normalize();
}
public clone(): Vector4 {
return new Vector4(this.x, this.y, this.z, this.w);
}
public normalize(): this {
const length = this.length;
this.x /= length;
this.y /= length;
this.z /= length;
this.w /= length;
return this;
}
public mul(value: ReadonlySimpleVector4 | number): this {
if (typeof value === "number") {
this.x *= value;
this.y *= value;
this.z *= value;
this.w *= value;
} else {
this.x *= value.x;
this.y *= value.y;
this.z *= value.z;
this.w *= value.w;
}
return this;
}
public add(vec: ReadonlySimpleVector4): this {
this.x += vec.x;
this.y += vec.y;
this.z += vec.z;
this.w += vec.w;
return this;
}
public sub(vec: ReadonlySimpleVector4): this {
this.x -= vec.x;
this.y -= vec.y;
this.z -= vec.z;
this.w -= vec.w;
return this;
}
public setData(x: number, y: number, z: number, w: number): this {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
return this;
}
public set(vec: ReadonlySimpleVector4): this {
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
this.w = vec.w;
return this;
}
}