Skip to content

Commit 9776d27

Browse files
committed
Vector*: Simplified clamp(). See mrdoob#7326.
1 parent 24471ae commit 9776d27

File tree

3 files changed

+9
-87
lines changed

3 files changed

+9
-87
lines changed

src/math/Vector2.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,25 +252,8 @@ THREE.Vector2.prototype = {
252252

253253
// This function assumes min < max, if this assumption isn't true it will not operate correctly
254254

255-
if ( this.x < min.x ) {
256-
257-
this.x = min.x;
258-
259-
} else if ( this.x > max.x ) {
260-
261-
this.x = max.x;
262-
263-
}
264-
265-
if ( this.y < min.y ) {
266-
267-
this.y = min.y;
268-
269-
} else if ( this.y > max.y ) {
270-
271-
this.y = max.y;
272-
273-
}
255+
this.x = Math.max( min.x, Math.min( max.x, this.x ) );
256+
this.y = Math.max( min.y, Math.min( max.y, this.y ) );
274257

275258
return this;
276259

src/math/Vector3.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -464,35 +464,9 @@ THREE.Vector3.prototype = {
464464

465465
// This function assumes min < max, if this assumption isn't true it will not operate correctly
466466

467-
if ( this.x < min.x ) {
468-
469-
this.x = min.x;
470-
471-
} else if ( this.x > max.x ) {
472-
473-
this.x = max.x;
474-
475-
}
476-
477-
if ( this.y < min.y ) {
478-
479-
this.y = min.y;
480-
481-
} else if ( this.y > max.y ) {
482-
483-
this.y = max.y;
484-
485-
}
486-
487-
if ( this.z < min.z ) {
488-
489-
this.z = min.z;
490-
491-
} else if ( this.z > max.z ) {
492-
493-
this.z = max.z;
494-
495-
}
467+
this.x = Math.max( min.x, Math.min( max.x, this.x ) );
468+
this.y = Math.max( min.y, Math.min( max.y, this.y ) );
469+
this.z = Math.max( min.z, Math.min( max.z, this.z ) );
496470

497471
return this;
498472

src/math/Vector4.js

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -473,45 +473,10 @@ THREE.Vector4.prototype = {
473473

474474
// This function assumes min < max, if this assumption isn't true it will not operate correctly
475475

476-
if ( this.x < min.x ) {
477-
478-
this.x = min.x;
479-
480-
} else if ( this.x > max.x ) {
481-
482-
this.x = max.x;
483-
484-
}
485-
486-
if ( this.y < min.y ) {
487-
488-
this.y = min.y;
489-
490-
} else if ( this.y > max.y ) {
491-
492-
this.y = max.y;
493-
494-
}
495-
496-
if ( this.z < min.z ) {
497-
498-
this.z = min.z;
499-
500-
} else if ( this.z > max.z ) {
501-
502-
this.z = max.z;
503-
504-
}
505-
506-
if ( this.w < min.w ) {
507-
508-
this.w = min.w;
509-
510-
} else if ( this.w > max.w ) {
511-
512-
this.w = max.w;
513-
514-
}
476+
this.x = Math.max( min.x, Math.min( max.x, this.x ) );
477+
this.y = Math.max( min.y, Math.min( max.y, this.y ) );
478+
this.z = Math.max( min.z, Math.min( max.z, this.z ) );
479+
this.w = Math.max( min.w, Math.min( max.w, this.w ) );
515480

516481
return this;
517482

0 commit comments

Comments
 (0)