Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Object-oriented
- */
- class Matrix
- {
- private $matrix = [];
- private $n = 0;
- public function __construct($n)
- {
- $this->n = $n;
- $index = 1;
- for ($row = 1; $row <= $n; $row++) {
- $currRow = [];
- for ($col = 1; $col <= $n; $col++) {
- $currRow[] = $index;
- $index++;
- }
- $this->matrix[] = $currRow;
- }
- }
- public function rotate()
- {
- $result = [];
- for ($col = 0; $col < $this->n; $col++) {
- $currRow = [];
- for ($row = $this->n - 1; $row >= 0; $row--) {
- $currRow[] = $this->matrix[$row][$col];
- }
- $result[] = $currRow;
- }
- $this->matrix = $result;
- }
- public function render()
- {
- for ($row = 0; $row < $this->n; $row++) {
- for ($col = 0; $col < $this->n; $col++) {
- echo $this->matrix[$row][$col] . ' ';
- }
- echo '<br />';
- }
- }
- }
- // Initial matrix
- $matrix = new Matrix(3);
- $matrix->render();
- echo '<br/>';
- // One rotation
- $matrix->rotate();
- $matrix->render();
- echo '<br/>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement