//生成圆上的点 参数 R 半径 const generateSpot = (radius: number, fineTune: number) => { const { random, sqrt, tan, pow } = Math; const angel = random() * 360; const symbol = random() > 0.5 ? 1 : -1; //随机决定正负 const randomFineTune = (random() - random()) * fineTune; //fineTune乘以 -1 到 1 ; const x = sqrt(pow(radius + randomFineTune, 2) / (pow(tan(angel), 2) + 1)) * symbol; const y = tan(angel) * x; return { x, y }; }; const configuration = [ { radius: 86, //位置半径 dotSize: 25, //元素的半径大小 fineTune: 9, //位置浮动, amount: 4 //数量 }, { radius: 149, dotSize: 15, fineTune: 15, amount: 6 } ]; export default class GetSpotPos { dotCoordinates: any setPos: Function constructor(setPos: Function) { this.dotCoordinates = []; //位置坐标集合 this.setPos = setPos; this.initDotEles(configuration); } initDotEles(param: any) { let index = 0 for (let i = 0; i < param.length; i++) { let { amount, radius, dotSize, fineTune } = param[i]; let rawX, rawY; while (amount > 0) { --amount; let { x, y } = generateSpot(radius, fineTune); rawX = x; rawY = y; while (!this.checkSpace(rawX, rawY, dotSize)) { //如果空间检测不通过,则继续生成点 let { x, y } = generateSpot(radius, fineTune); rawX = x; rawY = y; } this.setPos({ x: rawX + 178 - dotSize, y: rawY + 178 - dotSize, index }); index += 1; this.dotCoordinates.push({ x: rawX, y: rawY, r: dotSize }); } } } checkSpace(x: number, y: number, r: number) { //是否有小于20的 有则返回true ,检测不通过 // return true; if (r == 25 && y > 49) return false if (this.dotCoordinates.length == 0) return true; let permission = this.dotCoordinates.some((item: any, index: number) => { let distance = Math.sqrt( Math.pow(item.x - x, 2) + Math.pow(item.y - y, 2) ); return distance < r + item.r + 20; }); return !permission; } }