如果我有X,Y坐标及其高度/宽度,那么获取PHP中两个矩形之间重叠像素数的最短方法是什么?这是一个最佳实践问题,因为我目前正在使用错误/混乱的if语句集合。
编辑:这就是我现在要解决的问题:
echo overlapping(1,1,5,5,4,4,6,6).PHP_EOL;
echo overlapping(0,0,29,21,30,20,40,50).PHP_EOL;
function overlapping($x1,$y1,$w1,$h1,$x2,$y2,$w2,$h2){
$wover = abs($x1+($w1/2) - ($x2+($w2/2))) - ($w1/2)+ ($w2/2);
$hover = abs($y1+($h1/2) - ($y2+($h2/2))) - ($h1/2)+ ($h2/2);
return $wover*$hover;
}
答案 0 :(得分:0)
echo overlapping(0,-1,10,20,30,20,40,50);
function overlapping($x1,$y1,$w1,$h1,$x2,$y2,$w2,$h2){
$wover = abs($x1+($w1/2) - ($x2+($w2/2))) > ($w1/2)+ ($w2/2)?0:1;
$hover = abs($y1+($h1/2) - ($y2+($h2/2))) > ($h1/2)+ ($h2/2)?0:1;
if($hover && $wover ){
return 'over';
}
return 'not';
}
http://sandbox.onlinephpfunctions.com/code/1ea506fd72f827965d7241d520789a833b34dff5