我需要在具有一定宽度和高度的矩形上平均分配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像素),所以剩下的几个点可以在末尾随机添加,就可以了。
我正在寻找类似的东西(准随机):
我设法做到这一点,如果有人希望做到这一点,这是如何做的:
首先计算矩形的总面积,然后计算每个点应使用的面积,然后计算其自己的pointWidth和pointHeight(长度),然后迭代以创建cols和row,这是一个示例。
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. }
希望这对外面的人有帮助。