Skip to content

Commit

Permalink
Converting dot Utils usage, see #4
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Feb 14, 2025
1 parent 5c99f10 commit c263e75
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 226 deletions.
10 changes: 6 additions & 4 deletions js/CompletePiecewiseLinearFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
*/

import dot from './dot.js';
import Utils from './Utils.js';
import Vector2 from './Vector2.js';
import { arePointsCollinear } from './util/arePointsCollinear.js';
import { linear } from './util/linear.js';
import { lineLineIntersection } from './util/lineLineIntersection.js';

class CompletePiecewiseLinearFunction {

Expand All @@ -41,7 +43,7 @@ class CompletePiecewiseLinearFunction {
const b = this.points[ i + 1 ];
const c = this.points[ i + 2 ];

if ( Utils.arePointsCollinear( a, b, c ) ) {
if ( arePointsCollinear( a, b, c ) ) {
this.points.splice( i + 1, 1 );
i--;
}
Expand Down Expand Up @@ -81,7 +83,7 @@ class CompletePiecewiseLinearFunction {
return rightPoint.y;
}
else {
return Utils.linear( leftPoint.x, rightPoint.x, leftPoint.y, rightPoint.y, x );
return linear( leftPoint.x, rightPoint.x, leftPoint.y, rightPoint.y, x );
}
}
}
Expand All @@ -106,7 +108,7 @@ class CompletePiecewiseLinearFunction {
for ( let i = 0; i < xValues.length - 1; i++ ) {
const leftX = xValues[ i ];
const rightX = xValues[ i + 1 ];
const intersectionPoint = Utils.lineLineIntersection(
const intersectionPoint = lineLineIntersection(
// The linear function defined in this
new Vector2( leftX, this.evaluate( leftX ) ),
new Vector2( rightX, this.evaluate( rightX ) ),
Expand Down
19 changes: 10 additions & 9 deletions js/Complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/

import dot from './dot.js';
import Utils from './Utils.js';
import { cosh } from './util/cosh.js';
import { sinh } from './util/sinh.js';

export default class Complex {

Expand Down Expand Up @@ -186,8 +187,8 @@ export default class Complex {
*/
public sinOf(): Complex {
return new Complex(
Math.sin( this.real ) * Utils.cosh( this.imaginary ),
Math.cos( this.real ) * Utils.sinh( this.imaginary )
Math.sin( this.real ) * cosh( this.imaginary ),
Math.cos( this.real ) * sinh( this.imaginary )
);
}

Expand All @@ -198,8 +199,8 @@ export default class Complex {
*/
public cosOf(): Complex {
return new Complex(
Math.cos( this.real ) * Utils.cosh( this.imaginary ),
-Math.sin( this.real ) * Utils.sinh( this.imaginary )
Math.cos( this.real ) * cosh( this.imaginary ),
-Math.sin( this.real ) * sinh( this.imaginary )
);
}

Expand Down Expand Up @@ -362,8 +363,8 @@ export default class Complex {
*/
public sin(): Complex {
return this.setRealImaginary(
Math.sin( this.real ) * Utils.cosh( this.imaginary ),
Math.cos( this.real ) * Utils.sinh( this.imaginary )
Math.sin( this.real ) * cosh( this.imaginary ),
Math.cos( this.real ) * sinh( this.imaginary )
);
}

Expand All @@ -374,8 +375,8 @@ export default class Complex {
*/
public cos(): Complex {
return this.setRealImaginary(
Math.cos( this.real ) * Utils.cosh( this.imaginary ),
-Math.sin( this.real ) * Utils.sinh( this.imaginary )
Math.cos( this.real ) * cosh( this.imaginary ),
-Math.sin( this.real ) * sinh( this.imaginary )
);
}

Expand Down
22 changes: 12 additions & 10 deletions js/DelaunayTriangulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import arrayRemove from '../../phet-core/js/arrayRemove.js';
import merge from '../../phet-core/js/merge.js';
import Bounds2 from './Bounds2.js';
import dot from './dot.js';
import Utils from './Utils.js';
import { lineSegmentIntersection } from './util/lineSegmentIntersection.js';
import { pointInCircleFromPoints } from './util/pointInCircleFromPoints.js';
import { triangleAreaSigned } from './util/triangleAreaSigned.js';
import Vector2 from './Vector2.js';

class DelaunayTriangulation {
Expand Down Expand Up @@ -262,7 +264,7 @@ class DelaunayTriangulation {
const middleVertex = rightFrontEdge.endVertex;

while ( rightFrontEdge.previousEdge &&
Utils.triangleAreaSigned( middleVertex.point, rightFrontEdge.startVertex.point, rightFrontEdge.previousEdge.startVertex.point ) > 0 &&
triangleAreaSigned( middleVertex.point, rightFrontEdge.startVertex.point, rightFrontEdge.previousEdge.startVertex.point ) > 0 &&
( middleVertex.point.minus( rightFrontEdge.startVertex.point ) ).angleBetween( rightFrontEdge.previousEdge.startVertex.point.minus( rightFrontEdge.startVertex.point ) ) < Math.PI / 2 ) {
const previousEdge = rightFrontEdge.previousEdge;
const newRightEdge = new Edge( previousEdge.startVertex, middleVertex );
Expand All @@ -277,7 +279,7 @@ class DelaunayTriangulation {
rightFrontEdge = newRightEdge;
}
while ( leftFrontEdge.nextEdge &&
Utils.triangleAreaSigned( middleVertex.point, leftFrontEdge.nextEdge.endVertex.point, leftFrontEdge.endVertex.point ) > 0 &&
triangleAreaSigned( middleVertex.point, leftFrontEdge.nextEdge.endVertex.point, leftFrontEdge.endVertex.point ) > 0 &&
( middleVertex.point.minus( leftFrontEdge.endVertex.point ) ).angleBetween( leftFrontEdge.nextEdge.endVertex.point.minus( leftFrontEdge.endVertex.point ) ) < Math.PI / 2 ) {
const nextEdge = leftFrontEdge.nextEdge;
const newLeftEdge = new Edge( middleVertex, nextEdge.endVertex );
Expand Down Expand Up @@ -465,7 +467,7 @@ class DelaunayTriangulation {
const startVertex = edge.getOtherVertex( sharedVertex );
const endVertex = nextEdge.getOtherVertex( sharedVertex );

if ( Utils.triangleAreaSigned( startVertex.point, sharedVertex.point, endVertex.point ) <= 0 ) {
if ( triangleAreaSigned( startVertex.point, sharedVertex.point, endVertex.point ) <= 0 ) {
continue;
}

Expand Down Expand Up @@ -554,7 +556,7 @@ class DelaunayTriangulation {
for ( let i = 0; i < frontEdges.length - 1; i++ ) {
const firstEdge = frontEdges[ i ];
const secondEdge = frontEdges[ i + 1 ];
if ( Utils.triangleAreaSigned( secondEdge.endVertex.point, firstEdge.endVertex.point, firstEdge.startVertex.point ) > 1e-10 ) {
if ( triangleAreaSigned( secondEdge.endVertex.point, firstEdge.endVertex.point, firstEdge.startVertex.point ) > 1e-10 ) {
const newEdge = this.fillBorderTriangle( firstEdge, secondEdge, firstEdge.startVertex, firstEdge.endVertex, secondEdge.endVertex );
frontEdges.splice( i, 2, newEdge );
// start scanning from behind where we were previously (if possible)
Expand Down Expand Up @@ -610,7 +612,7 @@ class DelaunayTriangulation {
const sharedVertex = firstEdge.getSharedVertex( secondEdge );
const firstVertex = firstEdge.getOtherVertex( sharedVertex );
const secondVertex = secondEdge.getOtherVertex( sharedVertex );
if ( Utils.triangleAreaSigned( secondVertex.point, sharedVertex.point, firstVertex.point ) > 1e-10 ) {
if ( triangleAreaSigned( secondVertex.point, sharedVertex.point, firstVertex.point ) > 1e-10 ) {
const newEdge = this.fillBorderTriangle( firstEdge, secondEdge, firstVertex, sharedVertex, secondVertex );
backEdges.splice( i, 2, newEdge );
// start scanning from behind where we were previously (if possible)
Expand Down Expand Up @@ -650,8 +652,8 @@ class DelaunayTriangulation {
const farVertex1 = triangle1.getVertexOppositeFromEdge( edge );
const farVertex2 = triangle2.getVertexOppositeFromEdge( edge );

if ( Utils.pointInCircleFromPoints( triangle1.aVertex.point, triangle1.bVertex.point, triangle1.cVertex.point, farVertex2.point ) ||
Utils.pointInCircleFromPoints( triangle2.aVertex.point, triangle2.bVertex.point, triangle2.cVertex.point, farVertex1.point ) ) {
if ( pointInCircleFromPoints( triangle1.aVertex.point, triangle1.bVertex.point, triangle1.cVertex.point, farVertex2.point ) ||
pointInCircleFromPoints( triangle2.aVertex.point, triangle2.bVertex.point, triangle2.cVertex.point, farVertex1.point ) ) {
// TODO: better helper functions for adding/removing triangles (takes care of the edge stuff) https://github.com/phetsims/dot/issues/96
triangle1.remove();
triangle2.remove();
Expand Down Expand Up @@ -945,7 +947,7 @@ class Edge {
* @returns {boolean}
*/
intersectsConstrainedEdge( vertex, bottomVertex ) {
return Utils.lineSegmentIntersection( vertex.point.x, vertex.point.y, bottomVertex.point.x, bottomVertex.point.y,
return lineSegmentIntersection( vertex.point.x, vertex.point.y, bottomVertex.point.x, bottomVertex.point.y,
this.startVertex.point.x, this.startVertex.point.y,
this.endVertex.point.x, this.endVertex.point.y );
}
Expand Down Expand Up @@ -985,7 +987,7 @@ class Triangle {
assert && assert( cVertex === aEdge.startVertex || cVertex === aEdge.endVertex, 'cVertex should be in aEdge' );
assert && assert( cVertex === bEdge.startVertex || cVertex === bEdge.endVertex, 'cVertex should be in bEdge' );

assert && assert( Utils.triangleAreaSigned( aVertex.point, bVertex.point, cVertex.point ) > 0,
assert && assert( triangleAreaSigned( aVertex.point, bVertex.point, cVertex.point ) > 0,
'Should be counterclockwise' );

// @public {Vertex}
Expand Down
4 changes: 2 additions & 2 deletions js/LinearFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/

import dot from './dot.js';
import Utils from './Utils.js';
import { clamp } from './util/clamp.js';
import { linear } from './util/linear.js';

export default class LinearFunction {
private a1: number;
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class LinearFunction {
* Optionally clamp the result to the range [b1,b2].
*/
const map = ( a1: number, a2: number, b1: number, b2: number, a3: number, clampValue: boolean ): number => {
let b3 = Utils.linear( a1, a2, b1, b2, a3 );
let b3 = linear( a1, a2, b1, b2, a3 );
if ( clampValue ) {
const max = Math.max( b1, b2 );
const min = Math.min( b1, b2 );
Expand Down
4 changes: 2 additions & 2 deletions js/Permutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import dot from './dot.js';
import Utils from './Utils.js';
import { rangeInclusive } from './util/rangeInclusive.js';

class Permutation {

Expand Down Expand Up @@ -97,7 +97,7 @@ class Permutation {
*/
public static permutations( size: number ): Permutation[] {
const result: Permutation[] = [];
Permutation.forEachPermutation( Utils.rangeInclusive( 0, size - 1 ), integers => {
Permutation.forEachPermutation( rangeInclusive( 0, size - 1 ), integers => {
result.push( new Permutation( integers.slice() ) );
} );
return result;
Expand Down
4 changes: 2 additions & 2 deletions js/PiecewiseLinearFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import dot from './dot.js';
import Utils from './Utils.js';
import { linear } from './util/linear.js';

class PiecewiseLinearFunction {

Expand Down Expand Up @@ -64,7 +64,7 @@ class PiecewiseLinearFunction {
const anchor1Y = array[ lowerIndex + 1 ];
const anchor2X = array[ upperIndex ];
const anchor2Y = array[ upperIndex + 1 ];
return Utils.linear( anchor1X, anchor2X, anchor1Y, anchor2Y, x );
return linear( anchor1X, anchor2X, anchor1Y, anchor2Y, x );
}
}

Expand Down
1 change: 0 additions & 1 deletion js/Quaternion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import Poolable from '../../phet-core/js/Poolable.js';
import dot from './dot.js';
import Matrix3 from './Matrix3.js';
import './Utils.js';
import Vector3 from './Vector3.js';

class Quaternion {
Expand Down
Loading

0 comments on commit c263e75

Please sign in to comment.