要求:
有若干个要素地图点位相同,需要在周围指定范围内展示所有点
实现原理:
利用随机数给中心点x、y增量
// 定义个随机数函数返回[-1,1]的值
function random(){
return ((Math.random() * 2) - 1);
}
- 点在周围矩形范围内
let count = 100; // 随机点数
let step = 0.0001; // 范围
let item = {
longitude : 110,
latitude : 30
};
while(count--){
let x = Number(item.longitude) + random() * step;
let y = Number(item.latitude) + random() * step;
console.log('随机点',count,"x:",x,"y:",y);
}
- 点在周围的圆环上
let count = 100; // 随机点数
let step = 0.0001; // 范围
let item = {
longitude : 110,
latitude : 30
};
let mean = 2 / count; // 点在圆环上间隔
let radin = -1;
while(count--){
let l = radin * Math.PI;
let x = Number(item.longitude) + step * Math.cos(l);
let y = Number(item.latitude) + step * Math.sin(l);
radin = radin + mean;
console.log('随机点',count,"x:",x,"y:",y);
}
- 点在周围圆范围内
let count = 100; // 随机点数
let step = 0.0001; // 范围
let item = {
longitude : 110,
latitude : 30
};
while(count--){
let isAgain = true;
while(isAgain){
x = random();
y = random();
const l = Math.pow(x,2) + Math.pow(y,2);
isAgain = l > 1; // 判断是否在圆内,true时重新生成随机数.
}
let l = radin * Math.PI;
x = Number(item.longitude) + step * x;
y = Number(item.latitude) + step * y;
console.log('随机点',count,"x:",x,"y:",y);
}
或
let count = 100; // 随机点数
let step = 0.0001; // 范围
let item = {
longitude : 110,
latitude : 30
};
while(count--){
// 随机弧度
let radin = random() * Math.PI;
let mean = Math.random();
let x = Number(item.longitude) + mean * Math.cos(radin) * step;
let y = Number(item.latitude) + mean * Math.sin(radin) * step;
console.log('随机点',count,"x:",x,"y:",y);
}