dongshushi5579 2014-01-13 17:31
浏览 19
已采纳

如何以圆形形式均匀地传播点

I've searched through a dosen of questions similar to this one, but none with the same problem I've came upon. I have a map with units, 1000x1000 units, think of it as pixels. The problem is that I have to uniformly spread circle-shaped into the 1000x1000 map and all I could come up with till now is this:

$quadrant = array_search(min($quadrants), $quadrants); // the quadrant with less points
$radius = (current_points_number / sqrt(pi() / $points_density);
$angle = pi() * mt_rand() / 2 / mt_getrandmax();
$x = round((($quadrant == 2 || $quadrant == 3) ? -1 : 1) * cos($angle) * $radius + 500);
$y = round((($quadrant == 3 || $quadrant == 4) ? -1 : 1) * sin($angle) * $radius + 500);

The result of this actual algorithm is , as you can see in the next image, a problem, since it tends to make points denser to the center of the circle and widely scattered on at it's margins.

enter image description here

Any suggestion would be highly appreciated.

  • 写回答

2条回答 默认 最新

  • dongpo2002 2014-01-13 17:35
    关注

    simple solution

    you can simply iterate through all map's pixels, and for each pixel inside of the given circle - create unit with probability P``, so the code is something like

    for x=1 to max_x
      for y=1 to max_y
        if (x-circle_x)^2 + (y-circle_y)^2 <= circle_r^2
          if random() < P
            map[x][y] = new unit()     
    

    Obviously it is not optimal, as you do not really need iteration through non-circle points, but this should give you the general idea. It is easy to prove, that it generates the uniform distribution, as it is simply generation of the uniform distribution on the whole map and "removing" units from outside of the circle.

    more mathematical solution

    You can also do it in more strict way, by applying iteratively the uniformly distributed points generator:

    for i in 1...numer_of_units_to_generate:
      t = 2*pi*random()
      u = random()+random()
      if u>1 then 
        r=2-u 
      else 
        r=u
      map[r*cos(t)][r*sin(t)]=new unit()
    

    result:

    uniformly distributed points on the circle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站