265 lines
7.7 KiB
JavaScript
265 lines
7.7 KiB
JavaScript
'use strict';
|
|
|
|
var Matrix = require('./Matrix.js');
|
|
|
|
"use strict";
|
|
const ux = [1, 1, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, -1, -1, 0, 1];
|
|
const uy = [0, 1, 1, 1, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, -1, -1];
|
|
const vx = [0, -1, -1, -1, 0, 1, 1, 1, 0, 1, 1, 1, 0, -1, -1, -1];
|
|
const vy = [1, 1, 0, -1, -1, -1, 0, 1, -1, -1, 0, 1, 1, 1, 0, -1];
|
|
const rotationCayley = [];
|
|
const rotationMatrices = [];
|
|
const signum = Math.sign;
|
|
function init() {
|
|
for (let i = 0; i < 16; i++) {
|
|
const row = [];
|
|
rotationCayley.push(row);
|
|
for (let j = 0; j < 16; j++) {
|
|
const _ux = signum(ux[i] * ux[j] + vx[i] * uy[j]);
|
|
const _uy = signum(uy[i] * ux[j] + vy[i] * uy[j]);
|
|
const _vx = signum(ux[i] * vx[j] + vx[i] * vy[j]);
|
|
const _vy = signum(uy[i] * vx[j] + vy[i] * vy[j]);
|
|
for (let k = 0; k < 16; k++) {
|
|
if (ux[k] === _ux && uy[k] === _uy && vx[k] === _vx && vy[k] === _vy) {
|
|
row.push(k);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (let i = 0; i < 16; i++) {
|
|
const mat = new Matrix.Matrix();
|
|
mat.set(ux[i], uy[i], vx[i], vy[i], 0, 0);
|
|
rotationMatrices.push(mat);
|
|
}
|
|
}
|
|
init();
|
|
const groupD8 = {
|
|
/**
|
|
* | Rotation | Direction |
|
|
* |----------|-----------|
|
|
* | 0° | East |
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
E: 0,
|
|
/**
|
|
* | Rotation | Direction |
|
|
* |----------|-----------|
|
|
* | 45°↻ | Southeast |
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
SE: 1,
|
|
/**
|
|
* | Rotation | Direction |
|
|
* |----------|-----------|
|
|
* | 90°↻ | South |
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
S: 2,
|
|
/**
|
|
* | Rotation | Direction |
|
|
* |----------|-----------|
|
|
* | 135°↻ | Southwest |
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
SW: 3,
|
|
/**
|
|
* | Rotation | Direction |
|
|
* |----------|-----------|
|
|
* | 180° | West |
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
W: 4,
|
|
/**
|
|
* | Rotation | Direction |
|
|
* |-------------|--------------|
|
|
* | -135°/225°↻ | Northwest |
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
NW: 5,
|
|
/**
|
|
* | Rotation | Direction |
|
|
* |-------------|--------------|
|
|
* | -90°/270°↻ | North |
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
N: 6,
|
|
/**
|
|
* | Rotation | Direction |
|
|
* |-------------|--------------|
|
|
* | -45°/315°↻ | Northeast |
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
NE: 7,
|
|
/**
|
|
* Reflection about Y-axis.
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
MIRROR_VERTICAL: 8,
|
|
/**
|
|
* Reflection about the main diagonal.
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
MAIN_DIAGONAL: 10,
|
|
/**
|
|
* Reflection about X-axis.
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
MIRROR_HORIZONTAL: 12,
|
|
/**
|
|
* Reflection about reverse diagonal.
|
|
* @memberof maths.groupD8
|
|
* @constant {GD8Symmetry}
|
|
*/
|
|
REVERSE_DIAGONAL: 14,
|
|
/**
|
|
* @memberof maths.groupD8
|
|
* @param {GD8Symmetry} ind - sprite rotation angle.
|
|
* @returns {GD8Symmetry} The X-component of the U-axis
|
|
* after rotating the axes.
|
|
*/
|
|
uX: (ind) => ux[ind],
|
|
/**
|
|
* @memberof maths.groupD8
|
|
* @param {GD8Symmetry} ind - sprite rotation angle.
|
|
* @returns {GD8Symmetry} The Y-component of the U-axis
|
|
* after rotating the axes.
|
|
*/
|
|
uY: (ind) => uy[ind],
|
|
/**
|
|
* @memberof maths.groupD8
|
|
* @param {GD8Symmetry} ind - sprite rotation angle.
|
|
* @returns {GD8Symmetry} The X-component of the V-axis
|
|
* after rotating the axes.
|
|
*/
|
|
vX: (ind) => vx[ind],
|
|
/**
|
|
* @memberof maths.groupD8
|
|
* @param {GD8Symmetry} ind - sprite rotation angle.
|
|
* @returns {GD8Symmetry} The Y-component of the V-axis
|
|
* after rotating the axes.
|
|
*/
|
|
vY: (ind) => vy[ind],
|
|
/**
|
|
* @memberof maths.groupD8
|
|
* @param {GD8Symmetry} rotation - symmetry whose opposite
|
|
* is needed. Only rotations have opposite symmetries while
|
|
* reflections don't.
|
|
* @returns {GD8Symmetry} The opposite symmetry of `rotation`
|
|
*/
|
|
inv: (rotation) => {
|
|
if (rotation & 8) {
|
|
return rotation & 15;
|
|
}
|
|
return -rotation & 7;
|
|
},
|
|
/**
|
|
* Composes the two D8 operations.
|
|
*
|
|
* Taking `^` as reflection:
|
|
*
|
|
* | | E=0 | S=2 | W=4 | N=6 | E^=8 | S^=10 | W^=12 | N^=14 |
|
|
* |-------|-----|-----|-----|-----|------|-------|-------|-------|
|
|
* | E=0 | E | S | W | N | E^ | S^ | W^ | N^ |
|
|
* | S=2 | S | W | N | E | S^ | W^ | N^ | E^ |
|
|
* | W=4 | W | N | E | S | W^ | N^ | E^ | S^ |
|
|
* | N=6 | N | E | S | W | N^ | E^ | S^ | W^ |
|
|
* | E^=8 | E^ | N^ | W^ | S^ | E | N | W | S |
|
|
* | S^=10 | S^ | E^ | N^ | W^ | S | E | N | W |
|
|
* | W^=12 | W^ | S^ | E^ | N^ | W | S | E | N |
|
|
* | N^=14 | N^ | W^ | S^ | E^ | N | W | S | E |
|
|
*
|
|
* [This is a Cayley table]{@link https://en.wikipedia.org/wiki/Cayley_table}
|
|
* @memberof maths.groupD8
|
|
* @param {GD8Symmetry} rotationSecond - Second operation, which
|
|
* is the row in the above cayley table.
|
|
* @param {GD8Symmetry} rotationFirst - First operation, which
|
|
* is the column in the above cayley table.
|
|
* @returns {GD8Symmetry} Composed operation
|
|
*/
|
|
add: (rotationSecond, rotationFirst) => rotationCayley[rotationSecond][rotationFirst],
|
|
/**
|
|
* Reverse of `add`.
|
|
* @memberof maths.groupD8
|
|
* @param {GD8Symmetry} rotationSecond - Second operation
|
|
* @param {GD8Symmetry} rotationFirst - First operation
|
|
* @returns {GD8Symmetry} Result
|
|
*/
|
|
sub: (rotationSecond, rotationFirst) => rotationCayley[rotationSecond][groupD8.inv(rotationFirst)],
|
|
/**
|
|
* Adds 180 degrees to rotation, which is a commutative
|
|
* operation.
|
|
* @memberof maths.groupD8
|
|
* @param {number} rotation - The number to rotate.
|
|
* @returns {number} Rotated number
|
|
*/
|
|
rotate180: (rotation) => rotation ^ 4,
|
|
/**
|
|
* Checks if the rotation angle is vertical, i.e. south
|
|
* or north. It doesn't work for reflections.
|
|
* @memberof maths.groupD8
|
|
* @param {GD8Symmetry} rotation - The number to check.
|
|
* @returns {boolean} Whether or not the direction is vertical
|
|
*/
|
|
isVertical: (rotation) => (rotation & 3) === 2,
|
|
// rotation % 4 === 2
|
|
/**
|
|
* Approximates the vector `V(dx,dy)` into one of the
|
|
* eight directions provided by `groupD8`.
|
|
* @memberof maths.groupD8
|
|
* @param {number} dx - X-component of the vector
|
|
* @param {number} dy - Y-component of the vector
|
|
* @returns {GD8Symmetry} Approximation of the vector into
|
|
* one of the eight symmetries.
|
|
*/
|
|
byDirection: (dx, dy) => {
|
|
if (Math.abs(dx) * 2 <= Math.abs(dy)) {
|
|
if (dy >= 0) {
|
|
return groupD8.S;
|
|
}
|
|
return groupD8.N;
|
|
} else if (Math.abs(dy) * 2 <= Math.abs(dx)) {
|
|
if (dx > 0) {
|
|
return groupD8.E;
|
|
}
|
|
return groupD8.W;
|
|
} else if (dy > 0) {
|
|
if (dx > 0) {
|
|
return groupD8.SE;
|
|
}
|
|
return groupD8.SW;
|
|
} else if (dx > 0) {
|
|
return groupD8.NE;
|
|
}
|
|
return groupD8.NW;
|
|
},
|
|
/**
|
|
* Helps sprite to compensate texture packer rotation.
|
|
* @memberof maths.groupD8
|
|
* @param {Matrix} matrix - sprite world matrix
|
|
* @param {GD8Symmetry} rotation - The rotation factor to use.
|
|
* @param {number} tx - sprite anchoring
|
|
* @param {number} ty - sprite anchoring
|
|
*/
|
|
matrixAppendRotationInv: (matrix, rotation, tx = 0, ty = 0) => {
|
|
const mat = rotationMatrices[groupD8.inv(rotation)];
|
|
mat.tx = tx;
|
|
mat.ty = ty;
|
|
matrix.append(mat);
|
|
}
|
|
};
|
|
|
|
exports.groupD8 = groupD8;
|
|
//# sourceMappingURL=groupD8.js.map
|