@antv/g-math 0.1.6 → 0.1.9

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/src/cubic.ts ADDED
@@ -0,0 +1,186 @@
1
+ import { distance, isNumberEqual, getBBoxByArray, piMod } from './util';
2
+ import line from './line';
3
+ import { snapLength, nearestPoint } from './bezier';
4
+ import { Point } from './types';
5
+
6
+ function cubicAt(p0: number, p1: number, p2: number, p3: number, t: number) {
7
+ const onet = 1 - t; // t * t * t 的性能大概是 Math.pow(t, 3) 的三倍
8
+ return onet * onet * onet * p0 + 3 * p1 * t * onet * onet + 3 * p2 * t * t * onet + p3 * t * t * t;
9
+ }
10
+
11
+ function derivativeAt(p0: number, p1: number, p2: number, p3: number, t: number) {
12
+ const onet = 1 - t;
13
+ return 3 * (onet * onet * (p1 - p0) + 2 * onet * t * (p2 - p1) + t * t * (p3 - p2));
14
+ }
15
+
16
+ function extrema(p0: number, p1: number, p2: number, p3: number) {
17
+ const a = -3 * p0 + 9 * p1 - 9 * p2 + 3 * p3;
18
+ const b = 6 * p0 - 12 * p1 + 6 * p2;
19
+ const c = 3 * p1 - 3 * p0;
20
+ const extremas = [];
21
+ let t1: number;
22
+ let t2: number;
23
+ let discSqrt: number;
24
+
25
+ if (isNumberEqual(a, 0)) {
26
+ if (!isNumberEqual(b, 0)) {
27
+ t1 = -c / b;
28
+ if (t1 >= 0 && t1 <= 1) {
29
+ extremas.push(t1);
30
+ }
31
+ }
32
+ } else {
33
+ const disc = b * b - 4 * a * c;
34
+ if (isNumberEqual(disc, 0)) {
35
+ extremas.push(-b / (2 * a));
36
+ } else if (disc > 0) {
37
+ discSqrt = Math.sqrt(disc);
38
+ t1 = (-b + discSqrt) / (2 * a);
39
+ t2 = (-b - discSqrt) / (2 * a);
40
+ if (t1 >= 0 && t1 <= 1) {
41
+ extremas.push(t1);
42
+ }
43
+ if (t2 >= 0 && t2 <= 1) {
44
+ extremas.push(t2);
45
+ }
46
+ }
47
+ }
48
+ return extremas;
49
+ }
50
+
51
+ // 分割贝塞尔曲线
52
+ function divideCubic(
53
+ x1: number,
54
+ y1: number,
55
+ x2: number,
56
+ y2: number,
57
+ x3: number,
58
+ y3: number,
59
+ x4: number,
60
+ y4: number,
61
+ t: number
62
+ ) {
63
+ // 划分点
64
+ const xt = cubicAt(x1, x2, x3, x4, t);
65
+ const yt = cubicAt(y1, y2, y3, y4, t);
66
+ // 计算两点之间的差值点
67
+ const c1 = line.pointAt(x1, y1, x2, y2, t);
68
+ const c2 = line.pointAt(x2, y2, x3, y3, t);
69
+ const c3 = line.pointAt(x3, y3, x4, y4, t);
70
+ const c12 = line.pointAt(c1.x, c1.y, c2.x, c2.y, t);
71
+ const c23 = line.pointAt(c2.x, c2.y, c3.x, c3.y, t);
72
+ return [
73
+ [x1, y1, c1.x, c1.y, c12.x, c12.y, xt, yt],
74
+ [xt, yt, c23.x, c23.y, c3.x, c3.y, x4, y4],
75
+ ];
76
+ }
77
+
78
+ // 使用迭代法取贝塞尔曲线的长度,二阶和三阶分开写,更清晰和便于调试
79
+ function cubicLength(
80
+ x1: number,
81
+ y1: number,
82
+ x2: number,
83
+ y2: number,
84
+ x3: number,
85
+ y3: number,
86
+ x4: number,
87
+ y4: number,
88
+ iterationCount: number
89
+ ) {
90
+ if (iterationCount === 0) {
91
+ return snapLength([x1, x2, x3, x4], [y1, y2, y3, y4]);
92
+ }
93
+ const cubics = divideCubic(x1, y1, x2, y2, x3, y3, x4, y4, 0.5);
94
+ const left = cubics[0];
95
+ const right = cubics[1];
96
+ left.push(iterationCount - 1);
97
+ right.push(iterationCount - 1);
98
+ return cubicLength.apply(null, left) + cubicLength.apply(null, right);
99
+ }
100
+
101
+ export default {
102
+ extrema,
103
+ box(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number) {
104
+ const xArr = [x1, x4];
105
+ const yArr = [y1, y4];
106
+ const xExtrema = extrema(x1, x2, x3, x4);
107
+ const yExtrema = extrema(y1, y2, y3, y4);
108
+ for (let i = 0; i < xExtrema.length; i++) {
109
+ xArr.push(cubicAt(x1, x2, x3, x4, xExtrema[i]));
110
+ }
111
+ for (let i = 0; i < yExtrema.length; i++) {
112
+ yArr.push(cubicAt(y1, y2, y3, y4, yExtrema[i]));
113
+ }
114
+ return getBBoxByArray(xArr, yArr);
115
+ },
116
+ length(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number) {
117
+ // 迭代三次,划分成 8 段求长度
118
+ return cubicLength(x1, y1, x2, y2, x3, y3, x4, y4, 3);
119
+ },
120
+ nearestPoint(
121
+ x1: number,
122
+ y1: number,
123
+ x2: number,
124
+ y2: number,
125
+ x3: number,
126
+ y3: number,
127
+ x4: number,
128
+ y4: number,
129
+ x0: number,
130
+ y0: number,
131
+ length?: number
132
+ ) {
133
+ return nearestPoint([x1, x2, x3, x4], [y1, y2, y3, y4], x0, y0, cubicAt, length);
134
+ },
135
+ pointDistance(
136
+ x1: number,
137
+ y1: number,
138
+ x2: number,
139
+ y2: number,
140
+ x3: number,
141
+ y3: number,
142
+ x4: number,
143
+ y4: number,
144
+ x0: number,
145
+ y0: number,
146
+ length?: number
147
+ ) {
148
+ const point = this.nearestPoint(x1, y1, x2, y2, x3, y3, x4, y4, x0, y0, length);
149
+ return distance(point.x, point.y, x0, y0);
150
+ },
151
+ interpolationAt: cubicAt,
152
+ pointAt(
153
+ x1: number,
154
+ y1: number,
155
+ x2: number,
156
+ y2: number,
157
+ x3: number,
158
+ y3: number,
159
+ x4: number,
160
+ y4: number,
161
+ t: number
162
+ ): Point {
163
+ return {
164
+ x: cubicAt(x1, x2, x3, x4, t),
165
+ y: cubicAt(y1, y2, y3, y4, t),
166
+ };
167
+ },
168
+ divide(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number, t: number) {
169
+ return divideCubic(x1, y1, x2, y2, x3, y3, x4, y4, t);
170
+ },
171
+ tangentAngle(
172
+ x1: number,
173
+ y1: number,
174
+ x2: number,
175
+ y2: number,
176
+ x3: number,
177
+ y3: number,
178
+ x4: number,
179
+ y4: number,
180
+ t: number
181
+ ) {
182
+ const dx = derivativeAt(x1, x2, x3, x4, t);
183
+ const dy = derivativeAt(y1, y2, y3, y4, t);
184
+ return piMod(Math.atan2(dy, dx));
185
+ },
186
+ };
package/src/ellipse.ts ADDED
@@ -0,0 +1,154 @@
1
+ /**
2
+ * @fileoverview 椭圆的一些计算,
3
+ * - 周长计算参考:https://www.mathsisfun.com/geometry/ellipse-perimeter.html
4
+ * - 距离计算参考:https://wet-robots.ghost.io/simple-method-for-distance-to-ellipse/
5
+ * @author dxq613@gmail.com
6
+ */
7
+
8
+ import { distance, piMod } from './util';
9
+ import { Point, BBox } from './types';
10
+
11
+ function copysign(v1: number, v2: number) {
12
+ const absv = Math.abs(v1);
13
+ return v2 > 0 ? absv : absv * -1;
14
+ }
15
+
16
+ export default {
17
+ /**
18
+ * 包围盒计算
19
+ * @param {number} x 椭圆中心 x
20
+ * @param {number} y 椭圆中心 y
21
+ * @param {number} rx 椭圆 x 方向半径
22
+ * @param {number} ry 椭圆 y 方向半径
23
+ * @return {object} 包围盒
24
+ */
25
+ box(x: number, y: number, rx: number, ry: number): BBox {
26
+ return {
27
+ x: x - rx,
28
+ y: y - ry,
29
+ width: rx * 2,
30
+ height: ry * 2,
31
+ };
32
+ },
33
+ /**
34
+ * 计算周长,使用近似法
35
+ * @param {number} x 椭圆中心 x
36
+ * @param {number} y 椭圆中心 y
37
+ * @param {number} rx 椭圆 x 方向半径
38
+ * @param {number} ry 椭圆 y 方向半径
39
+ * @return {number} 椭圆周长
40
+ */
41
+ length(x: number, y: number, rx: number, ry: number) {
42
+ return Math.PI * (3 * (rx + ry) - Math.sqrt((3 * rx + ry) * (rx + 3 * ry)));
43
+ },
44
+ /**
45
+ * 距离椭圆最近的点
46
+ * @param {number} x 椭圆中心 x
47
+ * @param {number} y 椭圆中心 y
48
+ * @param {number} rx 椭圆 x 方向半径
49
+ * @param {number} ry 椭圆 y 方向半径
50
+ * @param {number} x0 指定的点 x
51
+ * @param {number} y0 指定的点 y
52
+ * @return {object} 椭圆上距离指定点最近的点
53
+ */
54
+ nearestPoint(x: number, y: number, rx: number, ry: number, x0: number, y0: number) {
55
+ const a = rx;
56
+ const b = ry;
57
+ // 假如椭圆半径为0则返回圆心
58
+ if (a === 0 || b === 0) {
59
+ return {
60
+ x,
61
+ y,
62
+ };
63
+ }
64
+ // 转换成 0, 0 为中心的椭圆计算
65
+ const relativeX = x0 - x;
66
+ const relativeY = y0 - y;
67
+ const px = Math.abs(relativeX);
68
+ const py = Math.abs(relativeY);
69
+ const squareA = a * a;
70
+ const squareB = b * b;
71
+ // const angle0 = Math.atan2(relativeY, relativeX);
72
+ let t = Math.PI / 4;
73
+ let nearestX: number; // 椭圆上的任一点
74
+ let nearestY: number;
75
+ // 迭代 4 次
76
+ for (let i = 0; i < 4; i++) {
77
+ nearestX = a * Math.cos(t);
78
+ nearestY = b * Math.sin(t);
79
+
80
+ const ex = ((squareA - squareB) * Math.cos(t) ** 3) / a;
81
+ const ey = ((squareB - squareA) * Math.sin(t) ** 3) / b;
82
+ const rx1 = nearestX - ex;
83
+ const ry1 = nearestY - ey;
84
+
85
+ const qx = px - ex;
86
+ const qy = py - ey;
87
+ const r = Math.hypot(ry1, rx1);
88
+ const q = Math.hypot(qy, qx);
89
+
90
+ const delta_c = r * Math.asin((rx1 * qy - ry1 * qx) / (r * q));
91
+ const delta_t = delta_c / Math.sqrt(squareA + squareB - nearestX * nearestX - nearestY * nearestY);
92
+
93
+ t += delta_t;
94
+ t = Math.min(Math.PI / 2, Math.max(0, t));
95
+ }
96
+
97
+ return {
98
+ x: x + copysign(nearestX, relativeX),
99
+ y: y + copysign(nearestY, relativeY),
100
+ };
101
+ },
102
+ /**
103
+ * 点到椭圆最近的距离
104
+ * @param {number} x 椭圆中心 x
105
+ * @param {number} y 椭圆中心 y
106
+ * @param {number} rx 椭圆 x 方向半径
107
+ * @param {number} ry 椭圆 y 方向半径
108
+ * @param {number} x0 指定的点 x
109
+ * @param {number} y0 指定的点 y
110
+ * @return {number} 点到椭圆的距离
111
+ */
112
+ pointDistance(x: number, y: number, rx: number, ry: number, x0: number, y0: number) {
113
+ const nearestPoint = this.nearestPoint(x, y, rx, ry, x0, y0);
114
+ return distance(nearestPoint.x, nearestPoint.y, x0, y0);
115
+ },
116
+ /**
117
+ * 根据比例获取点
118
+ * @param {number} x 椭圆中心 x
119
+ * @param {number} y 椭圆中心 y
120
+ * @param {number} rx 椭圆 x 方向半径
121
+ * @param {number} ry 椭圆 y 方向半径
122
+ * @param {number} t 指定比例,x轴方向为 0
123
+ * @return {object} 点
124
+ */
125
+ pointAt(x: number, y: number, rx: number, ry: number, t: number): Point {
126
+ const angle = 2 * Math.PI * t; // 按照角度进行计算,而不按照周长计算
127
+ return {
128
+ x: x + rx * Math.cos(angle),
129
+ y: y + ry * Math.sin(angle),
130
+ };
131
+ },
132
+ /**
133
+ * 根据比例计算切线角度
134
+ * @param {number} x 椭圆中心 x
135
+ * @param {number} y 椭圆中心 y
136
+ * @param {number} rx 椭圆 x 方向半径
137
+ * @param {number} ry 椭圆 y 方向半径
138
+ * @param {number} t 指定比例 0 - 1 之间,x轴方向为 0。在 0-1 范围之外是循环还是返回 null,还需要调整
139
+ * @return {number} 角度,在 0 - 2PI 之间
140
+ */
141
+ tangentAngle(x: number, y: number, rx: number, ry: number, t: number) {
142
+ const angle = 2 * Math.PI * t; // 按照角度进行计算,而不按照周长计算
143
+ // 直接使用 x,y 的导数计算, x' = -rx * sin(t); y' = ry * cos(t);
144
+ const tangentAngle = Math.atan2(ry * Math.cos(angle), -rx * Math.sin(angle));
145
+ // 也可以使用指定点的切线方程计算,成本有些高
146
+ // const point = this.pointAt(0, 0, rx, ry, t); // 椭圆的切线同椭圆的中心不相关
147
+ // let tangentAngle = -1 * Math.atan((ry * ry * point.x) / (rx * rx * point.y));
148
+ // if (angle >= 0 && angle <= Math.PI) {
149
+ // tangentAngle += Math.PI;
150
+ // }
151
+
152
+ return piMod(tangentAngle);
153
+ },
154
+ };
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ import Quad from './quadratic';
2
+ import Cubic from './cubic';
3
+ import Arc from './arc';
4
+ import Line from './line';
5
+ import Polygon from './polygon';
6
+ import Polyline from './polyline';
7
+ import * as Util from './util';
8
+
9
+ export { Quad, Cubic, Arc, Line, Polygon, Polyline, Util };
package/src/line.ts ADDED
@@ -0,0 +1,98 @@
1
+ import { distance, getBBoxByArray } from './util';
2
+ import * as vec2 from 'gl-matrix/vec2';
3
+ import { BBox, Point } from './types';
4
+
5
+ export default {
6
+ /**
7
+ * 计算线段的包围盒
8
+ * @param {number} x1 起始点 x
9
+ * @param {number} y1 起始点 y
10
+ * @param {number} x2 结束点 x
11
+ * @param {number} y2 结束点 y
12
+ * @return {object} 包围盒对象
13
+ */
14
+ box(x1: number, y1: number, x2: number, y2: number): BBox {
15
+ return getBBoxByArray([x1, x2], [y1, y2]);
16
+ },
17
+ /**
18
+ * 线段的长度
19
+ * @param {number} x1 起始点 x
20
+ * @param {number} y1 起始点 y
21
+ * @param {number} x2 结束点 x
22
+ * @param {number} y2 结束点 y
23
+ * @return {number} 距离
24
+ */
25
+ length(x1: number, y1: number, x2: number, y2: number) {
26
+ return distance(x1, y1, x2, y2);
27
+ },
28
+ /**
29
+ * 根据比例获取点
30
+ * @param {number} x1 起始点 x
31
+ * @param {number} y1 起始点 y
32
+ * @param {number} x2 结束点 x
33
+ * @param {number} y2 结束点 y
34
+ * @param {number} t 指定比例
35
+ * @return {object} 包含 x, y 的点
36
+ */
37
+ pointAt(x1: number, y1: number, x2: number, y2: number, t: number): Point {
38
+ return {
39
+ x: (1 - t) * x1 + t * x2,
40
+ y: (1 - t) * y1 + t * y2,
41
+ };
42
+ },
43
+ /**
44
+ * 点到线段的距离
45
+ * @param {number} x1 起始点 x
46
+ * @param {number} y1 起始点 y
47
+ * @param {number} x2 结束点 x
48
+ * @param {number} y2 结束点 y
49
+ * @param {number} x 测试点 x
50
+ * @param {number} y 测试点 y
51
+ * @return {number} 距离
52
+ */
53
+ pointDistance(x1: number, y1: number, x2: number, y2: number, x: number, y: number): number {
54
+ // 投影距离 x1, y1 的向量,假设 p, p1, p2 三个点,投影点为 a
55
+ // p1a = p1p.p1p2/|p1p2| * (p1p 的单位向量)
56
+ const cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
57
+ if (cross < 0) {
58
+ return distance(x1, y1, x, y);
59
+ }
60
+ const lengthSquare = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
61
+ if (cross > lengthSquare) {
62
+ return distance(x2, y2, x, y);
63
+ }
64
+ return this.pointToLine(x1, y1, x2, y2, x, y);
65
+ },
66
+ /**
67
+ * 点到直线的距离,而不是点到线段的距离
68
+ * @param {number} x1 起始点 x
69
+ * @param {number} y1 起始点 y
70
+ * @param {number} x2 结束点 x
71
+ * @param {number} y2 结束点 y
72
+ * @param {number} x 测试点 x
73
+ * @param {number} y 测试点 y
74
+ * @return {number} 距离
75
+ */
76
+ pointToLine(x1: number, y1: number, x2: number, y2: number, x: number, y: number) {
77
+ const d = [x2 - x1, y2 - y1];
78
+ // 如果端点相等,则判定点到点的距离
79
+ if (vec2.exactEquals(d, [0, 0])) {
80
+ return Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
81
+ }
82
+ const u = [-d[1], d[0]];
83
+ vec2.normalize(u, u);
84
+ const a = [x - x1, y - y1];
85
+ return Math.abs(vec2.dot(a, u));
86
+ },
87
+ /**
88
+ * 线段的角度
89
+ * @param {number} x1 起始点 x
90
+ * @param {number} y1 起始点 y
91
+ * @param {number} x2 结束点 x
92
+ * @param {number} y2 结束点 y
93
+ * @return {number} 导数
94
+ */
95
+ tangentAngle(x1: number, y1: number, x2: number, y2: number) {
96
+ return Math.atan2(y2 - y1, x2 - x1);
97
+ },
98
+ };
package/src/polygon.ts ADDED
@@ -0,0 +1,58 @@
1
+ import { pointAtSegments, angleAtSegments, distanceAtSegment, lengthOfSegment } from './segments';
2
+ import polyline from './polyline';
3
+ import { PointTuple } from './types';
4
+
5
+ function getAllPoints(points: PointTuple[]) {
6
+ const tmp = points.slice(0);
7
+ if (points.length) {
8
+ tmp.push(points[0]);
9
+ }
10
+ return tmp;
11
+ }
12
+
13
+ export default {
14
+ /**
15
+ * 计算多边形的包围盒
16
+ * @param {array} points 点的集合 [x,y] 的形式
17
+ * @return {object} 包围盒
18
+ */
19
+ box(points: PointTuple[]) {
20
+ return polyline.box(points);
21
+ },
22
+ /**
23
+ * 计算多边形的长度
24
+ * @param {array} points 点的集合 [x,y] 的形式
25
+ * @return {object} 多边形边的长度
26
+ */
27
+ length(points: PointTuple[]) {
28
+ return lengthOfSegment(getAllPoints(points));
29
+ },
30
+ /**
31
+ * 根据比例获取多边形的点
32
+ * @param {array} points 点的集合 [x,y] 的形式
33
+ * @param {number} t 在多边形的长度上的比例
34
+ * @return {object} 根据比例值计算出来的点
35
+ */
36
+ pointAt(points: PointTuple[], t: number) {
37
+ return pointAtSegments(getAllPoints(points), t);
38
+ },
39
+ /**
40
+ * 指定点到多边形的距离
41
+ * @param {array} points 点的集合 [x,y] 的形式
42
+ * @param {number} x 指定点的 x
43
+ * @param {number} y 指定点的 y
44
+ * @return {number} 点到多边形的距离
45
+ */
46
+ pointDistance(points: PointTuple[], x: number, y: number) {
47
+ return distanceAtSegment(getAllPoints(points), x, y);
48
+ },
49
+ /**
50
+ * 根据比例获取多边形的切线角度
51
+ * @param {array} points 点的集合 [x,y] 的形式
52
+ * @param {number} t 在多边形的长度上的比例
53
+ * @return {object} 根据比例值计算出来的角度
54
+ */
55
+ tangentAngle(points: PointTuple[], t: number) {
56
+ return angleAtSegments(getAllPoints(points), t);
57
+ },
58
+ };
@@ -0,0 +1,57 @@
1
+ import { pointAtSegments, angleAtSegments, distanceAtSegment, lengthOfSegment } from './segments';
2
+ import { getBBoxByArray } from './util';
3
+ import { PointTuple, BBox } from './types';
4
+
5
+ export default {
6
+ /**
7
+ * 计算多折线的包围盒
8
+ * @param {array} points 点的集合 [x,y] 的形式
9
+ * @return {object} 包围盒
10
+ */
11
+ box(points: PointTuple[]): BBox {
12
+ const xArr = [];
13
+ const yArr = [];
14
+ for (let i = 0; i < points.length; i++) {
15
+ const point = points[i];
16
+ xArr.push(point[0]);
17
+ yArr.push(point[1]);
18
+ }
19
+ return getBBoxByArray(xArr, yArr);
20
+ },
21
+ /**
22
+ * 计算多折线的长度
23
+ * @param {array} points 点的集合 [x,y] 的形式
24
+ * @return {object} 多条边的长度
25
+ */
26
+ length(points: PointTuple[]) {
27
+ return lengthOfSegment(points);
28
+ },
29
+ /**
30
+ * 根据比例获取多折线的点
31
+ * @param {array} points 点的集合 [x,y] 的形式
32
+ * @param {number} t 在多折线的长度上的比例
33
+ * @return {object} 根据比例值计算出来的点
34
+ */
35
+ pointAt(points: PointTuple[], t: number) {
36
+ return pointAtSegments(points, t);
37
+ },
38
+ /**
39
+ * 指定点到多折线的距离
40
+ * @param {array} points 点的集合 [x,y] 的形式
41
+ * @param {number} x 指定点的 x
42
+ * @param {number} y 指定点的 y
43
+ * @return {number} 点到多折线的距离
44
+ */
45
+ pointDistance(points: PointTuple[], x: number, y: number) {
46
+ return distanceAtSegment(points, x, y);
47
+ },
48
+ /**
49
+ * 根据比例获取多折线的切线角度
50
+ * @param {array} points 点的集合 [x,y] 的形式
51
+ * @param {number} t 在多折线的长度上的比例
52
+ * @return {object} 根据比例值计算出来的角度
53
+ */
54
+ tangentAngle(points: PointTuple[], t: number) {
55
+ return angleAtSegments(points, t);
56
+ },
57
+ };
@@ -0,0 +1,107 @@
1
+ import line from './line';
2
+ import { distance, isNumberEqual, getBBoxByArray, piMod } from './util';
3
+ import { nearestPoint } from './bezier';
4
+ import { Point } from './types';
5
+
6
+ // 差值公式
7
+ function quadraticAt(p0: number, p1: number, p2: number, t: number) {
8
+ const onet = 1 - t;
9
+ return onet * onet * p0 + 2 * t * onet * p1 + t * t * p2;
10
+ }
11
+
12
+ // 求极值
13
+ function extrema(p0: number, p1: number, p2: number) {
14
+ const a = p0 + p2 - 2 * p1;
15
+ if (isNumberEqual(a, 0)) {
16
+ return [0.5];
17
+ }
18
+ const rst = (p0 - p1) / a;
19
+ if (rst <= 1 && rst >= 0) {
20
+ return [rst];
21
+ }
22
+ return [];
23
+ }
24
+
25
+ function derivativeAt(p0: number, p1: number, p2: number, t: number) {
26
+ return 2 * (1 - t) * (p1 - p0) + 2 * t * (p2 - p1);
27
+ }
28
+
29
+ // 分割贝塞尔曲线
30
+ function divideQuadratic(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, t: number) {
31
+ // 划分点
32
+ const xt = quadraticAt(x1, x2, x3, t);
33
+ const yt = quadraticAt(y1, y2, y3, t);
34
+
35
+ // 分割的第一条曲线的控制点
36
+ const controlPoint1 = line.pointAt(x1, y1, x2, y2, t);
37
+ // 分割的第二条曲线的控制点
38
+ const controlPoint2 = line.pointAt(x2, y2, x3, y3, t);
39
+ return [
40
+ [x1, y1, controlPoint1.x, controlPoint1.y, xt, yt],
41
+ [xt, yt, controlPoint2.x, controlPoint2.y, x3, y3],
42
+ ];
43
+ }
44
+
45
+ // 使用迭代法取贝塞尔曲线的长度
46
+ function quadraticLength(
47
+ x1: number,
48
+ y1: number,
49
+ x2: number,
50
+ y2: number,
51
+ x3: number,
52
+ y3: number,
53
+ iterationCount: number
54
+ ) {
55
+ if (iterationCount === 0) {
56
+ return (distance(x1, y1, x2, y2) + distance(x2, y2, x3, y3) + distance(x1, y1, x3, y3)) / 2;
57
+ }
58
+ const quadratics = divideQuadratic(x1, y1, x2, y2, x3, y3, 0.5);
59
+ const left = quadratics[0];
60
+ const right = quadratics[1];
61
+ left.push(iterationCount - 1);
62
+ right.push(iterationCount - 1);
63
+ return quadraticLength.apply(null, left) + quadraticLength.apply(null, right);
64
+ }
65
+
66
+ export default {
67
+ box(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) {
68
+ const xExtrema = extrema(x1, x2, x3)[0];
69
+ const yExtrema = extrema(y1, y2, y3)[0];
70
+ // 控制点不加入 box 的计算
71
+ const xArr = [x1, x3];
72
+ const yArr = [y1, y3];
73
+ if (xExtrema !== undefined) {
74
+ xArr.push(quadraticAt(x1, x2, x3, xExtrema));
75
+ }
76
+ if (yExtrema !== undefined) {
77
+ yArr.push(quadraticAt(y1, y2, y3, yExtrema));
78
+ }
79
+ return getBBoxByArray(xArr, yArr);
80
+ },
81
+ length(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) {
82
+ return quadraticLength(x1, y1, x2, y2, x3, y3, 3);
83
+ },
84
+ nearestPoint(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x0: number, y0: number) {
85
+ return nearestPoint([x1, x2, x3], [y1, y2, y3], x0, y0, quadraticAt);
86
+ },
87
+ pointDistance(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x0: number, y0: number) {
88
+ const point = this.nearestPoint(x1, y1, x2, y2, x3, y3, x0, y0);
89
+ return distance(point.x, point.y, x0, y0);
90
+ },
91
+ interpolationAt: quadraticAt,
92
+ pointAt(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, t: number): Point {
93
+ return {
94
+ x: quadraticAt(x1, x2, x3, t),
95
+ y: quadraticAt(y1, y2, y3, t),
96
+ };
97
+ },
98
+ divide(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, t: number) {
99
+ return divideQuadratic(x1, y1, x2, y2, x3, y3, t);
100
+ },
101
+ tangentAngle(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, t: number) {
102
+ const dx = derivativeAt(x1, x2, x3, t);
103
+ const dy = derivativeAt(y1, y2, y3, t);
104
+ const angle = Math.atan2(dy, dx);
105
+ return piMod(angle);
106
+ },
107
+ };