關閉
標題:二個矩型 重疊
內容:
http://www.dazegraffiti.com/2012/05/blog-post.html
http://blog.csdn.net/zyxlsh/article/details/5937191
假設有兩個矩形A和B,矩形A的左上角坐標為(Xa1,Ya1),右下角坐標為(Xa2,Ya2),矩形B的左上角坐標為(Xb1,Yb1),右下角坐標為(Xb2,Yb2)。
當滿足下面兩個式子時,兩個矩形相交:
1.abs( Xb2+Xb1-Xa2-Xa1)<=Xa2-Xa1+Xb2-Xb1
2.abs(Yb2+Yb1-Ya2-Ya1) <=Ya2-Ya1+Yb2-Yb1
且相交的區域座標為:
左上(max(Xa1,Xb1),max(Ya1,Yb1))
右下(min(Xa2,Xb2),min(Ya2,Yb2))
public static boolean twoRectCross(double x1_left,double y1_top,double x1_right,double y1_bottom,
double x2_left,double y2_top,double x2_right,double y2_bottom)
{
double tmp_left=max( x1_left, x2_left );
double tmp_top =max( y1_top, y2_top );
double tmp_right = min( x1_right, x2_right );
double tmp_bottom = min( y1_bottom, y2_bottom );
return (tmp_right >= tmp_left && tmp_bottom >= tmp_top);
}