Last active
September 27, 2017 07:22
-
-
Save mmenavas/ff137a98e07fee017dd14afdad28074a to your computer and use it in GitHub Desktop.
Revisions
-
mmenavas revised this gist
Sep 27, 2017 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,7 @@ class rectangle { public function __construct($size, $width = 0, $height = 0) { $sizes = $this->getSizes(); if (isset($sizes[$size])) { $this->width = $sizes[$size]['width']; @@ -13,9 +14,11 @@ public function __construct($size, $width = 0, $height = 0) { $this->width = $width; $this->height = $height; } } private function getSizes() { $sizes = [ 'small' => [ 'width' => 2, @@ -79,6 +82,7 @@ private function drawRow($width, $isSide = false) { // Last column $row .= $isSide ? "." : "|"; return $row; } } -
mmenavas revised this gist
Sep 27, 2017 . 1 changed file with 35 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,21 +3,10 @@ class rectangle { public function __construct($size, $width = 0, $height = 0) { $sizes = $this->getSizes(); if (isset($sizes[$size])) { $this->width = $sizes[$size]['width']; $this->height = $sizes[$size]['height']; } else { // Assume custom size @@ -26,7 +15,32 @@ public function __construct($size, $width = 0, $height = 0) { } } private function getSizes() { $sizes = [ 'small' => [ 'width' => 2, 'height' => 1, ], 'medium' => [ 'width' => 4, 'height' => 2, ], 'large' => [ 'width' => 10, 'height' => 3, ], 'x-large' => [ 'width' => 20, 'height' => 4, ] ]; return $sizes; } public function drawRectangle() { $width = $this->width; $height = $this->height; $rectangle = ""; @@ -35,24 +49,28 @@ public function drawRectangle() { return $rectangle; } // Top row $rectangle = $this->drawRow($width, true) . "\n"; // Filling while ($height > 0) { $rectangle .= $this->drawRow($width) . "\n"; $height--; } // Bottom row $rectangle .= $this->drawRow($width, true) . "\n"; return $rectangle; } private function drawRow($width, $isSide = false) { // First column $row = $isSide ? "." : "|"; // Filling while ($width > 0) { $row .= $isSide ? "-" : " "; $width--; -
mmenavas created this gist
Sep 27, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ <?php class rectangle { public function __construct($size, $width = 0, $height = 0) { if ($size == 'small') { $this->width = 2; $this->height = 1; } elseif ($size == 'medium') { $this->width = 4; $this->height = 2; } elseif ($size == 'large') { $this->width = 10; $this->height = 3; } elseif ($size == 'x-large') { $this->width = 20; $this->height = 4; } else { // Assume custom size $this->width = $width; $this->height = $height; } } public function drawRectangle() { $width = $this->width; $height = $this->height; $rectangle = ""; if ($width <= 0 && $height <= 0) { return $rectangle; } // First row $rectangle = $this->drawRow($width, true) . "\n"; while ($height > 0) { $rectangle .= $this->drawRow($width) . "\n"; $height--; } // Last row $rectangle .= $this->drawRow($width, true) . "\n"; return $rectangle; } private function drawRow($width, $isSide = false) { // First column $row = $isSide ? "." : "|"; while ($width > 0) { $row .= $isSide ? "-" : " "; $width--; } // Last column $row .= $isSide ? "." : "|"; return $row; } } $rect = new Rectangle('small'); print $rect->drawRectangle(); $rect = new Rectangle('medium'); print $rect->drawRectangle(); $rect = new Rectangle('large'); print $rect->drawRectangle(); $rect = new Rectangle('x-large'); print $rect->drawRectangle(); $rect = new Rectangle('custom', 40, 6); print $rect->drawRectangle();