一个不错的求矩形交集的算法

/* ************************************************************************
    Return a Rect object that is the intersection of 'this' with 'rect'
************************************************************************
*/
Rect Rect::getIntersection(
const  Rect &  rect)  const
{
    
//  check for total exclusion
     if  ((d_right  >  rect.d_left)  &&
        (d_left 
<  rect.d_right)  &&
        (d_bottom 
>  rect.d_top)  &&
        (d_top 
<  rect.d_bottom))
    {
        Rect temp;

        
//  fill in temp with the intersection
        temp.d_left  =  (d_left  >  rect.d_left)  ?  d_left : rect.d_left;
        temp.d_right 
=  (d_right  <  rect.d_right)  ?  d_right : rect.d_right;
        temp.d_top 
=  (d_top  >  rect.d_top)  ?  d_top : rect.d_top;
        temp.d_bottom 
=  (d_bottom  <  rect.d_bottom)  ?  d_bottom : rect.d_bottom;

        
return  temp;
    }
    
else
    {
        
return  Rect( 0.0f 0.0f 0.0f 0.0f );
    }

}

你可能感兴趣的:(算法)