2
2
* A collection of geomtry-related utility functions
3
3
*/
4
4
5
- import { number as knumber , point as kpoint , sum } from "@khanacademy/kmath" ;
5
+ import {
6
+ approximateDeepEqual ,
7
+ approximateEqual ,
8
+ type Coord ,
9
+ } from "@khanacademy/perseus-core" ;
6
10
import _ from "underscore" ;
7
11
8
- import Util from "../util" ;
9
-
10
- import type { Coord , Line } from "../interactive2/types" ;
12
+ import { number as knumber , point as kpoint , sum } from "@khanacademy/kmath" ;
11
13
12
- const { eq , deepEq } = Util ;
14
+ type Line = [ Coord , Coord ] ;
13
15
14
16
// This should really be a readonly tuple of [number, number]
15
17
export type Range = [ number , number ] ;
@@ -21,12 +23,9 @@ export type SineCoefficient = [
21
23
number , // verticalOffset
22
24
] ;
23
25
24
- // a, b, c
25
- export type QuadraticCoefficient = [ number , number , number ] ;
26
-
27
26
// Given a number, return whether it is positive (1), negative (-1), or zero (0)
28
27
export function sign ( val : number ) : 0 | 1 | - 1 {
29
- if ( eq ( val , 0 ) ) {
28
+ if ( approximateEqual ( val , 0 ) ) {
30
29
return 0 ;
31
30
}
32
31
return val > 0 ? 1 : - 1 ;
@@ -39,7 +38,7 @@ export function ccw(a: Coord, b: Coord, c: Coord): number {
39
38
}
40
39
41
40
export function collinear ( a : Coord , b : Coord , c : Coord ) : boolean {
42
- return eq ( ccw ( a , b , c ) , 0 ) ;
41
+ return approximateEqual ( ccw ( a , b , c ) , 0 ) ;
43
42
}
44
43
45
44
// Given rect bounding points A and B, whether point C is inside the rect
@@ -229,15 +228,15 @@ export function similar(
229
228
// @ts -expect-error - TS4104 - The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
230
229
sides = rotate ( sides , i ) ;
231
230
232
- if ( deepEq ( angles1 , angles ) ) {
231
+ if ( approximateDeepEqual ( angles1 , angles ) ) {
233
232
const sidePairs = _ . zip ( sides1 , sides ) ;
234
233
235
234
const factors = _ . map ( sidePairs , function ( pair ) {
236
235
return pair [ 0 ] / pair [ 1 ] ;
237
236
} ) ;
238
237
239
238
const same = _ . all ( factors , function ( factor ) {
240
- return eq ( factors [ 0 ] , factor ) ;
239
+ return approximateEqual ( factors [ 0 ] , factor ) ;
241
240
} ) ;
242
241
243
242
const congruentEnough = _ . all ( sidePairs , function ( pair ) {
@@ -304,7 +303,7 @@ export function rotate<T>(
304
303
}
305
304
306
305
export function getLineEquation ( first : Coord , second : Coord ) : string {
307
- if ( eq ( first [ 0 ] , second [ 0 ] ) ) {
306
+ if ( approximateEqual ( first [ 0 ] , second [ 0 ] ) ) {
308
307
return "x = " + first [ 0 ] . toFixed ( 3 ) ;
309
308
}
310
309
const m = ( second [ 1 ] - first [ 1 ] ) / ( second [ 0 ] - first [ 0 ] ) ;
0 commit comments