在圆内随机生成坐标


Random generating coordinates within a circle

场景
我正在尝试生成500到1000个随机坐标(lat,long),该坐标位于圆心所在的半径为1公里的圆内(5.418680100372829)。我试图用php进行编码,但没有成功,因为我不知道我应该为$radius提供什么值。

$radius = ?;
$origin_x = 5.420525;
$origin_y = 100.319500;
$angle = deg2rad(mt_rand(0, 359));
$pointRadius = mt_rand(0, $radius);
$point[] = array(
    'x' => $origin_x + ($pointRadius * cos($angle)),
    'y' => $origin_y + ($pointRadius * sin($angle))
);

我想到了另一种方法。我不想在圆内生成点,而是想在正方形边界内生成点并应用Haversine大圆距离公式来确定随机生成的点是否位于半径为1KM的圆内。

注意:生成的点可以相互重叠。

请告知,我需要大致的想法,我应该采取什么方法。提前谢谢。

在圆的边界内创建随机点:

var bounds = circle.getBounds();
map.fitBounds(bounds);
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();    
for (var i = 0; i < 100; i++) {
   // create a random point inside the bounds
   var ptLat = Math.random() * (ne.lat() - sw.lat()) + sw.lat();
   var ptLng = Math.random() * (ne.lng() - sw.lng()) + sw.lng();
   var point = new google.maps.LatLng(ptLat,ptLng);

如果它们在圆圈内,则保留它们(在这种情况下,将它们添加到地图中),否则丢弃它们:

   if (google.maps.geometry.spherical.computeDistanceBetween(point,circle.getCenter()) < circle.getRadius()) {
     createMarker(map, point,"marker "+i);
     // break;  if only need one point
   } // else nothing.

使用Google Maps Javascript API v3:的示例

var circle;
var infowindow = new google.maps.InfoWindow({});
function initialize() {
  var map = new google.maps.Map(document.getElementById("map"), {
    zoom: 4,
    center: new google.maps.LatLng(22.7964, 79.8456),
    mapTypeId: google.maps.MapTypeId.HYBRID
  });
  circle = new google.maps.Circle({
    center: map.getCenter(),
    radius: 1000, // meters
    strokeColor: "#0000FF",
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#0000FF",
    fillOpacity: 0.26
  });
  circle.setMap(map);
  var bounds = circle.getBounds();
  map.fitBounds(bounds);
  var sw = bounds.getSouthWest();
  var ne = bounds.getNorthEast();
  for (var i = 0; i < 100; i++) {
    var ptLat = Math.random() * (ne.lat() - sw.lat()) + sw.lat();
    var ptLng = Math.random() * (ne.lng() - sw.lng()) + sw.lng();
    var point = new google.maps.LatLng(ptLat, ptLng);
    if (google.maps.geometry.spherical.computeDistanceBetween(point, circle.getCenter()) < circle.getRadius()) {
      createMarker(map, point, "marker " + i);
      // break;
    }
  }
}
function createMarker(map, point, content) {
  var marker = new google.maps.Marker({
    position: point,
    map: map
  });
  google.maps.event.addListener(marker, "click", function(evt) {
    infowindow.setContent(content + "<br>" + marker.getPosition().toUrlValue(6));
    infowindow.open(map, marker);
  });
  return marker;
}
google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="map" style="width: 530px; height: 500px">
</div>

小提琴

我会这样做:

  1. 从区间[0,1]一致地选择两个独立的xy坐标
  2. 如果x²+y平方>1,则点位于圆外。放弃该样本,然后重试。不将正方形转换为圆形可确保均匀分布
  3. 将圆中的坐标转换为球体上的纬度/经度。如果你想保持均匀分布,你可以在这里使用一个区域保持地图投影。但由于半径远小于地球半径,这无关紧要,所以你可以使用一些更简单的投影,除非你必须为均匀分布提供有力的保证

我想可能有一些方法可以避免步骤2中的丢弃,但这可能会使way变得更加复杂,所以对于实际应用,我会坚持这样做。