JSPM

2d-array-rotation

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 295
  • Score
    100M100P100Q109612F
  • License MIT

Rotates two-dimensional arrays clockwise by 90 degrees, 180 degrees, or 270 degrees

Package Exports

  • 2d-array-rotation

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (2d-array-rotation) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

2d-array-rotation

Build Status Coverage Status

A collection of functions to rotate the values within a given two-dimensional array (i.e. an Array of Arrays) clockwise by 90, 180 or 270 degrees. Potentially useful for -- say -- manipulating game pieces or sprites, among other possible applications. Provided as an ES6-compatible module.

Functions

Additional Notes

  • Source arrays are not directly modified.
  • Row contents can be of any valid type -- Number, String, Object, etc.
  • Values within rows are reindexed directly from the source array, not copied or cloned. Object references will therefore remain unchanged.
  • Arrays can be either square (e.g. 3x3, 4x4) or rectangular (e.g. 3x5, 4x8, etc.). However, make sure that the rows within the arrays are all the same length. Results will be inconsistent otherwise.

Installation

npm install 2d-array-rotation

Usage

Via ES6-style imports

import {transpose, mirror} from "2d-array-rotation";

Via standard node.js requires

const transpose = require('2d-array-rotation').transpose;
const mirror = require('2d-array-rotation').mirror;
// and so on

Functions

transpose(arr)

Flips the given array's values over its diagonal, equivalent to rotating it 90 degrees clockwise.

let i = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
];
let o = transpose(i);
/**
 * Result: [
 * [ 7, 4, 1 ],
 * [ 8, 5, 2 ],
 * [ 9, 6, 3 ]
 * ]
 */
Parameters
  • arr (Array): a two-dimensional array
Returns

The transpose of the input.

mirror(arr)

Flips the given array's values over its horizontal and vertical axes, the equivalent of rotating it 180 degrees clockwise.

let i = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12]
];
let o = mirror(i);
/**
 * Result: [
 * [12, 11, 10],
 * [ 9,  8,  7],
 * [ 6,  5,  4],
 * [ 3,  2,  1]
 * ]
 */
Parameters
  • arr (Array): a two-dimensional array
Returns

The mirrored form of the given array.

mirrorTranspose(arr)

Flips the given array's values over its diagonal as well as the horizontal and vertical axes. Equivalent to rotating it 270 degrees clockwise.

let i = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
];
let o = mirrorTranspose(i);
/**
 * Result: [
 * [ 3, 6, 9 ],
 * [ 2, 5, 8 ],
 * [ 1, 4, 7 ]
 * ]
 */
Parameters
  • arr (Array): a two-dimensional array
Returns

The mirrored transpose of the given array.

rotate(arr, deg)

Master function that calls transpose, mirror, or mirrorTranspose, depending on the input. Rotates the given two-dimensional array by the given number of degrees.

let i = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12],
    [13, 14, 15]
];
let o = rotate(i, 90);
/**
 * Result: [
 * [13, 10, 7, 4, 1],
 * [14, 11, 8, 5, 2],
 * [15, 12, 9, 6, 3]
 * ]
 */
Parameters
  • arr (Array): A two-dimensional array.
  • deg (Number): Number of degrees to rotate. Must be a multiple of 90, or an error will be thrown.
Returns

A two-dimensional Array with the values rotated; or, if rotating by 0 or 360 degrees, the original arr itself without any modifications.

hflip(arr)

Bonus function -- flips the array over its horizontal axis.

let input = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12]
];
let o = hflip(input);
/**
 * Result: [
 * [3, 2, 1],
 * [6, 5, 4],
 * [9, 8, 7],
 * [12, 11, 10],
 * ]
 */

vflip(arr)

Bonus function -- flips the array over its vertical axis.

let input = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
];
let o = vflip(input);
/**
 * Result: [
 * [ 7, 8, 9 ],
 * [ 4, 5, 6 ],
 * [ 1, 2, 3 ]
 * ]
 */

rotate90CW(arr)

A wrapper around transpose. Usage may potentially result in more readable code.

rotate180CW(arr)

A wrapper around mirror. Usage may potentially result in more readable code.

rotate270CW(arr)

A wrapper around mirrorTranspose. Usage may potentially result in more readable code.

Testing

npm test

Credits and Licensing

Created by Jon Stout. Licensed under the MIT license.

Special Thanks To

u/Epyo on Reddit.