给定圆心和半径在圆内随机画点

最近做智慧工地系统需要在地图做实时定位,需要对工人位置进行描绘。硬件会返回信标(蓝牙定位)的位置,由于无法获取工人在地图精确位置,

我这里暂时打算使用信标位置为圆心,距离最近信标的距离一半作为半径,去随机画点来对位置进行绘制。

前台采用地图采用leafletjs插件,https://leafletjs.com/。

下面是画点的算法:

r = R * sqrt(random())
theta = random() * 2 * PI

x = centerX + r * cos(theta)
y = centerY + r * sin(theta)
centerX 和centerY是圆心坐标,theta是生成的角度,R是需要画点的圆的半径,套用这个公式即可
    let x0 = targetX;
    let y0 = targetY;
    let r = radius * Math.sqrt(Math.random());
    let theta = Math.random() * 2 * Math.PI;
    targetX = x0 + r * Math.cos(theta);
    targetY = y0 + r * Math.sin(theta);

参考:https://*.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly/50746409#50746409