UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

72 lines (53 loc) 1.68 kB
import { array_swap } from "../../../../core/collection/array/array_swap.js"; import { assert } from "../../../../core/assert.js"; /** * * @param {number[]} pSortBuffer * @param {number} iLeft * @param {number} iRight * @param {number} channel * @param {number} uSeed * @returns {void} */ export function QuickSortEdges(pSortBuffer, iLeft, iRight, channel, uSeed) { let t; let iL, iR, n, index, iMid; // early out const iElems = iRight - iLeft + 1; if (iElems < 2) { return; } else if (iElems === 2) { if (pSortBuffer[iLeft * 3 + channel] > pSortBuffer[iRight * 3 + channel]) { array_swap(pSortBuffer, iLeft * 3, pSortBuffer, iRight * 3, 3); // swap elements } return; } // Random t = uSeed & 31; t = (uSeed << t) | (uSeed >> (32 - t)); uSeed = uSeed + t + 3; uSeed = uSeed >>> 0; // make unsigned // Random end iL = iLeft; iR = iRight; n = (iR - iL) + 1; assert.greaterThanOrEqual(n, 0); index = (uSeed % n); iMid = pSortBuffer[(index + iL) * 3 + channel]; do { while (pSortBuffer[iL * 3 + channel] < iMid) ++iL; while (pSortBuffer[iR * 3 + channel] > iMid) --iR; if (iL <= iR) { array_swap(pSortBuffer, iL * 3, pSortBuffer, iR * 3, 3); // do swap ++iL; --iR; } } while (iL <= iR); if (iLeft < iR) QuickSortEdges(pSortBuffer, iLeft, iR, channel, uSeed); if (iL < iRight) QuickSortEdges(pSortBuffer, iL, iRight, channel, uSeed); }