nhanh-pure-function 1.3.10 → 1.3.11
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.
- package/README.md +9 -9
- package/lib/Index.d.ts +8 -8
- package/lib/Index.js +5 -5
- package/lib/Math/Math.d.ts +73 -73
- package/lib/Math/Math.js +165 -165
- package/lib/User/User.d.ts +198 -198
- package/lib/User/User.js +536 -536
- package/lib/Utility/Utility.d.ts +251 -240
- package/lib/Utility/Utility.js +537 -513
- package/lib/index.less +5 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# nhanh-pure-function
|
|
2
|
-
|
|
3
|
-
纯函数工具库,提供了一些常用的纯函数工具,帮助您在 JavaScript/TypeScript 项目中更高效地处理数据。
|
|
4
|
-
|
|
5
|
-
## 安装
|
|
6
|
-
|
|
7
|
-
您可以通过 npm 安装该库:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
1
|
+
# nhanh-pure-function
|
|
2
|
+
|
|
3
|
+
纯函数工具库,提供了一些常用的纯函数工具,帮助您在 JavaScript/TypeScript 项目中更高效地处理数据。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
您可以通过 npm 安装该库:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
10
|
npm install nhanh-pure-function
|
package/lib/Index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from "./Utility/Utility";
|
|
2
|
-
export * from "./User/User";
|
|
3
|
-
export * from "./Math/Math";
|
|
4
|
-
|
|
5
|
-
/** 提取单个函数的参数类型 */
|
|
6
|
-
export type ExtractParameters<T> = T extends (...args: infer P) => any
|
|
7
|
-
? P
|
|
8
|
-
: never;
|
|
1
|
+
export * from "./Utility/Utility";
|
|
2
|
+
export * from "./User/User";
|
|
3
|
+
export * from "./Math/Math";
|
|
4
|
+
|
|
5
|
+
/** 提取单个函数的参数类型 */
|
|
6
|
+
export type ExtractParameters<T> = T extends (...args: infer P) => any
|
|
7
|
+
? P
|
|
8
|
+
: never;
|
package/lib/Index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "./index.css";
|
|
2
|
-
|
|
3
|
-
export * from "./Utility/Utility";
|
|
4
|
-
export * from "./User/User";
|
|
5
|
-
export * from "./Math/Math";
|
|
1
|
+
import "./index.css";
|
|
2
|
+
|
|
3
|
+
export * from "./Utility/Utility";
|
|
4
|
+
export * from "./User/User";
|
|
5
|
+
export * from "./Math/Math";
|
package/lib/Math/Math.d.ts
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 转为百分比字符串
|
|
3
|
-
* @param value 分子
|
|
4
|
-
* @param totalValue 分母
|
|
5
|
-
* @param decimalPlaces 保留小数位
|
|
6
|
-
* @returns 10.00%
|
|
7
|
-
*/
|
|
8
|
-
export function _ConvertToPercentage(
|
|
9
|
-
value: number,
|
|
10
|
-
totalValue: number,
|
|
11
|
-
decimalPlaces?: number
|
|
12
|
-
): number;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 误差范围
|
|
16
|
-
* @param value 需要判断的数字
|
|
17
|
-
* @param target 目标数字
|
|
18
|
-
* @param errorMargin 正负误差范围
|
|
19
|
-
* @returns 是否在误差内
|
|
20
|
-
*/
|
|
21
|
-
export function _IsWithinErrorMargin(
|
|
22
|
-
value: number,
|
|
23
|
-
target: number,
|
|
24
|
-
errorMargin: number
|
|
25
|
-
): boolean;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 进度
|
|
29
|
-
* @param {(schedule)=>void} callback callback( 进度百分比 )
|
|
30
|
-
* @param {Number} TIME 总时长
|
|
31
|
-
*/
|
|
32
|
-
export function _Schedule(
|
|
33
|
-
callback: (schedule: number) => void,
|
|
34
|
-
TIME: number
|
|
35
|
-
): void;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 格式化数字,给数字加上千位分隔符。
|
|
39
|
-
* @param {number} number - 要格式化的数字。
|
|
40
|
-
* @returns {string} - 格式化后的字符串。
|
|
41
|
-
*/
|
|
42
|
-
export function _FormatNumber(number: number): string;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 纯数字转 数字加单位
|
|
46
|
-
* @param number 数字或字符串数字
|
|
47
|
-
* @param config : {
|
|
48
|
-
* join 拼接起来吗
|
|
49
|
-
* suffix 后缀
|
|
50
|
-
* integer 不超过万位的数字时保持整数吗
|
|
51
|
-
* }
|
|
52
|
-
* @returns 123456 --> 12.34万 | [ 12.34 , 万 ]
|
|
53
|
-
*/
|
|
54
|
-
export function _FormatNumberWithUnit(
|
|
55
|
-
number: string | number,
|
|
56
|
-
config?: {
|
|
57
|
-
join?: boolean;
|
|
58
|
-
suffix?: string;
|
|
59
|
-
integer?: boolean;
|
|
60
|
-
}
|
|
61
|
-
): string | [number, string];
|
|
62
|
-
|
|
63
|
-
interface Point {
|
|
64
|
-
x: number;
|
|
65
|
-
y: number;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* 判断点是否在多边形内
|
|
69
|
-
* @param point - 待检测的点,包含 x 和 y 坐标
|
|
70
|
-
* @param polygon - 多边形的点集,数组形式,每个点包含 x 和 y 坐标
|
|
71
|
-
* @returns boolean - 点是否在多边形内
|
|
72
|
-
*/
|
|
73
|
-
export function _IsPointInPolygon(point: Point, polygon: Point[]): boolean;
|
|
1
|
+
/**
|
|
2
|
+
* 转为百分比字符串
|
|
3
|
+
* @param value 分子
|
|
4
|
+
* @param totalValue 分母
|
|
5
|
+
* @param decimalPlaces 保留小数位
|
|
6
|
+
* @returns 10.00%
|
|
7
|
+
*/
|
|
8
|
+
export function _ConvertToPercentage(
|
|
9
|
+
value: number,
|
|
10
|
+
totalValue: number,
|
|
11
|
+
decimalPlaces?: number
|
|
12
|
+
): number;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 误差范围
|
|
16
|
+
* @param value 需要判断的数字
|
|
17
|
+
* @param target 目标数字
|
|
18
|
+
* @param errorMargin 正负误差范围
|
|
19
|
+
* @returns 是否在误差内
|
|
20
|
+
*/
|
|
21
|
+
export function _IsWithinErrorMargin(
|
|
22
|
+
value: number,
|
|
23
|
+
target: number,
|
|
24
|
+
errorMargin: number
|
|
25
|
+
): boolean;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 进度
|
|
29
|
+
* @param {(schedule)=>void} callback callback( 进度百分比 )
|
|
30
|
+
* @param {Number} TIME 总时长
|
|
31
|
+
*/
|
|
32
|
+
export function _Schedule(
|
|
33
|
+
callback: (schedule: number) => void,
|
|
34
|
+
TIME: number
|
|
35
|
+
): void;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 格式化数字,给数字加上千位分隔符。
|
|
39
|
+
* @param {number} number - 要格式化的数字。
|
|
40
|
+
* @returns {string} - 格式化后的字符串。
|
|
41
|
+
*/
|
|
42
|
+
export function _FormatNumber(number: number): string;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 纯数字转 数字加单位
|
|
46
|
+
* @param number 数字或字符串数字
|
|
47
|
+
* @param config : {
|
|
48
|
+
* join 拼接起来吗
|
|
49
|
+
* suffix 后缀
|
|
50
|
+
* integer 不超过万位的数字时保持整数吗
|
|
51
|
+
* }
|
|
52
|
+
* @returns 123456 --> 12.34万 | [ 12.34 , 万 ]
|
|
53
|
+
*/
|
|
54
|
+
export function _FormatNumberWithUnit(
|
|
55
|
+
number: string | number,
|
|
56
|
+
config?: {
|
|
57
|
+
join?: boolean;
|
|
58
|
+
suffix?: string;
|
|
59
|
+
integer?: boolean;
|
|
60
|
+
}
|
|
61
|
+
): string | [number, string];
|
|
62
|
+
|
|
63
|
+
interface Point {
|
|
64
|
+
x: number;
|
|
65
|
+
y: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 判断点是否在多边形内
|
|
69
|
+
* @param point - 待检测的点,包含 x 和 y 坐标
|
|
70
|
+
* @param polygon - 多边形的点集,数组形式,每个点包含 x 和 y 坐标
|
|
71
|
+
* @returns boolean - 点是否在多边形内
|
|
72
|
+
*/
|
|
73
|
+
export function _IsPointInPolygon(point: Point, polygon: Point[]): boolean;
|
package/lib/Math/Math.js
CHANGED
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 转为百分比字符串
|
|
3
|
-
* @param value 分子
|
|
4
|
-
* @param totalValue 分母
|
|
5
|
-
* @param decimalPlaces 保留小数位
|
|
6
|
-
* @returns 10.00%
|
|
7
|
-
*/
|
|
8
|
-
export function _ConvertToPercentage(value, totalValue, decimalPlaces = 2) {
|
|
9
|
-
if (
|
|
10
|
-
typeof value !== "number" ||
|
|
11
|
-
typeof totalValue !== "number" ||
|
|
12
|
-
typeof decimalPlaces !== "number" ||
|
|
13
|
-
totalValue == 0
|
|
14
|
-
) {
|
|
15
|
-
console.error("异常输入:", arguments);
|
|
16
|
-
return "0.00%";
|
|
17
|
-
}
|
|
18
|
-
return (
|
|
19
|
-
Number(
|
|
20
|
-
parseInt((value / totalValue) * Math.pow(10, 2 + decimalPlaces)) /
|
|
21
|
-
Math.pow(10, decimalPlaces)
|
|
22
|
-
) || 0
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* 误差范围
|
|
28
|
-
* @param value 需要判断的数字
|
|
29
|
-
* @param target 目标数字
|
|
30
|
-
* @param errorMargin 正负误差范围
|
|
31
|
-
* @returns 是否在误差内
|
|
32
|
-
*/
|
|
33
|
-
export function _IsWithinErrorMargin(value, target, errorMargin) {
|
|
34
|
-
return Math.abs(value - target) <= errorMargin;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 进度
|
|
39
|
-
* @param {(schedule)=>void} callback callback( 进度百分比 )
|
|
40
|
-
* @param {Number} TIME 总时长
|
|
41
|
-
*/
|
|
42
|
-
export function _Schedule(callback, TIME = 500) {
|
|
43
|
-
let t;
|
|
44
|
-
function loop(time) {
|
|
45
|
-
if (!t) t = time;
|
|
46
|
-
let percentage = Math.min((time - t) / TIME, 1);
|
|
47
|
-
callback(percentage);
|
|
48
|
-
if (time - t < TIME) requestAnimationFrame(loop);
|
|
49
|
-
}
|
|
50
|
-
requestAnimationFrame(loop);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 格式化数字,给数字加上千位分隔符。
|
|
55
|
-
* @param {number} number - 要格式化的数字。
|
|
56
|
-
* @returns {string} - 格式化后的字符串。
|
|
57
|
-
*/
|
|
58
|
-
export function _FormatNumber(number) {
|
|
59
|
-
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 纯数字转 数字加单位
|
|
64
|
-
* @param number 数字或字符串数字
|
|
65
|
-
* @param config : {
|
|
66
|
-
* join 拼接起来吗
|
|
67
|
-
* suffix 后缀
|
|
68
|
-
* integer 不超过万位的数字时保持整数吗
|
|
69
|
-
* }
|
|
70
|
-
* @returns 123456 --> 12.34万 | [ 12.34 , 万 ]
|
|
71
|
-
*/
|
|
72
|
-
export function _FormatNumberWithUnit(number, config = {}) {
|
|
73
|
-
const { join, suffix, integer } = Object.assign(
|
|
74
|
-
{
|
|
75
|
-
join: true,
|
|
76
|
-
suffix: "",
|
|
77
|
-
integer: false,
|
|
78
|
-
},
|
|
79
|
-
config
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
function _join(value, suffix, plus = true) {
|
|
83
|
-
value = (plus ? "" : "-") + value;
|
|
84
|
-
if (join) return value + suffix;
|
|
85
|
-
else return [value, suffix];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (typeof number == "string") {
|
|
89
|
-
if (!/^\d+$/.test(number.trim())) {
|
|
90
|
-
console.error("错误输入:", number);
|
|
91
|
-
return _join(0, suffix);
|
|
92
|
-
}
|
|
93
|
-
} else if (typeof number != "number") {
|
|
94
|
-
console.error("错误输入:", number);
|
|
95
|
-
return _join(0, suffix);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (Math.abs(number) == Infinity || number == 0) {
|
|
99
|
-
return _join(0, suffix);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
number = Number(number);
|
|
103
|
-
const plus = number >= 0;
|
|
104
|
-
number = Math.abs(number);
|
|
105
|
-
|
|
106
|
-
const units = [
|
|
107
|
-
"",
|
|
108
|
-
"万",
|
|
109
|
-
"亿",
|
|
110
|
-
"兆",
|
|
111
|
-
"京",
|
|
112
|
-
"垓",
|
|
113
|
-
"秭",
|
|
114
|
-
"穰",
|
|
115
|
-
"沟",
|
|
116
|
-
"涧",
|
|
117
|
-
"正",
|
|
118
|
-
"载",
|
|
119
|
-
"极",
|
|
120
|
-
];
|
|
121
|
-
|
|
122
|
-
/** 计算位数 */
|
|
123
|
-
const digits = Math.floor(Math.log10(number) / 4);
|
|
124
|
-
|
|
125
|
-
/** 不超过万位的数字直接返回 */
|
|
126
|
-
if (digits === 0) {
|
|
127
|
-
if (integer) {
|
|
128
|
-
return _join(number, suffix, plus);
|
|
129
|
-
} else {
|
|
130
|
-
return _join(number.toFixed(2), suffix, plus);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const dividedNumber = number / Math.pow(10000, digits);
|
|
135
|
-
const formattedNumber = dividedNumber.toFixed(2);
|
|
136
|
-
|
|
137
|
-
return _join(formattedNumber, units[digits] + suffix, plus);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 判断点是否在多边形内
|
|
142
|
-
* @param point - 待检测的点,包含 x 和 y 坐标
|
|
143
|
-
* @param polygon - 多边形的点集,数组形式,每个点包含 x 和 y 坐标
|
|
144
|
-
* @returns boolean - 点是否在多边形内
|
|
145
|
-
*/
|
|
146
|
-
export function _IsPointInPolygon(point, polygon) {
|
|
147
|
-
let isInside = false;
|
|
148
|
-
|
|
149
|
-
const { x, y } = point;
|
|
150
|
-
const len = polygon.length;
|
|
151
|
-
|
|
152
|
-
for (let i = 0, j = len - 1; i < len; j = i++) {
|
|
153
|
-
const xi = polygon[i].x,
|
|
154
|
-
yi = polygon[i].y;
|
|
155
|
-
const xj = polygon[j].x,
|
|
156
|
-
yj = polygon[j].y;
|
|
157
|
-
|
|
158
|
-
const intersect =
|
|
159
|
-
yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
|
|
160
|
-
|
|
161
|
-
if (intersect) isInside = !isInside;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return isInside;
|
|
165
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 转为百分比字符串
|
|
3
|
+
* @param value 分子
|
|
4
|
+
* @param totalValue 分母
|
|
5
|
+
* @param decimalPlaces 保留小数位
|
|
6
|
+
* @returns 10.00%
|
|
7
|
+
*/
|
|
8
|
+
export function _ConvertToPercentage(value, totalValue, decimalPlaces = 2) {
|
|
9
|
+
if (
|
|
10
|
+
typeof value !== "number" ||
|
|
11
|
+
typeof totalValue !== "number" ||
|
|
12
|
+
typeof decimalPlaces !== "number" ||
|
|
13
|
+
totalValue == 0
|
|
14
|
+
) {
|
|
15
|
+
console.error("异常输入:", arguments);
|
|
16
|
+
return "0.00%";
|
|
17
|
+
}
|
|
18
|
+
return (
|
|
19
|
+
Number(
|
|
20
|
+
parseInt((value / totalValue) * Math.pow(10, 2 + decimalPlaces)) /
|
|
21
|
+
Math.pow(10, decimalPlaces)
|
|
22
|
+
) || 0
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 误差范围
|
|
28
|
+
* @param value 需要判断的数字
|
|
29
|
+
* @param target 目标数字
|
|
30
|
+
* @param errorMargin 正负误差范围
|
|
31
|
+
* @returns 是否在误差内
|
|
32
|
+
*/
|
|
33
|
+
export function _IsWithinErrorMargin(value, target, errorMargin) {
|
|
34
|
+
return Math.abs(value - target) <= errorMargin;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 进度
|
|
39
|
+
* @param {(schedule)=>void} callback callback( 进度百分比 )
|
|
40
|
+
* @param {Number} TIME 总时长
|
|
41
|
+
*/
|
|
42
|
+
export function _Schedule(callback, TIME = 500) {
|
|
43
|
+
let t;
|
|
44
|
+
function loop(time) {
|
|
45
|
+
if (!t) t = time;
|
|
46
|
+
let percentage = Math.min((time - t) / TIME, 1);
|
|
47
|
+
callback(percentage);
|
|
48
|
+
if (time - t < TIME) requestAnimationFrame(loop);
|
|
49
|
+
}
|
|
50
|
+
requestAnimationFrame(loop);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 格式化数字,给数字加上千位分隔符。
|
|
55
|
+
* @param {number} number - 要格式化的数字。
|
|
56
|
+
* @returns {string} - 格式化后的字符串。
|
|
57
|
+
*/
|
|
58
|
+
export function _FormatNumber(number) {
|
|
59
|
+
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 纯数字转 数字加单位
|
|
64
|
+
* @param number 数字或字符串数字
|
|
65
|
+
* @param config : {
|
|
66
|
+
* join 拼接起来吗
|
|
67
|
+
* suffix 后缀
|
|
68
|
+
* integer 不超过万位的数字时保持整数吗
|
|
69
|
+
* }
|
|
70
|
+
* @returns 123456 --> 12.34万 | [ 12.34 , 万 ]
|
|
71
|
+
*/
|
|
72
|
+
export function _FormatNumberWithUnit(number, config = {}) {
|
|
73
|
+
const { join, suffix, integer } = Object.assign(
|
|
74
|
+
{
|
|
75
|
+
join: true,
|
|
76
|
+
suffix: "",
|
|
77
|
+
integer: false,
|
|
78
|
+
},
|
|
79
|
+
config
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
function _join(value, suffix, plus = true) {
|
|
83
|
+
value = (plus ? "" : "-") + value;
|
|
84
|
+
if (join) return value + suffix;
|
|
85
|
+
else return [value, suffix];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (typeof number == "string") {
|
|
89
|
+
if (!/^\d+$/.test(number.trim())) {
|
|
90
|
+
console.error("错误输入:", number);
|
|
91
|
+
return _join(0, suffix);
|
|
92
|
+
}
|
|
93
|
+
} else if (typeof number != "number") {
|
|
94
|
+
console.error("错误输入:", number);
|
|
95
|
+
return _join(0, suffix);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (Math.abs(number) == Infinity || number == 0) {
|
|
99
|
+
return _join(0, suffix);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
number = Number(number);
|
|
103
|
+
const plus = number >= 0;
|
|
104
|
+
number = Math.abs(number);
|
|
105
|
+
|
|
106
|
+
const units = [
|
|
107
|
+
"",
|
|
108
|
+
"万",
|
|
109
|
+
"亿",
|
|
110
|
+
"兆",
|
|
111
|
+
"京",
|
|
112
|
+
"垓",
|
|
113
|
+
"秭",
|
|
114
|
+
"穰",
|
|
115
|
+
"沟",
|
|
116
|
+
"涧",
|
|
117
|
+
"正",
|
|
118
|
+
"载",
|
|
119
|
+
"极",
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
/** 计算位数 */
|
|
123
|
+
const digits = Math.floor(Math.log10(number) / 4);
|
|
124
|
+
|
|
125
|
+
/** 不超过万位的数字直接返回 */
|
|
126
|
+
if (digits === 0) {
|
|
127
|
+
if (integer) {
|
|
128
|
+
return _join(number, suffix, plus);
|
|
129
|
+
} else {
|
|
130
|
+
return _join(number.toFixed(2), suffix, plus);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const dividedNumber = number / Math.pow(10000, digits);
|
|
135
|
+
const formattedNumber = dividedNumber.toFixed(2);
|
|
136
|
+
|
|
137
|
+
return _join(formattedNumber, units[digits] + suffix, plus);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 判断点是否在多边形内
|
|
142
|
+
* @param point - 待检测的点,包含 x 和 y 坐标
|
|
143
|
+
* @param polygon - 多边形的点集,数组形式,每个点包含 x 和 y 坐标
|
|
144
|
+
* @returns boolean - 点是否在多边形内
|
|
145
|
+
*/
|
|
146
|
+
export function _IsPointInPolygon(point, polygon) {
|
|
147
|
+
let isInside = false;
|
|
148
|
+
|
|
149
|
+
const { x, y } = point;
|
|
150
|
+
const len = polygon.length;
|
|
151
|
+
|
|
152
|
+
for (let i = 0, j = len - 1; i < len; j = i++) {
|
|
153
|
+
const xi = polygon[i].x,
|
|
154
|
+
yi = polygon[i].y;
|
|
155
|
+
const xj = polygon[j].x,
|
|
156
|
+
yj = polygon[j].y;
|
|
157
|
+
|
|
158
|
+
const intersect =
|
|
159
|
+
yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
|
|
160
|
+
|
|
161
|
+
if (intersect) isInside = !isInside;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return isInside;
|
|
165
|
+
}
|