2015-07-28 66 views
0

如何獲得兩次比較的比較百分比?兩次CGRect比較的百分比

實例:

如果R1(0,0,150,150)R2(0,0,150,150),以便接收100%。

如果r1(0,0,150,150)r2(75,0,150,150)如此接收50%。

我需要接受兩個矩形,並返回百分比多少也有類似的一些功能.. 我需要兩個矩形

感謝您的幫助的重疊區域的:-)

+0

我想你可以創建這樣的方法。 –

+0

這不完全是你想要的,但可以是有用的。 'BOOL isIntersecting = CGRectIntersectsRect(rect1,rect2); ' –

回答

3

百分比嘗試這個;

var r1:CGRect = CGRect(x: 0, y: 0, width: 150, height: 150); 
var r2:CGRect = CGRect(x: 75, y: 0, width: 150, height: 150); 
//-- 
var per = rectIntersectionInPerc(r1, r2: r2); 
NSLog("per : \(per)) %"); 

-

//Width and Height of both rects may be different 
func rectIntersectionInPerc(r1:CGRect, r2:CGRect) -> CGFloat { 
    if (r1.intersects(r2)) { 
     var interRect:CGRect = r1.rectByIntersecting(r2); 
     //-- 
     return ((interRect.width * interRect.height)/(((r1.width * r1.height) + (r2.width * r2.height))/2.0) * 100.0) 
    } 
    return 0; 
} //F.E. 
+0

謝謝,但是如果rects有不同的寬度和高度? – Rebecca

+0

@Rebecca查看不同寬度和高度的rects的更新函數。 – Shoaib

+0

謝謝,我會檢查這個:-) – Rebecca