forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateMatrix.js
More file actions
54 lines (46 loc) · 1.49 KB
/
Copy pathRotateMatrix.js
File metadata and controls
54 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Based on the routine from {@link http://jsfiddle.net/MrPolywhirl/NH42z/}.
var CheckMatrix = require('./CheckMatrix');
var TransposeMatrix = require('./TransposeMatrix');
/**
* [description]
*
* @function Phaser.Utils.Array.Matrix.RotateMatrix
* @since 3.0.0
*
* @param {array} matrix - The array to rotate.
* @param {number|string} [direction=90] - The amount to rotate the matrix by. The value can be given in degrees: 90, -90, 270, -270 or 180, or a string command: `rotateLeft`, `rotateRight` or `rotate180`.
*
* @return {array} The rotated matrix array. The source matrix should be discard for the returned matrix.
*/
var RotateMatrix = function (matrix, direction)
{
if (direction === undefined) { direction = 90; }
if (!CheckMatrix(matrix))
{
return null;
}
if (typeof direction !== 'string')
{
direction = ((direction % 360) + 360) % 360;
}
if (direction === 90 || direction === -270 || direction === 'rotateLeft')
{
matrix = TransposeMatrix(matrix);
matrix = matrix.reverse();
}
else if (direction === -90 || direction === 270 || direction === 'rotateRight')
{
matrix = matrix.reverse();
matrix = TransposeMatrix(matrix);
}
else if (Math.abs(direction) === 180 || direction === 'rotate180')
{
for (var i = 0; i < matrix.length; i++)
{
matrix[i].reverse();
}
matrix = matrix.reverse();
}
return matrix;
};
module.exports = RotateMatrix;