1+ option = {
2+ xAxis : { } ,
3+ yAxis : { } ,
4+ series : [
5+ {
6+ symbolSize : 5 ,
7+ data : randomSwipe ( 0 , 0 , 100 , 100 ) ,
8+ // data: g(),
9+ type : 'scatter'
10+ }
11+ ]
12+ } ;
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+ function random ( min , max ) {
23+ return Math . floor ( Math . random ( ) * ( max - min + 1 ) + min ) ;
24+ }
25+
26+
27+ function bezierCreate ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ) {
28+ //构建参数
29+ var h = 100 ;
30+ var cp = [
31+ { x : x1 , y : y1 + h } ,
32+ { x : x2 , y : y2 + h } ,
33+ { x : x3 , y : y3 + h } ,
34+ { x : x4 , y : y4 + h } ,
35+ ] ;
36+ var numberOfPoints = 100 ;
37+ var curve = [ ] ;
38+ var dt = 1.0 / ( numberOfPoints - 1 ) ;
39+
40+ //计算轨迹
41+ for ( var i = 0 ; i < numberOfPoints ; i ++ ) {
42+ var ax , bx , cx ;
43+ var ay , by , cy ;
44+ var tSquared , tCubed ;
45+ var result_x , result_y ;
46+
47+ cx = 3.0 * ( cp [ 1 ] . x - cp [ 0 ] . x ) ;
48+ bx = 3.0 * ( cp [ 2 ] . x - cp [ 1 ] . x ) - cx ;
49+ ax = cp [ 3 ] . x - cp [ 0 ] . x - cx - bx ;
50+ cy = 3.0 * ( cp [ 1 ] . y - cp [ 0 ] . y ) ;
51+ by = 3.0 * ( cp [ 2 ] . y - cp [ 1 ] . y ) - cy ;
52+ ay = cp [ 3 ] . y - cp [ 0 ] . y - cy - by ;
53+
54+ var t = dt * i ;
55+ tSquared = t * t ;
56+ tCubed = tSquared * t ;
57+ result_x = ax * tCubed + bx * tSquared + cx * t + cp [ 0 ] . x ;
58+ result_y = ay * tCubed + by * tSquared + cy * t + cp [ 0 ] . y ;
59+ curve [ i ] = {
60+ x : result_x ,
61+ y : result_y ,
62+ } ;
63+ }
64+
65+ //轨迹转路数组
66+ var array = [ ] ;
67+ for ( var i = 0 ; i < curve . length ; i ++ ) {
68+ try {
69+ var j = i < 100 ? i : 199 - i ;
70+ xx = parseInt ( curve [ j ] . x ) ;
71+ yy = parseInt ( Math . abs ( 100 - curve [ j ] . y ) ) ;
72+ } catch ( e ) {
73+ break ;
74+ }
75+ array . push ( [ xx , yy ] ) ;
76+ }
77+
78+ return array ;
79+ }
80+
81+ /**
82+ * 真人模拟滑动函数
83+ *
84+ * 传入值:起点终点坐标
85+ * 效果:模拟真人滑动
86+ */
87+ function randomSwipe ( sx , sy , ex , ey ) {
88+ // debugger;
89+ //设置随机滑动时长范围
90+ var timeMin = 500 ;
91+ var timeMax = 1500 ;
92+ //设置控制点极限距离
93+ var leaveHeightLength = 500 ;
94+
95+ const [ p1 , p2 ] = generateRandomPoints ( [ sx , sy ] , [ ex , ey ] ) ;
96+ const x2 = p1 [ 0 ] ;
97+ const y2 = p1 [ 1 ] ;
98+ const x3 = p2 [ 0 ] ;
99+ const y3 = p2 [ 1 ] ;
100+ //获取运行轨迹,及参数
101+ var time = [ 0 , random ( timeMin , timeMax ) ] ;
102+ var track = bezierCreate ( sx , sy , x2 , y2 , x3 , y3 , ex , ey ) ;
103+
104+ console . log ( "随机控制点A坐标:" + x2 + "," + y2 ) ;
105+ console . log ( "随机控制点B坐标:" + x3 + "," + y3 ) ;
106+ console . log ( "随机滑动时长:" + time [ 1 ] ) ;
107+
108+
109+ // console.log(track);
110+ return [ ...track , [ x2 , y2 ] , [ x3 , y3 ] ]
111+
112+ //滑动
113+ // gestures(time.concat(track));
114+ // console.hide();
115+ }
116+
117+
118+ function getRandomPoint ( q1 , q2 , tMin , tMax ) {
119+ // 计算从 q1 到 q2 的向量
120+ const vector = [ q2 [ 0 ] - q1 [ 0 ] , q2 [ 1 ] - q1 [ 1 ] ] ;
121+
122+ // 生成一个随机的 t 值,确保点在 q1 和 q2 的线段内
123+ const t = Math . random ( ) * ( tMax - tMin ) + tMin ;
124+
125+ // 计算随机点的坐标
126+ const px = q1 [ 0 ] + t * vector [ 0 ] ;
127+ const py = q1 [ 1 ] + t * vector [ 1 ] ;
128+
129+ // 生成一个随机的偏移量,确保点不在连线上
130+ const offset = Math . random ( ) * 0.4 - 0.2 ;
131+
132+ // 计算垂直向量
133+ const perpendicularVector = [ - vector [ 1 ] , vector [ 0 ] ] ;
134+
135+ // 应用偏移量
136+ const finalPx = px + offset * perpendicularVector [ 0 ] ;
137+ const finalPy = py + offset * perpendicularVector [ 1 ] ;
138+
139+ return [ finalPx , finalPy ] ;
140+ }
141+
142+ function isOnSameSide ( p1 , p2 , q1 , q2 ) {
143+ // 计算向量 q1q2
144+ const q1q2 = [ q2 [ 0 ] - q1 [ 0 ] , q2 [ 1 ] - q1 [ 1 ] ] ;
145+
146+ // 计算向量 q1p1 和 q1p2
147+ const q1p1 = [ p1 [ 0 ] - q1 [ 0 ] , p1 [ 1 ] - q1 [ 1 ] ] ;
148+ const q1p2 = [ p2 [ 0 ] - q1 [ 0 ] , p2 [ 1 ] - q1 [ 1 ] ] ;
149+
150+ // 计算叉积
151+ const crossProduct1 = q1q2 [ 0 ] * q1p1 [ 1 ] - q1q2 [ 1 ] * q1p1 [ 0 ] ;
152+ const crossProduct2 = q1q2 [ 0 ] * q1p2 [ 1 ] - q1q2 [ 1 ] * q1p2 [ 0 ] ;
153+
154+ // 判断叉积是否同号
155+ return crossProduct1 * crossProduct2 > 0 ;
156+ }
157+
158+ function generateRandomPoints ( q1 , q2 ) {
159+ let p1 , p2 ;
160+ // let attempts = 0;
161+ // do {
162+ p1 = getRandomPoint ( q1 , q2 , 0.0005 , 0.2 ) ; // p1 更靠近 q1
163+ p2 = getRandomPoint ( q1 , q2 , 0.8 , 0.9995 ) ; // p2 更靠近 q2
164+ // attempts++;
165+ // if (attempts > 1000) {
166+ // console.error("Too many attempts to generate points. Possible issue with logic.");
167+ // break;
168+ // }
169+ // } while (!isOnSameSide(p1, p2, q1, q2));
170+
171+ return [ p1 , p2 ] ;
172+ }
173+
174+ // randomSwipe(0,0, 200, 200)
0 commit comments