function getIntersection(rect1, rect2) { let intersection = []; for (let i = rect1.x; i < rect1.x + rect1.width; i++) { for (let j = rect1.y; j < rect1.y + rect1.height; j++) { if (i >= rect2.x && i < rect2.x + rect2.width && j >= rect2.y && j < rect2.y + rect2.height) { intersection.push({ x: i, y: j }); } } } return intersection; }

// 测试 let rect1 = { x: 0, y: 0, width: 5, height: 5 }; let rect2 = { x: 2, y: 2, width: 5, height: 5 }; let intersection = getIntersection(rect1, rect2); if (intersection.length > 0) { console.log('Intersection:'); for (let i = 0; i < intersection.length; i++) { console.log((${intersection[i].x}, ${intersection[i].y})); } } else { console.log('No intersection.'); }

写一个JS方法 传入两个参数rect1和rect2,如果他们相交用for循环打印相交的位置

原文地址: https://www.cveoy.top/t/topic/xJ7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录