UNPKG

@allmaps/render

Version:

Render functions for WebGL and image buffers

86 lines (85 loc) 2.54 kB
function applyHomogeneousTransform(homogeneousTransform, coordinate) { const x = coordinate[0]; const y = coordinate[1]; return [ homogeneousTransform[0] * x + homogeneousTransform[2] * y + homogeneousTransform[4], homogeneousTransform[1] * x + homogeneousTransform[3] * y + homogeneousTransform[5] ]; } function createHomogeneousTransform() { return [1, 0, 0, 1, 0, 0]; } function multiplyHomogeneousTransform(homogeneousTransform1, homogeneousTransform2) { const a1 = homogeneousTransform1[0]; const b1 = homogeneousTransform1[1]; const c1 = homogeneousTransform1[2]; const d1 = homogeneousTransform1[3]; const e1 = homogeneousTransform1[4]; const f1 = homogeneousTransform1[5]; const a2 = homogeneousTransform2[0]; const b2 = homogeneousTransform2[1]; const c2 = homogeneousTransform2[2]; const d2 = homogeneousTransform2[3]; const e2 = homogeneousTransform2[4]; const f2 = homogeneousTransform2[5]; return [ a1 * a2 + c1 * b2, b1 * a2 + d1 * b2, a1 * c2 + c1 * d2, b1 * c2 + d1 * d2, a1 * e2 + c1 * f2 + e1, b1 * e2 + d1 * f2 + f1 ]; } function composeHomogeneousTransform(dx1, dy1, sx, sy, angle, dx2, dy2) { const sin = Math.sin(angle); const cos = Math.cos(angle); return [ sx * cos, sy * sin, -sx * sin, sy * cos, dx2 * sx * cos - dy2 * sx * sin + dx1, dx2 * sy * sin + dy2 * sy * cos + dy1 ]; } function invertHomogeneousTransform(source) { const determinant = getDeterminant(source); const a = source[0]; const b = source[1]; const c = source[2]; const d = source[3]; const e = source[4]; const f = source[5]; return [ d / determinant, -b / determinant, -c / determinant, a / determinant, (c * f - d * e) / determinant, -(a * f - b * e) / determinant ]; } function getDeterminant(mat) { return mat[0] * mat[3] - mat[1] * mat[2]; } function homogeneousTransformToMatrix4(homogeneousTransform) { const matrix4 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; matrix4[0] = homogeneousTransform[0]; matrix4[1] = homogeneousTransform[1]; matrix4[4] = homogeneousTransform[2]; matrix4[5] = homogeneousTransform[3]; matrix4[12] = homogeneousTransform[4]; matrix4[13] = homogeneousTransform[5]; return matrix4; } export { applyHomogeneousTransform, composeHomogeneousTransform, createHomogeneousTransform, getDeterminant, homogeneousTransformToMatrix4, invertHomogeneousTransform, multiplyHomogeneousTransform }; //# sourceMappingURL=homogeneous-transform.js.map