Skip to content

Commit

Permalink
Vector: Consume MathUtils.clamp internally (#29812)
Browse files Browse the repository at this point in the history
* Vector2: consume MathUtils.clamp internally

* Vector3: consume MathUtils.clamp internally

* Vector4: consume MathUtils.clamp internally
  • Loading branch information
satelllte authored Nov 6, 2024
1 parent bb4eacd commit 99a0210
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/math/Vector2.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,17 @@ class Vector2 {

// assumes min < max, componentwise

this.x = Math.max( min.x, Math.min( max.x, this.x ) );
this.y = Math.max( min.y, Math.min( max.y, this.y ) );
this.x = MathUtils.clamp( this.x, min.x, max.x );
this.y = MathUtils.clamp( this.y, min.y, max.y );

return this;

}

clampScalar( minVal, maxVal ) {

this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
this.x = MathUtils.clamp( this.x, minVal, maxVal );
this.y = MathUtils.clamp( this.y, minVal, maxVal );

return this;

Expand All @@ -260,7 +260,7 @@ class Vector2 {

const length = this.length();

return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
return this.divideScalar( length || 1 ).multiplyScalar( MathUtils.clamp( length, min, max ) );

}

Expand Down
14 changes: 7 additions & 7 deletions src/math/Vector3.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,19 @@ class Vector3 {

// assumes min < max, componentwise

this.x = Math.max( min.x, Math.min( max.x, this.x ) );
this.y = Math.max( min.y, Math.min( max.y, this.y ) );
this.z = Math.max( min.z, Math.min( max.z, this.z ) );
this.x = MathUtils.clamp( this.x, min.x, max.x );
this.y = MathUtils.clamp( this.y, min.y, max.y );
this.z = MathUtils.clamp( this.z, min.z, max.z );

return this;

}

clampScalar( minVal, maxVal ) {

this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
this.z = Math.max( minVal, Math.min( maxVal, this.z ) );
this.x = MathUtils.clamp( this.x, minVal, maxVal );
this.y = MathUtils.clamp( this.y, minVal, maxVal );
this.z = MathUtils.clamp( this.z, minVal, maxVal );

return this;

Expand All @@ -360,7 +360,7 @@ class Vector3 {

const length = this.length();

return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
return this.divideScalar( length || 1 ).multiplyScalar( MathUtils.clamp( length, min, max ) );

}

Expand Down
20 changes: 11 additions & 9 deletions src/math/Vector4.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as MathUtils from './MathUtils.js';

class Vector4 {

constructor( x = 0, y = 0, z = 0, w = 1 ) {
Expand Down Expand Up @@ -463,21 +465,21 @@ class Vector4 {

// assumes min < max, componentwise

this.x = Math.max( min.x, Math.min( max.x, this.x ) );
this.y = Math.max( min.y, Math.min( max.y, this.y ) );
this.z = Math.max( min.z, Math.min( max.z, this.z ) );
this.w = Math.max( min.w, Math.min( max.w, this.w ) );
this.x = MathUtils.clamp( this.x, min.x, max.x );
this.y = MathUtils.clamp( this.y, min.y, max.y );
this.z = MathUtils.clamp( this.z, min.z, max.z );
this.w = MathUtils.clamp( this.w, min.w, max.w );

return this;

}

clampScalar( minVal, maxVal ) {

this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
this.z = Math.max( minVal, Math.min( maxVal, this.z ) );
this.w = Math.max( minVal, Math.min( maxVal, this.w ) );
this.x = MathUtils.clamp( this.x, minVal, maxVal );
this.y = MathUtils.clamp( this.y, minVal, maxVal );
this.z = MathUtils.clamp( this.z, minVal, maxVal );
this.w = MathUtils.clamp( this.w, minVal, maxVal );

return this;

Expand All @@ -487,7 +489,7 @@ class Vector4 {

const length = this.length();

return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
return this.divideScalar( length || 1 ).multiplyScalar( MathUtils.clamp( length, min, max ) );

}

Expand Down

0 comments on commit 99a0210

Please sign in to comment.