【问题标题】:Equally distrubute N points on a rectangle在一个矩形上均匀分布 N 个点
【发布时间】:2012-05-21 17:01:45
【问题描述】:

我需要在一个具有一定宽度和高度的矩形上平均分布N个点。

例如。给定一个 10x10 的盒子和 100 个点,这些点将被设置为:

(1,1)  (1,2)  (1,3)  (1,4)  (1,5)  (1,6)  (1,7)  (1,8)  (1,9)  (1,10)
(2,1)  (2,2)  (2,3)  (2,4)  (2,5)  (2,6)  (2,7)  (2,8)  (2,9)  (2,10)
(3,1)  (3,2)  (3,3)  (3,4)  (3,5)  (3,6)  (3,7)  (3,8)  (3,9)  (3,10)
...
...

我如何对任何 N 个点、宽度和高度的组合进行概括?

注意:它不需要完美,但很接近,无论如何我都会随机化一点(从 X 和 Y 轴上的“起点”移动点 +/- x 像素),所以在最后随机添加几个点就可以了。

我正在寻找这样的东西(准随机):

【问题讨论】:

  • 我忘了说我正在为生成 Voronoi 图/Delaunay 三角剖分的 PHP 脚本执行此操作。我需要一种更快的方法来沿图像分布点,而不必计算图表然后必须多次进行劳埃德松弛。

标签: algorithm height width equals points


【解决方案1】:

我设法做到了,如果其他人想要做到这一点,方法如下:

首先计算矩形的总面积,然后计算每个点应该使用的面积,然后计算自己的pointWidth和pointHeight(长度),然后迭代制作cols和rows,这里是一个例子。

PHP 代码:

$width = 800;
$height = 300;
$nPoints = 50;

$totalArea = $width*$height;
$pointArea = $totalArea/$nPoints;
$length = sqrt($pointArea);

$im = imagecreatetruecolor($width,$height);
$red = imagecolorallocate($im,255,0,0);

for($i=$length/2; $i<$width; $i+=$length)
{
    for($j=$length/2; $j<$height; $j+=$length)
    {
        imageellipse($im,$i,$j,5,5,$im,$red);
    }
}

我还需要稍微随机化点的位置,我把它放在第二个“for”而不是上面的代码中。

{
    $x = $i+((rand(0,$length)-$length/2)*$rand);
    $y = $j+((rand(0,$length)-$length/2)*$rand);
    imageellipse($im,$x,$y,5,5,$im,$red);

    // $rand is a parameter of the function, which can take a value higher than 0 when using something like 0.001 the points are "NOT RANDOM", while a value of 1 makes the distribution of the points look random but well distributed, high values produced results unwanted for me, but might be useful for other applications.
}

希望这对那里的人有所帮助。

【讨论】:

    猜你喜欢
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多