Skip to content
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
6 changes: 6 additions & 0 deletions docs/api/math/Vector2.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ <h3>[method:Float dot]( [param:Vector2 v] )</h3>
Calculates the [link:https://en.wikipedia.org/wiki/Dot_product dot product] of this
vector and [page:Vector2 v].
</p>

<h3>[method:Float cross]( [param:Vector2 v] )</h3>
<p>
Calculates the [link:https://en.wikipedia.org/wiki/Cross_product cross product] of this
vector and [page:Vector2 v]. Note that a 'cross-product' in 2D is not well-defined. This function computes a geometric cross-product often used in 2D graphics
</p>

<h3>[method:Boolean equals]( [param:Vector2 v] )</h3>
<p>Checks for strict equality of this vector and [page:Vector2 v].</p>
Expand Down
6 changes: 6 additions & 0 deletions src/math/Vector2.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ Object.assign( Vector2.prototype, {

},

cross: function ( v ) {

return this.x * v.y - this.y * v.x;

},

lengthSq: function () {

return this.x * this.x + this.y * this.y;
Expand Down
15 changes: 13 additions & 2 deletions test/unit/src/math/Vector2.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { Matrix3 } from '../../../../src/math/Matrix3';
import { BufferAttribute } from '../../../../src/core/BufferAttribute';
import {
x,
y
y,
eps
} from './Constants.tests';

export default QUnit.module( 'Maths', () => {
Expand Down Expand Up @@ -306,6 +307,17 @@ export default QUnit.module( 'Maths', () => {

} );

QUnit.test( "cross", ( assert ) => {

var a = new Vector2( x, y );
var b = new Vector2( 2 * x, - y );
var answer = - 18;
var crossed = a.cross( b );

assert.ok( Math.abs( answer - crossed ) <= eps, "Check cross" );

} );

QUnit.todo( "lengthSq", ( assert ) => {

assert.ok( false, "everything's gonna be alright" );
Expand Down Expand Up @@ -337,7 +349,6 @@ export default QUnit.module( 'Maths', () => {

var a = new Vector2( x, 0 );
var b = new Vector2( 0, - y );
var c = new Vector2();

a.normalize();
assert.ok( a.length() == 1, "Passed!" );
Expand Down