refactor: angleage

This commit is contained in:
cha0s 2021-02-03 13:32:24 -06:00
parent 4be3f5ff5a
commit 7102f225e6
3 changed files with 62 additions and 1 deletions

View File

@ -51,7 +51,9 @@ export const PI_180 = Math.PI / 180;
export const add = (l, r) => l + r;
export const div = (l, r) => l / r;
export const frac = (number) => number % 1;
export const fromRad = (r) => (r % (Math.PI * 2)) / PI_180;
export const angleFromRad = (r) => (
(360 + ((2 * Math.PI - (r + HALF_PI)) % (Math.PI * 2)) / -PI_180) % 360
);
export const mul = (l, r) => l * r;
export const normalizeAngleRange = (min, max) => {
/* eslint-disable no-param-reassign */
@ -82,6 +84,30 @@ export const toRad = (a) => (a % 360) * PI_180;
export function describe() {
return {
children: {
add: {
label: 'Add',
type: 'number',
args: [
{
label: 'lhs',
type: 'number',
},
{
label: 'rhs',
type: 'number',
},
],
},
angleFromRad: {
label: 'Angle from radians',
type: 'number',
args: [
{
label: 'Operand',
type: 'number',
},
],
},
floor: {
label: 'Floor',
type: 'number',
@ -106,6 +132,20 @@ export function describe() {
},
],
},
sub: {
label: 'Sub',
type: 'number',
args: [
{
label: 'lhs',
type: 'number',
},
{
label: 'rhs',
type: 'number',
},
],
},
},
type: 'object',
};

View File

@ -388,6 +388,16 @@ export function describe() {
},
],
},
toRadians: {
type: 'number',
label: 'To radians',
args: [
{
label: 'Vector',
type: 'vector',
},
],
},
sub: {
type: 'vector',
label: '$1 minus $2',

View File

@ -0,0 +1,11 @@
import {expect} from 'chai';
import {angleFromRad, HALF_PI, PI} from '../src/math';
it('can translate radians into degrees', () => {
expect(angleFromRad(0)).to.equal(90);
expect(angleFromRad(HALF_PI)).to.equal(180);
expect(angleFromRad(PI)).to.equal(270);
expect(angleFromRad(PI + HALF_PI)).to.equal(0);
expect(angleFromRad(2 * PI)).to.equal(90);
});