Utility classes

2019
class GridSquare
{
    public function __construct($corner_a, $corner_b)
    {
        $this->tl = xy([min($corner_a->x, $corner_b->x), min($corner_a->y, $corner_b->y)]);
        $this->tr = xy([max($corner_a->x, $corner_b->x), min($corner_a->y, $corner_b->y)]);
        $this->bl = xy([min($corner_a->x, $corner_b->x), max($corner_a->y, $corner_b->y)]);
        $this->br = xy([max($corner_a->x, $corner_b->x), max($corner_a->y, $corner_b->y)]);
    }

    /**
     * Get the width of the square
     *
     * @return int
     */
    public function width()
    {
        return $this->tr->x - $this->tl->x + 1;
    }

    /**
     * Get the height of the square
     *
     * @return int
     */
    public function height()
    {
        return $this->bl->y - $this->tl->y + 1;
    }

    /**
     * Get all the points in the square
     *
     * @return Set
     */
    public function getPoints()
    {
        return $this->getCorners();
        $points = [];

        for ($x = $this->tl->x; $x <= $this->tr->x; $x++) {
            for ($y = $this->tl->y; $y <= $this->bl->y; $y++) {
                $points[] = xy([$x, $y]);
            }
        }

        return $points;
    }

    /**
     * Get the corners of the square
     *
     * @return Set
     */
    public function getCorners()
    {
        return [
            "tl" => $this->tl,
            "tr" => $this->tr,
            "bl" => $this->bl,
            "br" => $this->br,
        ];
    }

    /**
     * Rotate the square 90 degrees clockwise
     *
     * @return self
     */
    public function rotate()
    {
        return $this;

        // Must have a true center : extend the square if needed
        if (!($this->width() % 2)) {
            $this->tr->x++;
            $this->br->x++;
        }
        if (!($this->height() % 2)) {
            $this->bl->y++;
            $this->br->y++;
        }

        $this->center = xy([
            $this->tl->x + floor($this->width() / 2),
            $this->tl->y + floor($this->height() / 2)
        ]);

        $w = floor($this->width() / 2);
        $h = floor($this->height() / 2);

        // /* #LOG# */\Syltaen\Log::json(
        //     $this->center, [$w, $h],
        //     $this->tl,
        //     xy([$this->center->x - $h, $this->center->y - $w])
        // );

        $this->tl = xy([$this->center->x - $h, $this->center->y - $w]);
        $this->tr = xy([$this->center->x - $h, $this->center->y + $w]);
        $this->bl = xy([$this->center->x + $h, $this->center->y - $w]);
        $this->br = xy([$this->center->x + $h, $this->center->y + $w]);

        return new self($this->tl, $this->br);


        return $this;

    }
}