Skip to content

Instantly share code, notes, and snippets.

@mmenavas
Last active September 27, 2017 07:22
Show Gist options
  • Select an option

  • Save mmenavas/ff137a98e07fee017dd14afdad28074a to your computer and use it in GitHub Desktop.

Select an option

Save mmenavas/ff137a98e07fee017dd14afdad28074a to your computer and use it in GitHub Desktop.

Revisions

  1. mmenavas revised this gist Sep 27, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Rectangle.php
    Original 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;

    }

    }
  2. mmenavas revised this gist Sep 27, 2017. 1 changed file with 35 additions and 17 deletions.
    52 changes: 35 additions & 17 deletions Rectangle.php
    Original file line number Diff line number Diff line change
    @@ -3,21 +3,10 @@
    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;
    $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;
    }

    // First row
    // Top row
    $rectangle = $this->drawRow($width, true) . "\n";

    // Filling
    while ($height > 0) {
    $rectangle .= $this->drawRow($width) . "\n";
    $height--;
    }

    // Last row
    // 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--;
  3. mmenavas created this gist Sep 27, 2017.
    81 changes: 81 additions & 0 deletions Rectangle.php
    Original 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();