-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move functions from protractor to
math/
(#1394)
These math functions weren't tested, and had some lurking bugs which I've now fixed. Issue: LEMS-2135 ## Test plan: `yarn test` Author: benchristel Reviewers: jeremywiebe, mark-fitzgerald, nishasy Required Reviewers: Approved By: jeremywiebe Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald Pull Request URL: #1394
- Loading branch information
1 parent
caf7148
commit 8ae3d18
Showing
10 changed files
with
78 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus": patch | ||
--- | ||
|
||
Internal: move angle functions to math/angle.ts |
2 changes: 1 addition & 1 deletion
2
packages/perseus/src/widgets/interactive-graphs/graphs/components/vector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
packages/perseus/src/widgets/interactive-graphs/graphs/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/perseus/src/widgets/interactive-graphs/math/angles.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {calculateAngleInDegrees} from "./angles"; | ||
|
||
describe("calculateAngleInDegrees", () => { | ||
it.each` | ||
x | y | expected | note | ||
${0} | ${0} | ${0} | ${": zero vector has an angle of zero"} | ||
${1} | ${0} | ${0} | ${""} | ||
${1} | ${-0} | ${-0} | ${": a y-coord of -0 produces -0deg"} | ||
${0} | ${1} | ${90} | ${""} | ||
${-1} | ${0} | ${180} | ${""} | ||
${-1} | ${-0} | ${-180} | ${": a y-coord of -0 produces -180deg"} | ||
${0} | ${-1} | ${-90} | ${""} | ||
`("returns $expected degrees given [$x, $y]$note", (params) => { | ||
const {expected, x, y} = params; | ||
expect(calculateAngleInDegrees([x, y])).toBe(expected); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/perseus/src/widgets/interactive-graphs/math/angles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/perseus/src/widgets/interactive-graphs/math/interpolation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {lerp} from "./interpolation"; | ||
|
||
describe("lerp", () => { | ||
it.each` | ||
a | b | fraction | expected | note | ||
${3} | ${7} | ${0} | ${3} | ${""} | ||
${3} | ${7} | ${1} | ${7} | ${""} | ||
${3} | ${7} | ${0.5} | ${5} | ${": midpoint of 3 and 7"} | ||
${0} | ${4} | ${0.25} | ${1} | ${": 25% of the way between 0 and 4"} | ||
${4} | ${0} | ${0.25} | ${3} | ${""} | ||
${0} | ${4} | ${2} | ${4} | ${": fraction > 1 is treated as 1"} | ||
${0} | ${4} | ${-1} | ${0} | ${": fraction < 0 is treated as 0"} | ||
`("given $a, $b, $fraction, returns $expected$note", (params) => { | ||
const {a, b, fraction, expected} = params; | ||
expect(lerp(a, b, fraction)).toBe(expected); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/perseus/src/widgets/interactive-graphs/math/interpolation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {clamp} from "./clamp"; | ||
|
||
// [L]inear Int[erp]olation: gets the weighted average of two values `a` and | ||
// `b`, with the given `fraction` specifying how much weight `a` and `b` get: | ||
// - if `fraction` is 0, `lerp` returns `a`. | ||
// - if `fraction` is 0.5, `lerp` returns the average of `a` and `b`. | ||
// - if `fraction` is 1, `lerp` returns `b`. | ||
export function lerp(a: number, b: number, fraction: number): number { | ||
return (b - a) * clamp(fraction, 0, 1) + a; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters