@fbltd/math 1.0.37 → 1.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,9 @@
1
1
  export class Point {
2
+ static distance(p1, p2) {
3
+ return p1.reduce((acc, _, i) => {
4
+ return acc + (p1[i] - p2[i]) ** 2;
5
+ }, 0) ** 0.5;
6
+ }
2
7
  static sum(...ps) {
3
8
  let sum = ps[0];
4
9
  for (let i = 1; i < ps.length; i++) {
@@ -9,7 +14,7 @@ export class Point {
9
14
  static scale(p, scale) {
10
15
  return p.map((c) => c * scale);
11
16
  }
12
- static dif(p1, p2) {
17
+ static sub(p1, p2) {
13
18
  return p1.map((c, i) => c - p2[i]);
14
19
  }
15
20
  static abs(p) {
@@ -49,6 +54,6 @@ export class Point {
49
54
  * @returns вектор
50
55
  */
51
56
  static normal(p1, p2, p3) {
52
- return Point.crossProduct(Point.dif(p2, p1), Point.dif(p3, p1));
57
+ return Point.crossProduct(Point.sub(p2, p1), Point.sub(p3, p1));
53
58
  }
54
59
  }
@@ -7,12 +7,9 @@ export function toPositive(value, range) {
7
7
  return Math.abs((range + value % range) % range);
8
8
  }
9
9
  export function isCorrectNumber(value) {
10
- let v = +value;
11
- if (!isNaN(value) && isFinite(value)) {
12
- v = parseFloat(value);
13
- return !isNaN(v) && isFinite(v);
14
- }
15
- return false;
10
+ const v1 = parseFloat(value);
11
+ const v2 = +value;
12
+ return !Array.isArray(value) && v1 === v2 && !isNaN(v1) && isFinite(v1);
16
13
  }
17
14
  export function normalize(value, top, bottom = 0) {
18
15
  return (value - bottom) / (top - bottom);
@@ -20,3 +17,18 @@ export function normalize(value, top, bottom = 0) {
20
17
  export function denormalize(value, top, bottom = 0) {
21
18
  return (top - bottom) * value + bottom;
22
19
  }
20
+ export function isPositive(n) {
21
+ return !isNaN(n) && n > 0;
22
+ }
23
+ export function isNegative(n) {
24
+ return !isNaN(n) && n < 0;
25
+ }
26
+ export function isInteger(n) {
27
+ return !isNaN(n) && isFinite(n) && (n - (n % 1)) === n;
28
+ }
29
+ export function isNatural(n) {
30
+ return isPositive(n) && isInteger(n);
31
+ }
32
+ export function isNonNegativeInteger(n) {
33
+ return !isNegative(n) && isInteger(n);
34
+ }
@@ -5,9 +5,10 @@ export type IPoint2 = [number, number];
5
5
  export type IPoint3 = [number, number, number];
6
6
  export type IPoint = IPoint2 | IPoint3;
7
7
  export declare class Point {
8
+ static distance(p1: IPoint2, p2: IPoint2): number;
8
9
  static sum<T extends IPoint>(...ps: T[]): T;
9
10
  static scale<T extends IPoint>(p: T, scale: number): T;
10
- static dif<T extends IPoint>(p1: T, p2: T): T;
11
+ static sub<T extends IPoint>(p1: T, p2: T): T;
11
12
  static abs<T extends IPoint>(p: T): T;
12
13
  static dotProduct<T extends IPoint>(p1: T, p2: T): number;
13
14
  /**
@@ -3,3 +3,8 @@ export declare function toPositive(value: number, range: number): number;
3
3
  export declare function isCorrectNumber(value: any): boolean;
4
4
  export declare function normalize(value: number, top: number, bottom?: number): number;
5
5
  export declare function denormalize(value: number, top: number, bottom?: number): number;
6
+ export declare function isPositive(n: number): boolean;
7
+ export declare function isNegative(n: number): boolean;
8
+ export declare function isInteger(n: number): boolean;
9
+ export declare function isNatural(n: number): boolean;
10
+ export declare function isNonNegativeInteger(n: number): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fbltd/math",
3
- "version": "1.0.37",
3
+ "version": "1.0.39",
4
4
  "description": "Math and geometry utilities",
5
5
  "sideEffects": false,
6
6
  "main": "dist/bin/index.js",