Hit-testing

In computer programming, hit-testing (hit detection, picking, or pick correlation) is the process of determining whether a given object intersects another given object shape, line, or curve drawn on the screen. Most commonly, hit-testing is used by GUI environments to respond to user actions. In addition, hit-testing is used in small-scale games, which do not require much computing power, and are thus able to perform all of their collision detection tests in one frame of the game loop. In Web programming languages such as SVG and CSS, this is associated with the concept of pointer-events (e.g. user-initiated cursor movement or object selection).

Contents

Algorithm

The most commonly known hit-test algorithm is presented in the pseudo-code below:

function HitTest(Rectangle r1, Rectangle r2) returns boolean
{
    if ((r1.X + r1.Width >= r2.X) and
        (r1.X <= r2.X + r2.Width) and
        (r1.Y + r1.Height >= r2.Y) and
        (r1.Y <= r2.Y + r2.Height)) then return true
    else return false
}

See also

References

External links