Skip to content

Commit c4d3bbe

Browse files
authored
Merge pull request #21091 from marcofugaro/pingpong
Add MathUtils.pingpong()
2 parents edb93ed + 38fd114 commit c4d3bbe

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

docs/api/en/math/MathUtils.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ <h3>[method:Float mapLinear]( [param:Float x], [param:Float a1], [param:Float a2
6464
Linear mapping of [page:Float x] from range [[page:Float a1], [page:Float a2]] to range [[page:Float b1], [page:Float b2]].
6565
</p>
6666

67+
<h3>[method:Float pingpong]( [param:Float x], [param:Float length] )</h3>
68+
<p>
69+
[page:Float x] — The value to pingpong.<br />
70+
[page:Float length] — The positive value the function will pingpong to. Default is 1.<br /><br />
71+
72+
Returns a value that alternates between 0 and [param:Float length].</p>
73+
6774
<h3>[method:Integer ceilPowerOfTwo]( [param:Number n] )</h3>
6875
<p>Returns the smallest power of 2 that is greater than or equal to [page:Number n].</p>
6976

docs/api/zh/math/MathUtils.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ <h3>[method:Float mapLinear]( [param:Float x], [param:Float a1], [param:Float a2
6161
x从范围[[page:Float a1], [page:Float a2]] 到范围[[page:Float b1], [page:Float b2]]的线性映射。
6262
</p>
6363

64+
<h3>[method:Float pingpong]( [param:Float x], [param:Float length] )</h3>
65+
<p>
66+
[page:Float x] — The value to pingpong.<br />
67+
[page:Float length] — The positive value the function will pingpong to. Default is 1.<br /><br />
68+
69+
Returns a value that alternates between 0 and [param:Float length].</p>
70+
6471
<h3>[method:Integer ceilPowerOfTwo]( [param:Number n] )</h3>
6572
<p>返回大于等于 [page:Number n] 的2的最小次幂。</p>
6673

src/math/MathUtils.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ export namespace MathUtils {
8585
*/
8686
export function lerp( x: number, y: number, t: number ): number;
8787

88+
/**
89+
* Returns a value that alternates between 0 and length.
90+
*
91+
* @param x The value to pingpong.
92+
* @param length The positive value the function will pingpong to. Default is 1.
93+
* @return {number}
94+
*/
95+
export function pingpong( x: number, length?: number ): number;
96+
8897
/**
8998
* @deprecated Use {@link Math#floorPowerOfTwo .floorPowerOfTwo()}
9099
*/

src/math/MathUtils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ const MathUtils = {
6262

6363
},
6464

65+
// https://www.desmos.com/calculator/vcsjnyz7x4
66+
67+
pingpong: function ( x, length = 1 ) {
68+
69+
return length - Math.abs( MathUtils.euclideanModulo( x, length * 2 ) - length );
70+
71+
},
72+
6573
// http://en.wikipedia.org/wiki/Smoothstep
6674

6775
smoothstep: function ( x, min, max ) {

test/unit/src/math/MathUtils.tests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ export default QUnit.module( 'Maths', () => {
152152

153153
} );
154154

155+
156+
QUnit.test( "pingpong", ( assert ) => {
157+
158+
assert.strictEqual( MathUtils.pingpong( 2.5 ), 0.5, "Value at 2.5 is 0.5" );
159+
assert.strictEqual( MathUtils.pingpong( 2.5, 2 ), 1.5, "Value at 2.5 with length of 2 is 1.5" );
160+
assert.strictEqual( MathUtils.pingpong( - 1.5 ), 0.5, "Value at -1.5 is 0.5" );
161+
162+
} );
163+
155164
} );
156165

157166
} );

0 commit comments

Comments
 (0)