/** * @param Canvas $canvas * @param array $args * @return Canvas */ public function draw(Canvas $canvas, array $args) { $this->checkArgumentsValid($args); $startRow = $args[0]; $startCol = $args[1]; if ($canvas->isPointOutOfBounds($startRow, $startCol)) { // If the starting point is out of bounds, then draw nothing return $canvas; } $endRow = $args[2]; $endCol = $args[3]; if ($canvas->isRowOutOfBounds($endRow)) { // If the ending row is out of bounds, draw until the canvas ends $endRow = $canvas->getWidth(); } if ($canvas->isColumnOutOfBounds($endCol)) { // If the ending column is out of bounds, draw until the canvas ends $endCol = $canvas->getHeight(); } // Draw for ($i = $startRow; $i <= $endRow; ++$i) { for ($j = $startCol; $j <= $endCol; ++$j) { $canvas->markPoint($i, $j); } } return $canvas; }
public function test_it_draws_requested_horizontal_line() { $canvas = new Canvas(5, 9); $initialContent = $canvas->getContent(); $args = [4, 2, 3]; $this->command->draw($canvas, $args); $this->assertNotEquals($initialContent, $canvas->getContent()); $expectedContent = $initialContent; for ($i = 2; $i <= 3; ++$i) { $expectedContent[$i][4] = Canvas::POINT_MARKED; } $this->assertEquals($expectedContent, $canvas->getContent()); }
public function test_it_draws_requested_vertical_line() { $canvas = new Canvas(5, 9); $initialContent = $canvas->getContent(); $args = [4, 2, 7]; $this->command->draw($canvas, $args); $this->assertNotEquals($initialContent, $canvas->getContent()); $expectedContent = $initialContent; for ($j = 2; $j <= 7; ++$j) { $expectedContent[4][$j] = Canvas::POINT_MARKED; } $this->assertEquals($expectedContent, $canvas->getContent()); }
public function test_it_draws_requested_rectangle() { $canvas = new Canvas(5, 9); $initialContent = $canvas->getContent(); $args = [2, 3, 4, 7]; $this->command->draw($canvas, $args); $this->assertNotEquals($initialContent, $canvas->getContent()); $expectedContent = $initialContent; for ($i = 2; $i <= 4; ++$i) { for ($j = 3; $j <= 7; ++$j) { $expectedContent[$i][$j] = Canvas::POINT_MARKED; } } $this->assertEquals($expectedContent, $canvas->getContent()); }
private function clearCanvas() { $this->canvas->clear(); }
/** * @dataProvider pointDataProvider */ public function test_it_correctly_verifies_whether_point_is_out_of_bounds($row, $column, $expected) { $canvas = new Canvas(3, 2); $result = $canvas->isPointOutOfBounds($row, $column); $this->assertEquals($expected, $result); }