Skip to content

Commit b27462e

Browse files
authored
Merge pull request #14567 from yaoyao-cn/dev
add Vector2.cross method
2 parents 62aa6f6 + a821d16 commit b27462e

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

docs/api/math/Vector2.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ <h3>[method:Float dot]( [param:Vector2 v] )</h3>
176176
Calculates the [link:https://en.wikipedia.org/wiki/Dot_product dot product] of this
177177
vector and [page:Vector2 v].
178178
</p>
179+
180+
<h3>[method:Float cross]( [param:Vector2 v] )</h3>
181+
<p>
182+
Calculates the [link:https://en.wikipedia.org/wiki/Cross_product cross product] of this
183+
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
184+
</p>
179185

180186
<h3>[method:Boolean equals]( [param:Vector2 v] )</h3>
181187
<p>Checks for strict equality of this vector and [page:Vector2 v].</p>

src/math/Vector2.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@ Object.assign( Vector2.prototype, {
353353

354354
},
355355

356+
cross: function ( v ) {
357+
358+
return this.x * v.y - this.y * v.x;
359+
360+
},
361+
356362
lengthSq: function () {
357363

358364
return this.x * this.x + this.y * this.y;

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { Matrix3 } from '../../../../src/math/Matrix3';
99
import { BufferAttribute } from '../../../../src/core/BufferAttribute';
1010
import {
1111
x,
12-
y
12+
y,
13+
eps
1314
} from './Constants.tests';
1415

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

307308
} );
308309

310+
QUnit.test( "cross", ( assert ) => {
311+
312+
var a = new Vector2( x, y );
313+
var b = new Vector2( 2 * x, - y );
314+
var answer = - 18;
315+
var crossed = a.cross( b );
316+
317+
assert.ok( Math.abs( answer - crossed ) <= eps, "Check cross" );
318+
319+
} );
320+
309321
QUnit.todo( "lengthSq", ( assert ) => {
310322

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

338350
var a = new Vector2( x, 0 );
339351
var b = new Vector2( 0, - y );
340-
var c = new Vector2();
341352

342353
a.normalize();
343354
assert.ok( a.length() == 1, "Passed!" );

0 commit comments

Comments
 (0)