给定数组中的开始和结束坐标,标记数组槽以绘制矩形

我有一个10x10的数组,我知道矩形的起点和终点在哪里。矩形可以是 1x3、2x2、3x2 等。


给定开始和结束坐标,我需要标记矩形在 10x10 数组中的位置。


示例一(正确):


左上角节点:[0, 1]


右下节点:[1,2]


|--0--| |--x--| |--x--| |--0--|

|--0--| |--x--| |--x--| |--0--|

|--0--| |--0--| |--0--| |--0--|

|--0--| |--0--| |--0--| |--0--|

示例二(不正确):


左上角节点:[1,3]


右下节点:[3,3]


电流输出:


|--0--| |--0--| |--0--| |--0--|

|--0--| |--0--| |--0--| |--x--|

|--0--| |--0--| |--0--| |--0--|

|--0--| |--0--| |--0--| |--0--|

所需输出:


|--0--| |--0--| |--0--| |--0--|

|--0--| |--0--| |--0--| |--x--|

|--0--| |--0--| |--0--| |--x--|

|--0--| |--0--| |--0--| |--x--|

这是我尝试过的:


$width = $lowerRightNode[1] - $upperLeftNode[1] + 1;

echo 'width: ' . $width .'<br/>';

for($i=$upperLeftNode[0]; $i < $upperLeftNode[0] + $width; $i++) {

    for($z=$upperLeftNode[1]; $z <= $lowerRightNode[1]; $z++) {

        $this->_grid[$i][$z] = 'x';

        echo $i . ' ' . $z .'<br/>';

    }

}


慕工程0101907
浏览 182回答 2
2回答

长风秋雁

所以这真的像一个充洪问题。您有一个固定的 2D 格网作为数组,并且您希望根据 X/Y 坐标填充特定区域。我简化了下面的实现,以演示如何完成此操作。const GRID_WIDTH&nbsp; = 10;const GRID_HEIGHT = 10;// Coordinates are described as [[y1, x1], [y2, x2]]$coordinates = [[1,3], [3,3]];// Create the grid$grid = array_fill(0, GRID_WIDTH, array_fill(0, GRID_HEIGHT, '-'));// Plot the coordinates on the gridfor ($y = 0; $y < GRID_WIDTH; $y++) { // plot Y&nbsp; &nbsp; for ($x = 0; $x < GRID_HEIGHT; $x++) { // plot X&nbsp; &nbsp; &nbsp; &nbsp; if ($y >= $coordinates[0][0] && $y <= $coordinates[0][1]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($x >= $coordinates[1][0] && $x <= $coordinates[1][1]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $grid[$y][$x] = 'X';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}// Print the gridforeach ($grid as $x => $y) {&nbsp; &nbsp; foreach ($y as $plot) {&nbsp; &nbsp; &nbsp; &nbsp; echo " $plot ";&nbsp; &nbsp; }&nbsp; &nbsp; echo "\n";}输出应如下所示:&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; X&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; X&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; X&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;在你的第一个例子中,它看起来像这样:[0,1][1,2]&nbsp;-&nbsp; X&nbsp; X&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; X&nbsp; X&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;&nbsp;-&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp; -&nbsp;这里的关键是确保当我们扫描网格(从X到Y)时,X1,Y1,X2和Y2的坐标在光标的范围内。这允许我们一次绘制一个单元格,只要我们在这些边界内(包括)。这是通过检查 和 来完成的。由于计算坐标的宽度是不够的(这就是上面的实现正在做的事情)。因为它不标识高度边界。if ($y >= $coordinates[0][0] && $y <= $coordinates[0][1])if ($x >= $coordinates[1][0] && $x <= $coordinates[1][1])

桃花长相依

您可以使用已有的代码(尽管不需要计算)迭代要绘制的矩形:$width$rects = array(array(array(1, 3), array(3, 3)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(array(0, 1), array(1, 2))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );$grid = array_fill(0, 10, array_fill(0, 10, '0'));foreach ($rects as $r) {&nbsp; &nbsp; list($upperLeftNode, $lowerRightNode) = $r;&nbsp; &nbsp; for($x = $upperLeftNode[0]; $x <= $lowerRightNode[0]; $x++) {&nbsp; &nbsp; &nbsp; &nbsp; for($y = $upperLeftNode[1]; $y <= $lowerRightNode[1]; $y++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $grid[$x][$y] = 'x';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}foreach ($grid as $row) {&nbsp; &nbsp; echo implode(' ', $row) . "\n";}输出:0 x x 0 0 0 0 0 0 00 x x x 0 0 0 0 0 00 0 0 x 0 0 0 0 0 00 0 0 x 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0
打开App,查看更多内容
随时随地看视频慕课网APP