Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* fastest way to find a rectange from a specified surface*/
- function findBestRectangles(area, nbRectanges) {
- nbRectanges = nbRectanges || 1
- // let area = 12008;
- let a = Math.sqrt(area), maxXi = Math.ceil(area / 2) + 1;
- // this a match to return if no match was found
- let x = Math.floor(a); let y = Math.ceil(a);
- let bismatched = { x: x, y: y, calc: x * y };
- let allMatched_X = []; let allMatched_Y = [];
- const result = {
- matched: null,
- allMatched: [],
- matchedBis: bismatched
- };
- // check if already good
- if (result.matchedBis.calc == area) {
- result.matched = result.matchedBis;
- return result;
- }
- let Xi; let Yj;
- Xi = x; Yj = y;
- console.time('findBestRectangles_' + area);
- if (result.matched == null)
- do {
- Yj--; // hauteur
- //we reset X
- Xi = x; // largeur
- do {
- Xi++;
- let calc = Yj * Xi;
- if (calc == area) {
- let bx = allMatched_X.indexOf(Xi) == -1;
- let bY = allMatched_Y.indexOf(Yj) == -1;
- if (bx && bY) {
- allMatched_Y.push(Xi); allMatched_X.push(Yj);
- let matched = { x: Xi, y: Yj };
- result.allMatched.push(matched);
- if (result.allMatched.length >= nbRectanges) {
- result.matched = result.allMatched.length > 0 ? result.allMatched[0] : null;
- console.timeEnd('findBestRectangles_' + area);
- return result;
- // break;
- }
- }
- }
- } while (Xi < maxXi)
- } while (Yj > 1);
- console.timeEnd('findBestRectangles_' + area);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement