2013-04-18 38 views
-1

如果我有它們的X,Y座標和它們的高度/寬度,PHP中兩個矩形之間重疊像素的數量最短的方法是什麼?這是一個最佳實踐問題,因爲我現在正在用if語句的bug/messy集合來做這件事。獲取重疊像素的數量PHP

編輯:這就是我想要的那一刻來解決:

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; 
} 
+11

分享的if語句,我們將幫助您imrovise它你的車的集合。 – DhruvPathak

回答

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

+0

我想獲取像素數量而不是布爾值。 – user2288298

+0

將'<'改爲' - ',del'?0:1', 它是重疊區域的高度和寬度 –

+0

'echo overlapping(1,1,5,5,4,4,5,5); ($ x1,$ y1,$ w1,$ h1,$ x2,$ y2,$ w2,$ h2){ $ wover = abs($ x1 +($ w1/2) - ($ x2 +($ w2/2))) - ($ w1/2)+($ w2/2); ($ h1/2) - ($ y2 +($ h2/2))) - ($ h1/2)+($ h2/2); $ h1 = return $ hover; } $ wover和$ hover都返回3 – user2288298