Skip to content

Commit

Permalink
Merge pull request #196 from VisActor/feat/compute-quadrant
Browse files Browse the repository at this point in the history
Feat/compute quadrant
  • Loading branch information
TingLittleKang authored Aug 1, 2024
2 parents fb14da3 + 0c93c51 commit 447d811
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@visactor/vutils",
"comment": "feat: add function `computeQuadrant`",
"type": "none"
}
],
"packageName": "@visactor/vutils"
}
15 changes: 14 additions & 1 deletion packages/vutils/__tests__/angle.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { findBoundaryAngles, normalizeAngle, polarToCartesian, calculateMaxRadius } from '../src';
import { findBoundaryAngles, normalizeAngle, polarToCartesian, calculateMaxRadius, computeQuadrant } from '../src';

describe('angle utils', () => {
it('normalizeAngle', () => {
Expand Down Expand Up @@ -41,4 +41,17 @@ describe('angle utils', () => {
calculateMaxRadius({ width: 400, height: 500 }, { x: 200, y: 350 }, -0.8 * Math.PI, 0.1 * Math.PI)
).toBeCloseTo(157.71933363574007);
});

it('computeQuadrant', () => {
expect(computeQuadrant(-0.5)).toBe(1);
expect(computeQuadrant(-1)).toBe(1);
expect(computeQuadrant(-2)).toBe(4);
expect(computeQuadrant(-3)).toBe(4);
expect(computeQuadrant(-4)).toBe(3);
expect(computeQuadrant(0.5)).toBe(2);
expect(computeQuadrant(1)).toBe(2);
expect(computeQuadrant(2)).toBe(3);
expect(computeQuadrant(3)).toBe(3);
expect(computeQuadrant(4)).toBe(4);
});
});
14 changes: 14 additions & 0 deletions packages/vutils/src/angle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,17 @@ export function calculateMaxRadius(

return Math.min.apply(null, radiusList);
}

export type Quadrant = 1 | 2 | 3 | 4;

export function computeQuadrant(angle: number): Quadrant {
angle = normalizeAngle(angle);
if (angle > 0 && angle <= Math.PI / 2) {
return 2;
} else if (angle > Math.PI / 2 && angle <= Math.PI) {
return 3;
} else if (angle > Math.PI && angle <= (3 * Math.PI) / 2) {
return 4;
}
return 1;
}

0 comments on commit 447d811

Please sign in to comment.