Skip to content

Commit 27a43d5

Browse files
authored
improve nw4r/math/types.VEC3
1 parent 75fbc09 commit 27a43d5

1 file changed

Lines changed: 54 additions & 49 deletions

File tree

RVLTS/nw4r/math/types.ts

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ export class VEC3 implements _VEC3 {
6363
y: number;
6464
z: number;
6565

66-
constructor(fx: number, fy: number, fz: number) {
67-
this.x = fx;
68-
this.y = fy;
69-
this.z = fz;
66+
constructor()
67+
constructor(fx: number, fy: number, fz: number)
68+
constructor(fx?: number, fy?: number, fz?: number) {
69+
this.x = fx == undefined ? 0 : fx;
70+
this.y = fy == undefined ? 0 : fy;
71+
this.z = fz == undefined ? 0 : fz;
7072
}
7173

7274
/** Squared length of the vector. */
@@ -81,70 +83,52 @@ export class VEC3 implements _VEC3 {
8183

8284
/** + operator */
8385
__plus(val: VEC3): VEC3 {
84-
return new VEC3(
85-
this.x + val.x,
86-
this.y + val.y,
87-
this.z + val.z
88-
);
86+
let out: VEC3 = new VEC3();
87+
VEC3.add(out, this, val);
88+
return out;
8989
}
9090

9191
/** - operator */
9292
__minus(val: VEC3): VEC3 {
93-
return new VEC3(
94-
this.x - val.x,
95-
this.y - val.y,
96-
this.z - val.z
97-
);
93+
let out: VEC3 = new VEC3();
94+
VEC3.sub(out, this, val);
95+
return out;
9896
}
9997

10098
/** * operator */
101-
__multiply(val: VEC3): VEC3 {
102-
return new VEC3(
103-
this.x * val.x,
104-
this.y * val.y,
105-
this.z * val.z
106-
);
99+
__multiply(val: number): VEC3 {
100+
let out: VEC3 = new VEC3();
101+
VEC3.scale(out, this, val);
102+
return out;
107103
}
108104

109105
/** / operator */
110-
__divide(val: VEC3): VEC3 {
111-
return new VEC3(
112-
this.x / val.x,
113-
this.y / val.y,
114-
this.z / val.z
115-
);
106+
__divide(val: number): VEC3 {
107+
let r: number = 1 / val;
108+
return this.__multiply(r);
116109
}
117110

118111
/** += operator */
119112
__addAssign(val: VEC3): VEC3 {
120-
this.x += val.x;
121-
this.y += val.y;
122-
this.z += val.z;
113+
VEC3.add(this, this, val);
123114
return this;
124115
}
125116

126117
/** += operator */
127118
__minusAssign(val: VEC3): VEC3 {
128-
this.x -= val.x;
129-
this.y -= val.y;
130-
this.z -= val.z;
119+
VEC3.sub(this, this, val);
131120
return this;
132121
}
133122

134123
/** *= operator */
135-
__multiplyAssign(val: VEC3): VEC3 {
136-
this.x *= val.x;
137-
this.y *= val.y;
138-
this.z *= val.z;
124+
__multiplyAssign(val: number): VEC3 {
125+
VEC3.scale(this, this, val);
139126
return this;
140127
}
141128

142129
/** /= operator */
143-
__divideAssign(val: VEC3): VEC3 {
144-
this.x /= val.x;
145-
this.y /= val.y;
146-
this.z /= val.z;
147-
return this;
130+
__divideAssign(val: number): VEC3 {
131+
return this.__multiplyAssign(1 / val);
148132
}
149133

150134
/** == operator */
@@ -166,20 +150,41 @@ export class VEC3 implements _VEC3 {
166150
}
167151

168152
/** Dot product of two 3-dimensional vectors. */
169-
static dot(vec1: VEC3, vec2: VEC3): number {
153+
static dot(v1: VEC3, v2: VEC3): number {
170154
return (
171-
vec1.x * vec2.x +
172-
vec1.y * vec2.y +
173-
vec1.z * vec2.z
155+
v1.x * v2.x +
156+
v1.y * v2.y +
157+
v1.z * v2.z
174158
);
175159
}
176160

177161
/** Linear interpolation of two 3-dimensional vectors. */
178-
static lerp(vec1: VEC3, vec2, VEC3, t: number) {
162+
static lerp(v1: VEC3, v2, VEC3, t: number) {
179163
return new VEC3(
180-
(vec2.x - vec1.x) * t + vec1.x,
181-
(vec2.y - vec1.y) * t + vec1.y,
182-
(vec2.z - vec1.z) * t + vec1.z,
164+
(v2.x - v1.x) * t + v1.x,
165+
(v2.y - v1.y) * t + v1.y,
166+
(v2.z - v1.z) * t + v1.z,
183167
);
184168
}
169+
170+
/** Addition of two 3-dimensional vectors. */
171+
static add(out: VEC3, v1: VEC3, v2: VEC3) {
172+
out.x = v1.x + v2.x;
173+
out.y = v1.y + v2.y;
174+
out.z = v1.z + v2.z;
175+
}
176+
177+
/** Subtraction of two 3-dimensional vectors. */
178+
static sub(out: VEC3, v1: VEC3, v2: VEC3) {
179+
out.x = v1.x - v2.x;
180+
out.y = v1.y - v2.y;
181+
out.z = v1.z - v2.z;
182+
}
183+
184+
/** Multiplication of a 3-dimensional vector by a scalar. */
185+
static scale(out: VEC3, v1: VEC3, x: number) {
186+
out.x = v1.x * x;
187+
out.y = v1.y * x;
188+
out.z = v1.z * x;
189+
}
185190
}

0 commit comments

Comments
 (0)