@thi.ng/math
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -0,7 +1,42 @@ | ||
/** | ||
* Returns vector of `[sin(theta)*n, cos(theta)*n]`. | ||
* | ||
* @param theta | ||
* @param n | ||
*/ | ||
export declare const sincos: (theta: number, n?: number) => number[]; | ||
/** | ||
* Returns vector of `[cos(theta)*n, sin(theta)*n]`. | ||
* | ||
* @param theta | ||
* @param n | ||
*/ | ||
export declare const cossin: (theta: number, n?: number) => number[]; | ||
/** | ||
* Projects `theta` into [0 .. 2π] interval. | ||
* | ||
* @param theta | ||
*/ | ||
export declare const absTheta: (theta: number) => number; | ||
export declare const absInnerAngle: (x: number) => number; | ||
export declare const absInnerAngle: (theta: number) => number; | ||
/** | ||
* Returns smallest absolute angle difference between `a` and `b`. | ||
* Result will be in [0 .. π] interval. | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export declare const angleDist: (a: number, b: number) => number; | ||
/** | ||
* Like `Math.atan2`, but always returns angle in [0 .. TAU) interval. | ||
* | ||
* @param y | ||
* @param x | ||
*/ | ||
export declare const atan2Abs: (y: number, x: number) => number; | ||
/** | ||
* Returns quadrant ID (0-3) of given angle (in radians). | ||
* | ||
* @param theta | ||
*/ | ||
export declare const quadrant: (theta: number) => number; | ||
@@ -11,10 +46,58 @@ /** | ||
* | ||
* @param x angle in radians | ||
* @param theta angle in radians | ||
*/ | ||
export declare const deg: (x: number) => number; | ||
export declare const deg: (theta: number) => number; | ||
/** | ||
* Converts angle to radians. | ||
* | ||
* @param x angle in degrees | ||
* @param theta angle in degrees | ||
*/ | ||
export declare const rad: (x: number) => number; | ||
export declare const rad: (theta: number) => number; | ||
/** | ||
* Cosecant. Approaches `±Infinity` for `theta` near multiples of π. | ||
* | ||
* @param theta angle in radians | ||
*/ | ||
export declare const csc: (theta: number) => number; | ||
/** | ||
* Secant. Approaches `±Infinity` for `theta` near π/2 ± nπ | ||
* | ||
* @param theta angle in radians | ||
*/ | ||
export declare const sec: (theta: number) => number; | ||
/** | ||
* Cotangent. Approaches `±Infinity` for `theta` near multiples of π. | ||
* | ||
* @param theta angle in radians | ||
*/ | ||
export declare const cot: (theta: number) => number; | ||
/** | ||
* Law of Cosines. Takes length of two sides of a triangle and the inner | ||
* angle (in radians) between them. Returns length of third side. | ||
* | ||
* @param a | ||
* @param b | ||
* @param gamma | ||
*/ | ||
export declare const loc: (a: number, b: number, gamma: number) => number; | ||
/** | ||
* Approximates cos(xπ) for x in [-1,1] | ||
* | ||
* @param x | ||
*/ | ||
export declare const normCos: (x: number) => number; | ||
/** | ||
* Fast cosine approximation using `normCos()` (polynomial). Max. error | ||
* ~0.00059693 | ||
* | ||
* In [0 .. 2π] interval, approx. 18-20% faster than `Math.cos` on V8. | ||
* | ||
* @param theta in radians | ||
*/ | ||
export declare const fastCos: (theta: number) => number; | ||
/** | ||
* @see fastCos | ||
* | ||
* @param theta in radians | ||
*/ | ||
export declare const fastSin: (theta: number) => number; |
117
angle.js
@@ -1,2 +0,8 @@ | ||
import { DEG2RAD, HALF_PI, PI, RAD2DEG, TAU } from "./api"; | ||
import { DEG2RAD, HALF_PI, INV_HALF_PI, PI, RAD2DEG, TAU } from "./api"; | ||
/** | ||
* Returns vector of `[sin(theta)*n, cos(theta)*n]`. | ||
* | ||
* @param theta | ||
* @param n | ||
*/ | ||
export const sincos = (theta, n = 1) => [ | ||
@@ -6,2 +12,8 @@ Math.sin(theta) * n, | ||
]; | ||
/** | ||
* Returns vector of `[cos(theta)*n, sin(theta)*n]`. | ||
* | ||
* @param theta | ||
* @param n | ||
*/ | ||
export const cossin = (theta, n = 1) => [ | ||
@@ -11,18 +23,109 @@ Math.cos(theta) * n, | ||
]; | ||
/** | ||
* Projects `theta` into [0 .. 2π] interval. | ||
* | ||
* @param theta | ||
*/ | ||
export const absTheta = (theta) => ((theta %= TAU), theta < 0 ? TAU + theta : theta); | ||
export const absInnerAngle = (x) => ((x = Math.abs(x)), x > PI ? TAU - x : x); | ||
export const absInnerAngle = (theta) => ((theta = Math.abs(theta)), theta > PI ? TAU - theta : theta); | ||
/** | ||
* Returns smallest absolute angle difference between `a` and `b`. | ||
* Result will be in [0 .. π] interval. | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export const angleDist = (a, b) => absInnerAngle(absTheta((b % TAU) - (a % TAU))); | ||
/** | ||
* Like `Math.atan2`, but always returns angle in [0 .. TAU) interval. | ||
* | ||
* @param y | ||
* @param x | ||
*/ | ||
export const atan2Abs = (y, x) => absTheta(Math.atan2(y, x)); | ||
export const quadrant = (theta) => (absTheta(theta) / HALF_PI) | 0; | ||
/** | ||
* Returns quadrant ID (0-3) of given angle (in radians). | ||
* | ||
* @param theta | ||
*/ | ||
export const quadrant = (theta) => (absTheta(theta) * INV_HALF_PI) | 0; | ||
/** | ||
* Converts angle to degrees. | ||
* | ||
* @param x angle in radians | ||
* @param theta angle in radians | ||
*/ | ||
export const deg = (x) => x * RAD2DEG; | ||
export const deg = (theta) => theta * RAD2DEG; | ||
/** | ||
* Converts angle to radians. | ||
* | ||
* @param x angle in degrees | ||
* @param theta angle in degrees | ||
*/ | ||
export const rad = (x) => x * DEG2RAD; | ||
export const rad = (theta) => theta * DEG2RAD; | ||
/** | ||
* Cosecant. Approaches `±Infinity` for `theta` near multiples of π. | ||
* | ||
* @param theta angle in radians | ||
*/ | ||
export const csc = (theta) => 1 / Math.sin(theta); | ||
/** | ||
* Secant. Approaches `±Infinity` for `theta` near π/2 ± nπ | ||
* | ||
* @param theta angle in radians | ||
*/ | ||
export const sec = (theta) => 1 / Math.cos(theta); | ||
/** | ||
* Cotangent. Approaches `±Infinity` for `theta` near multiples of π. | ||
* | ||
* @param theta angle in radians | ||
*/ | ||
export const cot = (theta) => 1 / Math.tan(theta); | ||
/** | ||
* Law of Cosines. Takes length of two sides of a triangle and the inner | ||
* angle (in radians) between them. Returns length of third side. | ||
* | ||
* @param a | ||
* @param b | ||
* @param gamma | ||
*/ | ||
export const loc = (a, b, gamma) => Math.sqrt(a * a + b * b - 2 * a * b * Math.cos(gamma)); | ||
/** | ||
* Approximates cos(xπ) for x in [-1,1] | ||
* | ||
* @param x | ||
*/ | ||
export const normCos = (x) => { | ||
const x2 = x * x; | ||
return 1.0 + x2 * (-4 + 2 * x2); | ||
}; | ||
const __fastCos = (x) => { | ||
const x2 = x * x; | ||
return 0.99940307 + x2 * (-0.49558072 + 0.03679168 * x2); | ||
}; | ||
/** | ||
* Fast cosine approximation using `normCos()` (polynomial). Max. error | ||
* ~0.00059693 | ||
* | ||
* In [0 .. 2π] interval, approx. 18-20% faster than `Math.cos` on V8. | ||
* | ||
* @param theta in radians | ||
*/ | ||
export const fastCos = (theta) => { | ||
theta %= TAU; | ||
theta < 0 && (theta = -theta); | ||
switch ((theta * INV_HALF_PI) | 0) { | ||
case 0: | ||
return __fastCos(theta); | ||
case 1: | ||
return -__fastCos(PI - theta); | ||
case 2: | ||
return -__fastCos(theta - PI); | ||
default: | ||
return __fastCos(TAU - theta); | ||
} | ||
}; | ||
/** | ||
* @see fastCos | ||
* | ||
* @param theta in radians | ||
*/ | ||
export const fastSin = (theta) => fastCos(HALF_PI - theta); |
@@ -7,2 +7,5 @@ export declare const PI: number; | ||
export declare const SIXTH_PI: number; | ||
export declare const INV_PI: number; | ||
export declare const INV_TAU: number; | ||
export declare const INV_HALF_PI: number; | ||
export declare const DEG2RAD: number; | ||
@@ -13,2 +16,4 @@ export declare const RAD2DEG: number; | ||
export declare const SQRT3: number; | ||
export declare const SQRT2_2: number; | ||
export declare const SQRT2_3: number; | ||
export declare const THIRD: number; | ||
@@ -15,0 +20,0 @@ export declare const TWO_THIRD: number; |
@@ -7,2 +7,5 @@ export const PI = Math.PI; | ||
export const SIXTH_PI = PI / 6; | ||
export const INV_PI = 1 / PI; | ||
export const INV_TAU = 1 / TAU; | ||
export const INV_HALF_PI = 1 / HALF_PI; | ||
export const DEG2RAD = PI / 180; | ||
@@ -13,2 +16,4 @@ export const RAD2DEG = 180 / PI; | ||
export const SQRT3 = Math.sqrt(3); | ||
export const SQRT2_2 = SQRT2 / 2; | ||
export const SQRT2_3 = SQRT3 / 2; | ||
export const THIRD = 1 / 3; | ||
@@ -15,0 +20,0 @@ export const TWO_THIRD = 2 / 3; |
@@ -6,2 +6,15 @@ # Change Log | ||
# [1.2.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/math@1.1.1...@thi.ng/math@1.2.0) (2019-03-18) | ||
### Features | ||
* **math:** add consts ([28e9898](https://github.com/thi-ng/umbrella/commit/28e9898)) | ||
* **math:** add cos/sin approximations, loc(), add docstrings ([78ed751](https://github.com/thi-ng/umbrella/commit/78ed751)) | ||
* **math:** more trigonometry ([b5e1c02](https://github.com/thi-ng/umbrella/commit/b5e1c02)) | ||
## [1.1.1](https://github.com/thi-ng/umbrella/compare/@thi.ng/math@1.1.0...@thi.ng/math@1.1.1) (2019-03-01) | ||
@@ -8,0 +21,0 @@ |
@@ -11,2 +11,5 @@ 'use strict'; | ||
const SIXTH_PI = PI / 6; | ||
const INV_PI = 1 / PI; | ||
const INV_TAU = 1 / TAU; | ||
const INV_HALF_PI = 1 / HALF_PI; | ||
const DEG2RAD = PI / 180; | ||
@@ -17,2 +20,4 @@ const RAD2DEG = 180 / PI; | ||
const SQRT3 = Math.sqrt(3); | ||
const SQRT2_2 = SQRT2 / 2; | ||
const SQRT2_3 = SQRT3 / 2; | ||
const THIRD = 1 / 3; | ||
@@ -35,8 +40,35 @@ const TWO_THIRD = 2 / 3; | ||
const absTheta = (theta) => ((theta %= TAU), theta < 0 ? TAU + theta : theta); | ||
const absInnerAngle = (x) => ((x = Math.abs(x)), x > PI ? TAU - x : x); | ||
const absInnerAngle = (theta) => ((theta = Math.abs(theta)), theta > PI ? TAU - theta : theta); | ||
const angleDist = (a, b) => absInnerAngle(absTheta((b % TAU) - (a % TAU))); | ||
const atan2Abs = (y, x) => absTheta(Math.atan2(y, x)); | ||
const quadrant = (theta) => (absTheta(theta) / HALF_PI) | 0; | ||
const deg = (x) => x * RAD2DEG; | ||
const rad = (x) => x * DEG2RAD; | ||
const quadrant = (theta) => (absTheta(theta) * INV_HALF_PI) | 0; | ||
const deg = (theta) => theta * RAD2DEG; | ||
const rad = (theta) => theta * DEG2RAD; | ||
const csc = (theta) => 1 / Math.sin(theta); | ||
const sec = (theta) => 1 / Math.cos(theta); | ||
const cot = (theta) => 1 / Math.tan(theta); | ||
const loc = (a, b, gamma) => Math.sqrt(a * a + b * b - 2 * a * b * Math.cos(gamma)); | ||
const normCos = (x) => { | ||
const x2 = x * x; | ||
return 1.0 + x2 * (-4 + 2 * x2); | ||
}; | ||
const __fastCos = (x) => { | ||
const x2 = x * x; | ||
return 0.99940307 + x2 * (-0.49558072 + 0.03679168 * x2); | ||
}; | ||
const fastCos = (theta) => { | ||
theta %= TAU; | ||
theta < 0 && (theta = -theta); | ||
switch ((theta * INV_HALF_PI) | 0) { | ||
case 0: | ||
return __fastCos(theta); | ||
case 1: | ||
return -__fastCos(PI - theta); | ||
case 2: | ||
return -__fastCos(theta - PI); | ||
default: | ||
return __fastCos(TAU - theta); | ||
} | ||
}; | ||
const fastSin = (theta) => fastCos(HALF_PI - theta); | ||
@@ -256,2 +288,5 @@ const abs = Math.abs; | ||
exports.SIXTH_PI = SIXTH_PI; | ||
exports.INV_PI = INV_PI; | ||
exports.INV_TAU = INV_TAU; | ||
exports.INV_HALF_PI = INV_HALF_PI; | ||
exports.DEG2RAD = DEG2RAD; | ||
@@ -262,2 +297,4 @@ exports.RAD2DEG = RAD2DEG; | ||
exports.SQRT3 = SQRT3; | ||
exports.SQRT2_2 = SQRT2_2; | ||
exports.SQRT2_3 = SQRT2_3; | ||
exports.THIRD = THIRD; | ||
@@ -278,2 +315,9 @@ exports.TWO_THIRD = TWO_THIRD; | ||
exports.rad = rad; | ||
exports.csc = csc; | ||
exports.sec = sec; | ||
exports.cot = cot; | ||
exports.loc = loc; | ||
exports.normCos = normCos; | ||
exports.fastCos = fastCos; | ||
exports.fastSin = fastSin; | ||
exports.eqDelta = eqDelta; | ||
@@ -280,0 +324,0 @@ exports.eqDeltaFixed = eqDeltaFixed; |
@@ -13,2 +13,5 @@ (function (global, factory) { | ||
const SIXTH_PI = PI / 6; | ||
const INV_PI = 1 / PI; | ||
const INV_TAU = 1 / TAU; | ||
const INV_HALF_PI = 1 / HALF_PI; | ||
const DEG2RAD = PI / 180; | ||
@@ -19,2 +22,4 @@ const RAD2DEG = 180 / PI; | ||
const SQRT3 = Math.sqrt(3); | ||
const SQRT2_2 = SQRT2 / 2; | ||
const SQRT2_3 = SQRT3 / 2; | ||
const THIRD = 1 / 3; | ||
@@ -37,8 +42,35 @@ const TWO_THIRD = 2 / 3; | ||
const absTheta = (theta) => ((theta %= TAU), theta < 0 ? TAU + theta : theta); | ||
const absInnerAngle = (x) => ((x = Math.abs(x)), x > PI ? TAU - x : x); | ||
const absInnerAngle = (theta) => ((theta = Math.abs(theta)), theta > PI ? TAU - theta : theta); | ||
const angleDist = (a, b) => absInnerAngle(absTheta((b % TAU) - (a % TAU))); | ||
const atan2Abs = (y, x) => absTheta(Math.atan2(y, x)); | ||
const quadrant = (theta) => (absTheta(theta) / HALF_PI) | 0; | ||
const deg = (x) => x * RAD2DEG; | ||
const rad = (x) => x * DEG2RAD; | ||
const quadrant = (theta) => (absTheta(theta) * INV_HALF_PI) | 0; | ||
const deg = (theta) => theta * RAD2DEG; | ||
const rad = (theta) => theta * DEG2RAD; | ||
const csc = (theta) => 1 / Math.sin(theta); | ||
const sec = (theta) => 1 / Math.cos(theta); | ||
const cot = (theta) => 1 / Math.tan(theta); | ||
const loc = (a, b, gamma) => Math.sqrt(a * a + b * b - 2 * a * b * Math.cos(gamma)); | ||
const normCos = (x) => { | ||
const x2 = x * x; | ||
return 1.0 + x2 * (-4 + 2 * x2); | ||
}; | ||
const __fastCos = (x) => { | ||
const x2 = x * x; | ||
return 0.99940307 + x2 * (-0.49558072 + 0.03679168 * x2); | ||
}; | ||
const fastCos = (theta) => { | ||
theta %= TAU; | ||
theta < 0 && (theta = -theta); | ||
switch ((theta * INV_HALF_PI) | 0) { | ||
case 0: | ||
return __fastCos(theta); | ||
case 1: | ||
return -__fastCos(PI - theta); | ||
case 2: | ||
return -__fastCos(theta - PI); | ||
default: | ||
return __fastCos(TAU - theta); | ||
} | ||
}; | ||
const fastSin = (theta) => fastCos(HALF_PI - theta); | ||
@@ -258,2 +290,5 @@ const abs = Math.abs; | ||
exports.SIXTH_PI = SIXTH_PI; | ||
exports.INV_PI = INV_PI; | ||
exports.INV_TAU = INV_TAU; | ||
exports.INV_HALF_PI = INV_HALF_PI; | ||
exports.DEG2RAD = DEG2RAD; | ||
@@ -264,2 +299,4 @@ exports.RAD2DEG = RAD2DEG; | ||
exports.SQRT3 = SQRT3; | ||
exports.SQRT2_2 = SQRT2_2; | ||
exports.SQRT2_3 = SQRT2_3; | ||
exports.THIRD = THIRD; | ||
@@ -280,2 +317,9 @@ exports.TWO_THIRD = TWO_THIRD; | ||
exports.rad = rad; | ||
exports.csc = csc; | ||
exports.sec = sec; | ||
exports.cot = cot; | ||
exports.loc = loc; | ||
exports.normCos = normCos; | ||
exports.fastCos = fastCos; | ||
exports.fastSin = fastSin; | ||
exports.eqDelta = eqDelta; | ||
@@ -282,0 +326,0 @@ exports.eqDeltaFixed = eqDeltaFixed; |
{ | ||
"name": "@thi.ng/math", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Assorted common math functions & utilities", | ||
@@ -47,3 +47,3 @@ "module": "./index.js", | ||
"sideEffects": false, | ||
"gitHead": "e43f57c7554fd78380bba58d37ae62ca01221eeb" | ||
"gitHead": "b032167da28b83e93d3cbc54a47b053092ad7c4f" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105912
35
1764