-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Polygon] Remove duplicate points when determining if a polygon can be closed #1978
Conversation
Size Change: +261 B (+0.02%) Total Size: 1.27 MB
ℹ️ View Unchanged
|
npm Snapshot: PublishedGood news!! We've packaged up the latest commit from this PR (0cc8c19) and published it to npm. You Example: yarn add @khanacademy/perseus@PR1978 If you are working in Khan Academy's webapp, you can run: ./dev/tools/bump_perseus_version.sh -t PR1978 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved! Tested it out and it looks so sharp and really solves a lot of additional edge cases that make the polygon really great!
Added a comment about a way to condense the duplication function and make it more efficient but we should be good to go!
@@ -493,6 +493,23 @@ function doMovePoint( | |||
return state; | |||
} | |||
|
|||
// If the last point is being moved to the same position as | |||
// the first point, close the polygon. | |||
if ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this! If you could add this test case to our interactive-graph-reducer.test.ts
that would be perfect!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I ended up removing this code because this function should only be marking the polygon as closed? I noticed in the tests that the polygon's state changed to close even if it had an empty coords array, so I removed these lines to stay consistent with that behavior.
@@ -200,8 +201,15 @@ function doClickPoint( | |||
|
|||
function doClosePolygon(state: InteractiveGraphState): InteractiveGraphState { | |||
if (isUnlimitedGraphState(state) && state.type === "polygon") { | |||
// We want to remove any duplicate points when closing the polygon to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love these comments!
@@ -44,3 +45,20 @@ export const getIntersectionOfRayWithBox = ( | |||
function isBetween(x: number, low: number, high: number) { | |||
return x >= low && x <= high; | |||
} | |||
|
|||
export function getArrayWithoutDuplicates(array: Array<Coord>): Array<Coord> { | |||
const returnArray: Array<Coord> = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might I suggest using the spread operator or the filter method to remove the duplicates. I think they have more efficiencies built in that will make this run with higher performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional caviat, our arrays will likely never be more than 20 points long if that. So we don't necessarily need to worry about it. But it would be nicer to have this code a little shorter and I think this article can help with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to just create a set similar to that, but my understanding is that that doesn't work for array whose elements are also arrays or objects, because it doesn't consider two separate arrays equivalent even if their elements are the same.
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @khanacademy/[email protected] ### Patch Changes - [#2014](#2014) [`763d2d0f1`](763d2d0) Thanks [@somewhatabstract](https://github.com/somewhatabstract)! - Migrate off deprecated ID generation APIs - [#2009](#2009) [`b09d19b7b`](b09d19b) Thanks [@catandthemachines](https://github.com/catandthemachines)! - Fixing bug in creating concave shapes in unlimited polygons. - [#1978](#1978) [`81632c326`](81632c3) Thanks [@nishasy](https://github.com/nishasy)! - [Polygon] Remove duplicate points when determining if a polygon can be closed - [#1999](#1999) [`278527b08`](278527b) Thanks [@catandthemachines](https://github.com/catandthemachines)! - Fixing open polygon scoring issues within exercises and editors. ## @khanacademy/[email protected] ### Patch Changes - [#2014](#2014) [`763d2d0f1`](763d2d0) Thanks [@somewhatabstract](https://github.com/somewhatabstract)! - Migrate off deprecated ID generation APIs - [#1978](#1978) [`81632c326`](81632c3) Thanks [@nishasy](https://github.com/nishasy)! - [Polygon] Remove duplicate points when determining if a polygon can be closed - [#1999](#1999) [`278527b08`](278527b) Thanks [@catandthemachines](https://github.com/catandthemachines)! - Fixing open polygon scoring issues within exercises and editors. - Updated dependencies \[[`763d2d0f1`](763d2d0), [`b09d19b7b`](b09d19b), [`81632c326`](81632c3), [`278527b08`](278527b)]: - @khanacademy/[email protected] ## @khanacademy/[email protected] ### Patch Changes - [#2014](#2014) [`763d2d0f1`](763d2d0) Thanks [@somewhatabstract](https://github.com/somewhatabstract)! - Migrate off deprecated ID generation APIs
Summary:
Right now, if a user were trying to close the polygon by clicking the last point, missed, created a new point instead as a result, and then tried to drag the new point to the first point to close out the polygon, the question would be marked wrong even if the polygon looks right.
Relatedly, if a user were to drag a middle point over another point in the polygon, even if the polygon looks right, it would be marked wrong.
To fix both of these issues, I'm making it so
Issue: https://khanacademy.atlassian.net/browse/LEMS-2711
(and also https://khanacademy.atlassian.net/browse/LEMS-2722)
Test plan:
yarn jest packages/perseus/src/widgets/interactive-graphs/graphs/utils.test.ts
yarn jest packages/perseus/src/widgets/interactive-graphs/mafs-graph.test.tsx
yarn jest packages/perseus/src/widgets/interactive-graphs/reducer/interactive-graph-reducer.test.ts
Storybook
Before:
Screen.Recording.2024-12-12.at.2.21.03.PM.mov
After:
Screen.Recording.2024-12-12.at.2.21.45.PM.mov