Game Development Reference
In-Depth Information
m_width -= clipping_rectangle.Left() - m_left;
m_left = clipping_rectangle.Left();
}
if ( m_top < clipping_rectangle.Top() )
{
m_height -= clipping_rectangle.Top() - m_top;
m_top = clipping_rectangle.Top();
}
if ( Right() > clipping_rectangle.Right() )
{
m_width -= ( Right() - clipping_rectangle.Right() );
}
if ( Bottom() > clipping_rectangle.Bottom() )
{
m_height -= ( Bottom() - clipping_rectangle.Bottom() );
}
}
Constraining
Similar to clipping, in some cases we want to guarantee that some point cannot leave the
boundary defined by the rectangle. The same test can be applied whether we intend to con-
strain a point or another rectangle. The tests are straightforward, if the point minus half of
the optional width parameter is less than the rectangle's left boundary, we will clamp the
leftpointtotheleftboundary.Ifthepointplushalfoftheoptionalwidthisbeyondtheright
boundary, we clamp the point to the right boundary minus the optional width. This will en-
sure that the rectangle (or point, if the width is zero) is constrained. The tests are the same
for the top and bottom coordinates.
void rectangle::Constrain(point& p, const point& size/*=point::Zero*/) const
{
float halfWidth = size.x() * 0.5f;
float halfHeight = size.y() * 0.5f;
if ( p.x() - halfWidth < m_left )
{
p.x() = m_left;
}
if ( p.x() + halfWidth > Right() )
{
p.x() = Right() - size.x();
}
if ( p.y() - halfHeight < m_top )
{
p.y() = m_top;
}
if ( p.y() + halfHeight >= Bottom() )
Search WWH ::




Custom Search