我正在尝试根据计算的加密密钥构建图像。这纯粹是为了让我训练自己在PHP中变得更好。图像应该类似于Telegram加密。我目前有以下代码来计算每种颜色的方块数。我不知道的是,如何从这里开始用这些彩色方块填满桌子。我可以使用rand生成坐标,但如何排除已经填充的方块?
PHP
//number of squares with a certain color
$white = rand (0, 50);
$lightblue = rand (0, 200);
$whiteblue = $white + $lightblue;
$darkblue = rand ($whiteblue, 750);
$total = '1024';
$mediumblue = $total - ($darkblue + $whiteblue);
$totalsquares = $white + $lightblue + $mediumblue + $darkblue;
//number of letters in white squares
$letters = rand(0, $white);
答案 0 :(得分:0)
我已经修改了一些代码,使其更易于管理。基本上,它将适当数量的彩色方块添加到数组中,然后对该数组进行混洗。要绘制正方形,只需遍历该数组即可。
$squares = array();
$total = 1024;
$squares['white'] = rand (0, 50);
$squares['lightblue'] = rand (0, 200);
$whiteblue = $squares['white'] + $squares['lightblue'];
$squares['darkblue'] = rand ($whiteblue, 750);
$squares['mediumblue'] = $total - ($squares['darkblue'] + $whiteblue);
//number of letters in white squares
$squares['whiteletters'] = rand(0, $squares['white']);
$squares['white'] -= $squares['whiteletters'];
// Create array of squares
$items = array();
foreach ($squares as $key => $count) {
$items = array_merge($items, array_fill(0, $count, $key));
}
shuffle($items);
// Draw squares
$size = sqrt($total);
// <table>
for ($y = 0; $y < $size; $y++) {
// <tr>
for ($x = 0; $x < $size; $x++) {
$square = $items[$y * $size + $x];
// <td>
// draw $square
// </td>
}
// </tr>
}
// </table>