-
Notifications
You must be signed in to change notification settings - Fork 354
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
[SR] Polygon (limited) - Add screen reader experience #2173
Changes from 1 commit
595ffd2
e5531d1
6be1ed8
0dfecbd
708d821
09eb34e
0b8c5ed
05a1319
9117d5f
d795acc
299289f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
export function srFormatNumber(a: number, locale: string): string { | ||
export function srFormatNumber( | ||
a: number, | ||
locale: string, | ||
maximumFractionDigits?: number, | ||
): string { | ||
// adding zero here converts negative zero to positive zero. | ||
return (0 + a).toLocaleString(locale, { | ||
maximumFractionDigits: 3, | ||
maximumFractionDigits: maximumFractionDigits ?? 3, | ||
useGrouping: false, // no thousands separators | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import {vec} from "mafs"; | ||
|
||
import {srFormatNumber} from "./screenreader-text"; | ||
|
||
import type {PerseusStrings} from "../../../strings"; | ||
import type {PairOfPoints} from "../types"; | ||
import type {Coord} from "@khanacademy/perseus"; | ||
import type {Interval, vec} from "mafs"; | ||
import type {Interval} from "mafs"; | ||
|
||
/** | ||
* Given a ray and a rectangular box, find the point where the ray intersects | ||
|
@@ -237,3 +239,74 @@ export function getQuadraticXIntercepts( | |
|
||
return [x1, x2]; | ||
} | ||
|
||
export function getAngleFromPoints(points: Coord[], i: number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love this! It might be good to add a comment or two just to help save future devs some time. |
||
if (points.length < 3) { | ||
return null; | ||
} | ||
|
||
const point = points.at(i); | ||
const pt1 = points.at(i - 1); | ||
const pt2 = points[(i + 1) % points.length]; | ||
if (!point || !pt1 || !pt2) { | ||
return null; | ||
} | ||
|
||
const a = vec.dist(point, pt1); | ||
const b = vec.dist(point, pt2); | ||
const c = vec.dist(pt1, pt2); | ||
|
||
// Law of cosines | ||
const angle = Math.acos((a ** 2 + b ** 2 - c ** 2) / (2 * a * b)); | ||
|
||
return angle; | ||
} | ||
|
||
export function radianToDegree(radians: number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could have sworn we already had a function for this, possibly in kmath? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting here for posterity: kmath had |
||
const degree = (radians / Math.PI) * 180; | ||
// Account for floating point errors. | ||
return Number(degree.toPrecision(15)); | ||
} | ||
|
||
export function getSideLengthsFromPoints( | ||
points: Coord[], | ||
i: number, | ||
): Array<{ | ||
pointIndex: number; | ||
sideLength: number; | ||
}> { | ||
if (points.length < 2) { | ||
return []; | ||
} | ||
|
||
const returnArray: Array<{ | ||
pointIndex: number; | ||
sideLength: number; | ||
}> = []; | ||
const point = points[i]; | ||
// If this point is the first point, then side 1 is the | ||
// last point in the list. | ||
const point1Index = i === 0 ? points.length - 1 : i - 1; | ||
const point1 = points[point1Index]; | ||
// Make sure the previous point is not the same | ||
// as the current point. | ||
const side1 = i !== point1Index ? vec.dist(point, point1) : null; | ||
if (side1) { | ||
returnArray.push({pointIndex: point1Index, sideLength: side1}); | ||
} | ||
|
||
// See if there is a side 2. | ||
const point2Index = (i + 1) % points.length; | ||
const point2 = points[point2Index]; | ||
// Make sure that the next point is not the same as the | ||
// current point, and don't repeat the first point. | ||
const side2 = | ||
i !== point2Index && point2Index !== point1Index | ||
? vec.dist(point, point2) | ||
: null; | ||
if (side2) { | ||
returnArray.push({pointIndex: point2Index, sideLength: side2}); | ||
} | ||
|
||
return returnArray; | ||
} |
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.
Adding this parameter so that the screen reader label for the side angles can match what's presented by the "show angles" button (which is only one decimal point).