Skip to content

Commit

Permalink
move distanceXY and factorial to dot/js/util/, phetsims/models-of-the…
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Feb 13, 2025
1 parent c96c8b2 commit d1f7ba7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions js/util/distanceXY.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2025, University of Colorado Boulder

/**
* Returns the distance between 2 points, given by (x,y) coordinates.
*
* @author Chris Malley ([email protected])
*/
export default function distanceXY( x1: number, y1: number, x2: number, y2: number ): number {
const dx = x1 - x2;
const dy = y1 - y2;
return Math.sqrt( dx * dx + dy * dy );
}
16 changes: 16 additions & 0 deletions js/util/factorial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2025, University of Colorado Boulder

/**
* Computes the factorial of a non-negative integer n without using recursion.
* n! = 1 * 2 * ... * ( n - 1 ) * n
*
* @author Chris Malley ([email protected])
*/
export default function factorial( n: number ): number {
assert && assert( Number.isInteger( n ) && n >= 0, `n must be a non-negative integer: ${n}` );
let f = 1;
for ( let i = 2; i <= n; i++ ) {
f *= i;
}
return f;
}

0 comments on commit d1f7ba7

Please sign in to comment.