Skip to content

Commit

Permalink
remove update of angle-indicator files
Browse files Browse the repository at this point in the history
  • Loading branch information
anakaren-rojas committed Dec 4, 2024
1 parent 96d2474 commit b055724
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {getClockwiseAngle} from "../../math";

import {
adjustCoordsForAngleCalculation,
getWholeAngleMeasure,
shouldDrawArcOutside,
} from "./angle-indicators";
import {shouldDrawArcOutside} from "./angle-indicators";

import type {CollinearTuple} from "../../../../perseus-types";
import type {Coord} from "@khanacademy/perseus";
Expand Down Expand Up @@ -108,67 +104,3 @@ describe("shouldDrawArcOutside", () => {
expect(getClockwiseAngle(coords)).toBe(45);
});
});

describe("getWholeAngleMeasure", () => {
test("should return 0 for no angle", () => {
const coords: [Coord, Coord, Coord] = [
[0, 0],
[0, 0],
[0, 0],
];
const vertex = coords[1];

expect(getWholeAngleMeasure(coords, vertex)).toBe(0);
});

test("should return 270 for a reflex right angle", () => {
const coords: [Coord, Coord, Coord] = [
[0, 0],
[0, 1],
[1, 0],
];
const vertex = coords[1];

expect(getWholeAngleMeasure(coords, vertex)).toBe(270);
});

test("should not return decimals for angle", () => {
const coords: [Coord, Coord, Coord] = [
[0, 0],
[7, 0.5],
[12.5, 2.5],
];
const vertex = coords[1];
expect(getWholeAngleMeasure(coords, vertex)).toBe(184);
});
});

describe("getClockwiseCoords", () => {
test("should return the coordinates in clockwise order", () => {
const coords: [Coord, Coord, Coord] = [
[0, 0],
[0, 1],
[1, 1],
];

expect(adjustCoordsForAngleCalculation(coords, coords[0])).toEqual(
coords,
);
});

test("should return the coordinates in counter-clockwise order when reflex angles are allowed", () => {
const coords: [Coord, Coord, Coord] = [
[0, 0],
[1, 0],
[0, 1],
];

expect(
adjustCoordsForAngleCalculation(coords, coords[0], true),
).toEqual([
[0, 1],
[1, 0],
[0, 0],
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {MafsCssTransformWrapper} from "./css-transform-wrapper";
import {TextLabel} from "./text-label";

import type {CollinearTuple} from "../../../../perseus-types";
import type {Vector2} from "@use-gesture/react";
import type {Interval} from "mafs";

interface PolygonAngleProps {
Expand Down Expand Up @@ -145,44 +144,24 @@ interface AngleProps {
range: [Interval, Interval];
}

export function getWholeAngleMeasure(coords: Vector2[], vertex: vec.Vector2) {
const startAngle = getAngleFromVertex(coords[0], vertex);
const endAngle = getAngleFromVertex(coords[1], vertex);
const angle = (startAngle + 360 - endAngle) % 360;

// return only whole angles
return parseFloat(angle.toFixed(0));
}

export function adjustCoordsForAngleCalculation(
coords: Vector2[],
vertex: vec.Vector2,
allowReflexAngles: boolean = false,
) {
// Check if the points are clockwise or not, depending on whether we allow reflex angles
const areClockwise = clockwise([...coords, vertex]);
const shouldReverseCoords = areClockwise && !allowReflexAngles;

// Reverse the coordinates accordingly to ensure the angle is calculated correctly
return shouldReverseCoords ? coords : coords.reverse();
}

export const Angle = ({
vertex,
coords,
showAngles,
allowReflexAngles,
range,
}: AngleProps) => {
// Get the clockwise coordinates
const clockwiseCoords = adjustCoordsForAngleCalculation(
coords,
vertex,
allowReflexAngles,
);
// Check if the points are clockwise or not, depending on whether we allow reflex angles
const areClockwise = clockwise([...coords, vertex]);
const shouldReverseCoords = areClockwise && !allowReflexAngles;

// Reverse the coordinates accordingly to ensure the angle is calculated correctly
const clockwiseCoords = shouldReverseCoords ? coords : coords.reverse();

// Calculate the angles between the two points
const angle = getWholeAngleMeasure(clockwiseCoords, vertex);
const startAngle = getAngleFromVertex(clockwiseCoords[0], vertex);
const endAngle = getAngleFromVertex(clockwiseCoords[1], vertex);
const angle = (startAngle + 360 - endAngle) % 360;

// Check if the angle is reflexive
const isReflexive = angle > 180;
Expand Down Expand Up @@ -225,6 +204,9 @@ export const Angle = ({
// Create the SVG path for the arc
const arc = `M ${x1} ${y1} A ${radius} ${radius} 0 ${largeArcFlag} ${sweepFlag} ${x2} ${y2}`;

// We only ever want to show whole angles
const angleLabel = parseFloat(angle.toFixed(0)); // Only want to show whole angles

// Calculate the text position based on the angle and whether we allow reflex angles
// Let's try the angle bisector method to find the midpoint of the arc
const [textX, textY] = calculateBisectorPoint(
Expand Down Expand Up @@ -263,7 +245,7 @@ export const Angle = ({
)}
{showAngles && (
<TextLabel x={textX} y={textY} color={color.blue}>
{angle}°
{angleLabel}°
</TextLabel>
)}
</>
Expand Down

0 comments on commit b055724

Please sign in to comment.