1
votes

I have a matrix of [ n x m ] elements where center may not be on coordinate(x,y) 0,0:

<?php
$matrix_yx = array(
     21 => array(10,11,12),
     22 => array(10,11,12),
     23 => array(10,11,12),
     24 => array(10,11,12)
);
$origin_yx = array(23,11);
?>

I'm trying to write a function to pass as input

  1. the matrix
  2. pivot coordinate to be used as fulcrum
  3. eventually the rotation in degree (that can be only 90, 180 or 270)

So, considering a representation of matrix (x,y) elements:

y
^
| 10,24   11,24   12,24
| 10,23  >11,23<  12,23
| 10,22   11,22   12,22
| 10,21   11,21   12,21
+-----------------------> x

I need to rotate it of 90' degree clockwise around point (11,23) basically obtaining a new matrix like this:

y
^
| 09,24   10,24   11,24   12,24
| 09,23   10,23  >11,23<  12,23
| 09,22   10,22   11,22   12,23
+-------------------------------> x

I'm a little bit confused as if the (x,y) origin would be on coordinate (0,0) then it would be simple as

(x,y) = (11,23)

translate of 90' around (0) i would obtain

function Rotate($matrix) {
    list($x,y) = array($y,-$x);
    (x,y) = (23,-11);
}

but then what I have to consider if I want to pass a different pivot coordinate?

function Rotate($matrix, $pivot_x, $pivot_y) {
    //-- (...)
}

In case I would have to transpose the matrix of 180 or 270 degree (instead of 90) then function will be called 2 or 3 times... unless obviously there's a smarter approach (that I'm sure exists but - at the moment - not in my mind).

1
you dont need do worries about your pivote, translate your matrix to pivot (0,0) this mean if you have your point (x,y) = (11,23) and your pivote (x_p,y_p) = (1,1) your point in (0,0) = (x-x_p, y-y_p) and can apply your function. And about your called function, remeber you are in a circle, divide your grade with 90 and do mod 4, example 450:90 = 5 , 5 mod 4 = 1 => 1 time you need call your function and max time = 3, 3 mod 4 = 3Laurentiu

1 Answers

0
votes

Following the @Laurentiu suggestion this is the function that should reply to my question:

$matrix = array(
     21 => array(10,11,12),
     22 => array(10,11,12),
     23 => array(10,11,12),
     24 => array(10,11,12)
);

function Rotate($matrix, $pivot_x, $pivot_y) {
        $output = array();
        krsort($matrix);
        while (list($y) = each($matrix)) {
                reset($matrix[$y]);
                while (list(,$x) = each($matrix[$y])) {
                        list($offset_x,$offset_y) = array(
                                ($pivot_y - $y),
                                -1 * ($pivot_x - $x)
                        );
                        list($newx,$newy) = array($pivot_x - $offset_x , $pivot_y - $offset_y);

                        $output[$newy][] = $newx;
                }
        }
        return($output);
}

So, starting from a matrix like this...

10,24    11,24    12,24
10,23   >11,23<   12,23
10,22    11,22    12,22
10,21    11,21    12,21

If I rotate on pivot (x,y) (11,23)..

$output  = Rotate($matrix, 11, 23);

9,24    10,24    11,24    12,24
9,23    10,23   >11,23<   12,23
9,22    10,22    11,22    12,22

Whereas if I rotate on pivot (0,0):

$output = Rotate($matrix, 0, 0);

21,-10    22,-10    23,-10    24,-10
21,-11    22,-11    23,-11    24,-11
21,-12    22,-12    23,-12    24,-12

Obviously, any error correction is more than appreciated!