简体   繁体   English

在地图中生成区域的随机点?

[英]Generating random points in the map for a region?

I have to generate random point/marker in the map which should not outside of specific country. 我必须在地图中生成随机点/标记,而该点/标记不应位于特定国家/地区之外。

For canvas, I found following example 对于画布,我发现以下示例

http://jsfiddle.net/6cFfU/3/ http://jsfiddle.net/6cFfU/3/

var  markerPositions = [[225,175], [75,275], [150,225], [400,125], [300,300]];

var svgNS = "http://www.w3.org/2000/svg";
var xlinkNS = "http://www.w3.org/1999/xlink";

for (var i=0; i<markerPositions.length; i++) {
    // Create an SVG <use> element
    var  use = document.createElementNS(svgNS, "use");
    // Point it at our pin marker (the circle)
    use.setAttributeNS(xlinkNS, "href", "#pin");
    // Set it's x and y
    use.setAttribute("x", markerPositions[i][0]);
    use.setAttribute("y", markerPositions[i][1]);
    // Add it to the "markers" group
    document.getElementById("markers").appendChild(use);
}

It adds marker but I have to generate random marker and check it should not be outside of the shape/region. 它添加了标记,但是我必须生成随机标记,并检查它是否不应在形状/区域之外。

Is there any way to detect whether the random point is outside? 有什么方法可以检测随机点是否在外面? As the above sample has simple shapes but map will have different corners/polygons? 由于上述示例具有简单的形状,但地图将具有不同的角/多边形?

First, you calculate the bounding box rectangle. 首先,您计算边界框矩形。
Then you generate random points within that rectangle. 然后,您在该矩形内生成随机点。

x ==>  rand(x_min,x_max)
y ==> rand (y_min, y_max)

Then you check if the point is in the polygon, and if yes add. 然后检查该点是否在多边形中,如果是,则添加。
While not numMatchingPoints < numPointsMustHave ==> repeat 虽然不是numMatchingPoints <numPointsMustHave ==>重复

You can check if a point is in a non-overlapping n-Vertex-Polygon like this (C-Code): 您可以像这样(C代码)检查点是否在不重叠的n顶点多边形中:

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
  int i, j, c = 0;
  for (i = 0, j = nvert-1; i < nvert; j = i++) {
    if ( ((verty[i]>testy) != (verty[j]>testy)) &&
     (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
       c = !c;
  }
  return c;
}

This is called a ray-casting algorithm. 这称为射线投射算法。
This algorithm ray-casts to the right. 该算法向右传播。
In each iteration, it tests the test point against one of the polygon's edges. 在每次迭代中,它都针对多边形的一条边线测试测试点。

The first if-statement succeeds if the point's y-coord is within the edge's scope. 如果点的y坐标在边的范围内,则第一个if语句成功。
The second condition checks whether the test point is to the left of the line 第二个条件检查测试点是否在行的左侧

If both are true, then the line drawn rightwards from the test point crosses that edge. 如果两个都成立,则从测试点向右绘制的线会越过该边缘。

By repeatedly inverting the value of c, the algorithm counts how many times the rightward line crosses the polygon. 通过反复反转c的值,该算法计算出右线穿过多边形的次数。

If it crosses an odd number of times, then the point is inside the polygon; 如果交叉的次数是奇数,则该点位于多边形内部;
if it crosses an even number of times, the point is outside the polygon. 如果它交叉偶数次,则该点在多边形之外。

See also: http://en.wikipedia.org/wiki/Point_in_polygon 另请参阅: http : //en.wikipedia.org/wiki/Point_in_polygon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM