Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*slower method to find a rectange from a surface*/
- function findAllRectanglesBrutForce(area) {
- // let area = 558;
- let a = Math.sqrt(area), maxY = Math.ceil(area / 2) + 1;
- // if area= 18, a= 4.242, but 18=6*3
- let x = Math.floor(a); let y = Math.ceil(a);
- // this a match to return if no match was found
- let bismatched = { x: x, y: y, calc: x * y };
- const result = {
- matched: null,
- allMatched: null,
- matchedBis: bismatched
- };
- // check if already good
- if (x = + a && y == a) {
- result.matched = matched;
- }
- let matched = null;
- let allMatched = [];
- let allMatched_X = [];
- let allMatched_Y = [];
- let xi; let yi;
- xi = x; yi = y;
- console.time('findAllRectanglesBrutForce_' + area);
- if (result.matched == null)
- for (let xi = 2; xi < area; xi++) {
- for (let yi = area; yi > 1; yi--) {
- let calc = xi * yi;
- if (calc == area) {
- let bx = allMatched_X.indexOf(yi) == -1;
- let bY = allMatched_Y.indexOf(xi) == -1;
- if (bx && bY) {
- allMatched_X.push(xi);
- allMatched_Y.push(yi);
- matched = { x: xi, y: yi };
- allMatched.push(matched);
- }
- }
- }
- }
- console.timeEnd('findAllRectanglesBrutForce_' + area);
- /**
- * area 558 = 9.28076171875ms
- * algo0_558: 6.281005859375ms
- */
- result.matched = matched;
- result.allMatched = allMatched;
- result.matchedBis = bismatched;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement