-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a bug where line-ending arrows would flip around or vanish (#1263)
The bug happened when `-0` was passed as a coordinate (yes, `-0` is a distinct value in floating point, because the sign bit is stored separately from the mantissa). This PR patches the bug by handling horizontal and vertical lines as a special case, and then refactors to a design where the special-casing is no longer necessary. It's probably easiest to read the PR one commit at a time. Issue: LEMS-1955 ## Test plan: - `yarn dev` - Play around with the linear graphs at `localhost:5173` Author: benchristel Reviewers: jeremywiebe, benchristel, handeyeco, Myranae Required Reviewers: Approved By: jeremywiebe Checks: ✅ codecov/project, ✅ codecov/patch, ✅ Upload Coverage, ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Jest Coverage (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (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: #1263
- Loading branch information
1 parent
88c48a7
commit 1f03243
Showing
6 changed files
with
175 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 | ||
--- | ||
|
||
Fix a bug in Mafs linear graphs where the arrows on the ends of the lines would sometimes disappear or flip around |
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
140 changes: 140 additions & 0 deletions
140
packages/perseus/src/widgets/interactive-graphs/graphs/utils.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,140 @@ | ||
import {getIntersectionOfRayWithBox} from "./utils"; | ||
|
||
import type {Interval, vec} from "mafs"; | ||
|
||
describe("getIntersectionOfRayWithBox", () => { | ||
it("returns [0, 0] when the given points are the same", () => { | ||
const box: [Interval, Interval] = [ | ||
[-7, 7], | ||
[-11, 11], | ||
]; | ||
const initialPoint: vec.Vector2 = [3, 5]; | ||
const throughPoint: vec.Vector2 = [3, 5]; | ||
const intersection = getIntersectionOfRayWithBox( | ||
initialPoint, | ||
throughPoint, | ||
box, | ||
); | ||
expect(intersection).toEqual([0, 0]); | ||
}); | ||
|
||
test("given a horizontal ray passing through the origin to the left", () => { | ||
const box: [Interval, Interval] = [ | ||
[-7, 7], | ||
[-11, 11], | ||
]; | ||
const initialPoint: vec.Vector2 = [1, 0]; | ||
const throughPoint: vec.Vector2 = [-1, 0]; | ||
const intersection = getIntersectionOfRayWithBox( | ||
initialPoint, | ||
throughPoint, | ||
box, | ||
); | ||
expect(intersection).toEqual([-7, 0]); | ||
}); | ||
|
||
test("given a horizontal ray passing through the origin to the right", () => { | ||
const box: [Interval, Interval] = [ | ||
[-7, 7], | ||
[-11, 11], | ||
]; | ||
const initialPoint: vec.Vector2 = [-1, 0]; | ||
const throughPoint: vec.Vector2 = [1, 0]; | ||
const intersection = getIntersectionOfRayWithBox( | ||
initialPoint, | ||
throughPoint, | ||
box, | ||
); | ||
expect(intersection).toEqual([7, 0]); | ||
}); | ||
|
||
test("given a vertical ray passing through the origin upward", () => { | ||
const box: [Interval, Interval] = [ | ||
[-7, 7], | ||
[-11, 11], | ||
]; | ||
const initialPoint: vec.Vector2 = [0, -1]; | ||
const throughPoint: vec.Vector2 = [0, 1]; | ||
const intersection = getIntersectionOfRayWithBox( | ||
initialPoint, | ||
throughPoint, | ||
box, | ||
); | ||
expect(intersection).toEqual([0, 11]); | ||
}); | ||
|
||
test("given a vertical ray passing through the origin downward", () => { | ||
const box: [Interval, Interval] = [ | ||
[-7, 7], | ||
[-11, 11], | ||
]; | ||
const initialPoint: vec.Vector2 = [0, 1]; | ||
const throughPoint: vec.Vector2 = [0, -1]; | ||
const intersection = getIntersectionOfRayWithBox( | ||
initialPoint, | ||
throughPoint, | ||
box, | ||
); | ||
expect(intersection).toEqual([0, -11]); | ||
}); | ||
|
||
test("given a y coordinate of -0 for the initialPoint when the ray points right", () => { | ||
const box: [Interval, Interval] = [ | ||
[-7, 7], | ||
[-11, 11], | ||
]; | ||
const initialPoint: vec.Vector2 = [-1, -0]; | ||
const throughPoint: vec.Vector2 = [1, 0]; | ||
const intersection = getIntersectionOfRayWithBox( | ||
initialPoint, | ||
throughPoint, | ||
box, | ||
); | ||
expect(intersection).toEqual([7, 0]); | ||
}); | ||
|
||
test("given a y coordinate of -0 for the throughPoint when the ray points right", () => { | ||
const box: [Interval, Interval] = [ | ||
[-7, 7], | ||
[-11, 11], | ||
]; | ||
const initialPoint: vec.Vector2 = [-1, 0]; | ||
const throughPoint: vec.Vector2 = [1, -0]; | ||
const intersection = getIntersectionOfRayWithBox( | ||
initialPoint, | ||
throughPoint, | ||
box, | ||
); | ||
expect(intersection).toEqual([7, 0]); | ||
}); | ||
|
||
test("given a y coordinate of -0 for the initialPoint when the ray points left", () => { | ||
const box: [Interval, Interval] = [ | ||
[-7, 7], | ||
[-11, 11], | ||
]; | ||
const initialPoint: vec.Vector2 = [1, -0]; | ||
const throughPoint: vec.Vector2 = [-1, 0]; | ||
const intersection = getIntersectionOfRayWithBox( | ||
initialPoint, | ||
throughPoint, | ||
box, | ||
); | ||
expect(intersection).toEqual([-7, 0]); | ||
}); | ||
|
||
test("given a y coordinate of -0 for the throughPoint when the ray points left", () => { | ||
const box: [Interval, Interval] = [ | ||
[-7, 7], | ||
[-11, 11], | ||
]; | ||
const initialPoint: vec.Vector2 = [1, 0]; | ||
const throughPoint: vec.Vector2 = [-1, -0]; | ||
const intersection = getIntersectionOfRayWithBox( | ||
initialPoint, | ||
throughPoint, | ||
box, | ||
); | ||
expect(intersection).toEqual([-7, 0]); | ||
}); | ||
}); |
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