Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math area and space: move to es6 classes #20076

Merged
merged 9 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 49 additions & 49 deletions src/math/Box2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import { Vector2 } from './Vector2.js';

const _vector = new Vector2();

function Box2( min, max ) {
class Box2 {

this.min = ( min !== undefined ) ? min : new Vector2( + Infinity, + Infinity );
this.max = ( max !== undefined ) ? max : new Vector2( - Infinity, - Infinity );
constructor( min, max ) {

}
this.min = ( min !== undefined ) ? min : new Vector2( + Infinity, + Infinity );
this.max = ( max !== undefined ) ? max : new Vector2( - Infinity, - Infinity );

Object.assign( Box2.prototype, {
}

set: function ( min, max ) {
set( min, max ) {

this.min.copy( min );
this.max.copy( max );

return this;

},
}

setFromPoints: function ( points ) {
setFromPoints( points ) {

this.makeEmpty();

Expand All @@ -32,51 +32,51 @@ Object.assign( Box2.prototype, {

return this;

},
}

setFromCenterAndSize: function ( center, size ) {
setFromCenterAndSize( center, size ) {

const halfSize = _vector.copy( size ).multiplyScalar( 0.5 );
this.min.copy( center ).sub( halfSize );
this.max.copy( center ).add( halfSize );

return this;

},
}

clone: function () {
clone() {

return new this.constructor().copy( this );

},
}

copy: function ( box ) {
copy( box ) {

this.min.copy( box.min );
this.max.copy( box.max );

return this;

},
}

makeEmpty: function () {
makeEmpty() {

this.min.x = this.min.y = + Infinity;
this.max.x = this.max.y = - Infinity;

return this;

},
}

isEmpty: function () {
isEmpty() {

// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes

return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );

},
}

getCenter: function ( target ) {
getCenter( target ) {

if ( target === undefined ) {

Expand All @@ -87,9 +87,9 @@ Object.assign( Box2.prototype, {

return this.isEmpty() ? target.set( 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );

},
}

getSize: function ( target ) {
getSize( target ) {

if ( target === undefined ) {

Expand All @@ -100,50 +100,50 @@ Object.assign( Box2.prototype, {

return this.isEmpty() ? target.set( 0, 0 ) : target.subVectors( this.max, this.min );

},
}

expandByPoint: function ( point ) {
expandByPoint( point ) {

this.min.min( point );
this.max.max( point );

return this;

},
}

expandByVector: function ( vector ) {
expandByVector( vector ) {

this.min.sub( vector );
this.max.add( vector );

return this;

},
}

expandByScalar: function ( scalar ) {
expandByScalar( scalar ) {

this.min.addScalar( - scalar );
this.max.addScalar( scalar );

return this;

},
}

containsPoint: function ( point ) {
containsPoint( point ) {

return point.x < this.min.x || point.x > this.max.x ||
point.y < this.min.y || point.y > this.max.y ? false : true;

},
}

containsBox: function ( box ) {
containsBox( box ) {

return this.min.x <= box.min.x && box.max.x <= this.max.x &&
this.min.y <= box.min.y && box.max.y <= this.max.y;

},
}

getParameter: function ( point, target ) {
getParameter( point, target ) {

// This can potentially have a divide by zero if the box
// has a size dimension of 0.
Expand All @@ -160,18 +160,18 @@ Object.assign( Box2.prototype, {
( point.y - this.min.y ) / ( this.max.y - this.min.y )
);

},
}

intersectsBox: function ( box ) {
intersectsBox( box ) {

// using 4 splitting planes to rule out intersections

return box.max.x < this.min.x || box.min.x > this.max.x ||
box.max.y < this.min.y || box.min.y > this.max.y ? false : true;

},
}

clampPoint: function ( point, target ) {
clampPoint( point, target ) {

if ( target === undefined ) {

Expand All @@ -182,49 +182,49 @@ Object.assign( Box2.prototype, {

return target.copy( point ).clamp( this.min, this.max );

},
}

distanceToPoint: function ( point ) {
distanceToPoint( point ) {

const clampedPoint = _vector.copy( point ).clamp( this.min, this.max );
return clampedPoint.sub( point ).length();

},
}

intersect: function ( box ) {
intersect( box ) {

this.min.max( box.min );
this.max.min( box.max );

return this;

},
}

union: function ( box ) {
union( box ) {

this.min.min( box.min );
this.max.max( box.max );

return this;

},
}

translate: function ( offset ) {
translate( offset ) {

this.min.add( offset );
this.max.add( offset );

return this;

},
}

equals: function ( box ) {
equals( box ) {

return box.min.equals( this.min ) && box.max.equals( this.max );

}

} );
}


export { Box2 };
Loading