Game Development Reference
In-Depth Information
Another approach that is less computationally demanding but limited to rectangles is to ro-
tate the point into the rectangle's local space, then do the point in rectangle test.
bool rectangle_oriented::Contains(float x, float y)
{
float cosine = cos(m_angle);
float sine = sin(m_angle);
const auto center = Center();
float rx = center.x() + cosine * ( x - center.x() ) - sine * ( y - center.y() );
float ry = center.y() + sine * ( x - center.y() ) + cosine * (y - center.y() );
return rectangle::Contains(rx, ry);
}
Rectangle Intersection
Thetestforrectangleoverlapisstraightforward,giventworectangles, a and b respectively,
to test if a intersects with b we test whether a is completely outside of b , if it is, then it is
impossible for an intersection to exist, otherwise the rectangles intersect in some way.
bool rectangle::Intersects(const rectangle& rect) const
{
return (Left() < rect.Right() && Right() > rect.Left() &&
Top() < rect.Bottom() && Bottom() > rect.Top());
}
Clipping
It is often useful to clip or cut a rectangle to be within another, the rectangle being clipped
takes on the characteristics of the clipping rectangle wherever it intersects.
Iftheleft edgeoftherectangle isless thantheclipping rectangle'sleft edge,weneedtore-
ducethewidthoftherectangletocutofftheportionthatisoutsideoftheclippingrectangle
and then we clamp the left coordinate to the clipping rectangle's left coordinate. Clipping
the top coordinate is done in the same way using the top and heights respectively.
Clipping from the right side only requires decreasing the width of the rectangle by the por-
tion of the rectangle that extrudes the clipping rectangle, the left coordinate remains unaf-
fected as it is guaranteed to be within the clipping rectangle.
void rectangle::ClipTo(const rectangle& clipping_rectangle)
{
if ( m_left < clipping_rectangle.Left() )
{
Search WWH ::




Custom Search