* @description 生成随机路径 * @param start * @param end * @param steps * @returns
(start, end, steps)
| 2283 | * @returns |
| 2284 | */ |
| 2285 | function createRandomPath(start, end, steps) { |
| 2286 | // 最小水平增量 |
| 2287 | const minDeltaX = (end.x - start.x) / steps; |
| 2288 | // 最大垂直增量 |
| 2289 | const maxDeltaY = (end.y - start.y) / steps; |
| 2290 | const path = []; |
| 2291 | // 开始节点 |
| 2292 | path.push(start); |
| 2293 | // 插入点 |
| 2294 | for (let i = 0; i < steps; i++) { |
| 2295 | // 横坐标 |
| 2296 | const x = path[i].x + Math.random() * 5 + minDeltaX; |
| 2297 | // 纵坐标 |
| 2298 | const y = path[i].y + |
| 2299 | Math.random() * 5 * Math.pow(-1, ~~(Math.random() * 2 + 1)) + |
| 2300 | maxDeltaY; |
| 2301 | path.push({ |
| 2302 | x, |
| 2303 | y, |
| 2304 | }); |
| 2305 | } |
| 2306 | return path; |
| 2307 | } |
| 2308 | /** |
| 2309 | * @description 随机数字 |
| 2310 | * @returns |