Skip to content

Commit 8891ba7

Browse files
LeXXikwilleastcott
andauthored
New API: add a scaled vector (#5702)
* vec3 * vec2 * simplify vec3 example * vec4 * Update src/core/math/vec4.js Co-authored-by: Will Eastcott <[email protected]> * Update src/core/math/vec3.js Co-authored-by: Will Eastcott <[email protected]> * Update src/core/math/vec2.js Co-authored-by: Will Eastcott <[email protected]> * number type * * to number --------- Co-authored-by: Will Eastcott <[email protected]>
1 parent f993afb commit 8891ba7

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

src/core/math/vec2.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ class Vec2 {
103103
return this;
104104
}
105105

106+
/**
107+
* Adds a 2-dimensional vector scaled by scalar value. Does not modify the vector being added.
108+
*
109+
* @param {Vec2} rhs - The vector to add to the specified vector.
110+
* @param {number} scalar - The number to multiply the added vector with.
111+
* @returns {Vec2} Self for chaining.
112+
* @example
113+
* const vec = new pc.Vec2(1, 2);
114+
*
115+
* vec.addScaled(pc.Vec2.UP, 2);
116+
*
117+
* // Outputs [1, 4]
118+
* console.log("The result of the addition is: " + vec.toString());
119+
*/
120+
addScaled(rhs, scalar) {
121+
this.x += rhs.x * scalar;
122+
this.y += rhs.y * scalar;
123+
124+
return this;
125+
}
126+
106127
/**
107128
* Returns an identical copy of the specified 2-dimensional vector.
108129
*

src/core/math/vec3.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@ class Vec3 {
114114
return this;
115115
}
116116

117+
/**
118+
* Adds a 3-dimensional vector scaled by scalar value. Does not modify the vector being added.
119+
*
120+
* @param {Vec3} rhs - The vector to add to the specified vector.
121+
* @param {number} scalar - The number to multiply the added vector with.
122+
* @returns {Vec3} Self for chaining.
123+
* @example
124+
* const vec = new pc.Vec3(1, 2, 3);
125+
*
126+
* vec.addScaled(pc.Vec3.UP, 2);
127+
*
128+
* // Outputs [1, 4, 3]
129+
* console.log("The result of the addition is: " + vec.toString());
130+
*/
131+
addScaled(rhs, scalar) {
132+
this.x += rhs.x * scalar;
133+
this.y += rhs.y * scalar;
134+
this.z += rhs.z * scalar;
135+
136+
return this;
137+
}
138+
117139
/**
118140
* Returns an identical copy of the specified 3-dimensional vector.
119141
*

src/core/math/vec4.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,29 @@ class Vec4 {
127127
return this;
128128
}
129129

130+
/**
131+
* Adds a 4-dimensional vector scaled by scalar value. Does not modify the vector being added.
132+
*
133+
* @param {Vec4} rhs - The vector to add to the specified vector.
134+
* @param {number} scalar - The number to multiply the added vector with.
135+
* @returns {Vec4} Self for chaining.
136+
* @example
137+
* const vec = new pc.Vec4(1, 2, 3, 4);
138+
*
139+
* vec.addScaled(pc.Vec4.ONE, 2);
140+
*
141+
* // Outputs [3, 4, 5, 6]
142+
* console.log("The result of the addition is: " + vec.toString());
143+
*/
144+
addScaled(rhs, scalar) {
145+
this.x += rhs.x * scalar;
146+
this.y += rhs.y * scalar;
147+
this.z += rhs.z * scalar;
148+
this.w += rhs.w * scalar;
149+
150+
return this;
151+
}
152+
130153
/**
131154
* Returns an identical copy of the specified 4-dimensional vector.
132155
*

test/core/math/vec2.test.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ describe('Vec2', function () {
7676

7777
});
7878

79+
describe('#addScaled', function () {
80+
81+
it('adds a scaled vector', function () {
82+
const v = new Vec2(1, 2);
83+
v.addScaled(Vec2.UP, 2);
84+
expect(v.x).to.equal(1);
85+
expect(v.y).to.equal(4);
86+
});
87+
88+
});
89+
7990
describe('#ceil', function () {
8091

8192
it('leaves integers unchanged', function () {

test/core/math/vec3.test.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ describe('Vec3', function () {
8484

8585
});
8686

87+
describe('#addScaled', function () {
88+
89+
it('adds a scaled vector', function () {
90+
const v = new Vec3(1, 2, 3);
91+
v.addScaled(Vec3.UP, 2);
92+
expect(v.x).to.equal(1);
93+
expect(v.y).to.equal(4);
94+
expect(v.z).to.equal(3);
95+
});
96+
97+
});
98+
8799
describe('#ceil', function () {
88100

89101
it('leaves integers unchanged', function () {

test/core/math/vec4.test.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ describe('Vec4', function () {
9292

9393
});
9494

95+
describe('#addScaled', function () {
96+
97+
it('adds a scaled vector', function () {
98+
const v = new Vec4(1, 2, 3, 4);
99+
v.addScaled(Vec4.ONE, 2);
100+
expect(v.x).to.equal(3);
101+
expect(v.y).to.equal(4);
102+
expect(v.z).to.equal(5);
103+
expect(v.w).to.equal(6);
104+
});
105+
106+
});
107+
95108
describe('#ceil', function () {
96109

97110
it('leaves integers unchanged', function () {

0 commit comments

Comments
 (0)