Jump to content

Hit-testing

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 137.175.144.40 (talk) at 23:44, 25 May 2014 (Algorithm: the code was overly complicated and used the bad practice : "if( x ) return true; else return false;" instead of "return x;"). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer graphics programming, hit-testing (hit detection, picking, or pick correlation [1]) is the process of determining whether a user-controlled cursor (such as a mouse cursor or touch-point on a touch-screen interface) intersects a given graphical object (such as a shape, line, or curve) drawn on the screen. Hit-testing may be performed on the movement or activation of a mouse or other pointing device.

Hit-testing is used by GUI environments to respond to user actions, such as selecting a menu item or a target in a game based on its visual location. In Web programming languages such as HTML, SVG, and CSS, this is associated with the concept of pointer-events (e.g. user-initiated cursor movement or object selection).

In computer gaming, 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.

Collision detection is a related concept for detecting intersections of two or more different graphical objects, rather than intersection of a cursor with one or more graphical objects.

Algorithm

There are many different algorithms that may be used to perform hit-testing, with different performance or accuracy outcomes. One common hit-test algorithm is presented in the pseudo-code below:

function HitTest(Rectangle r1, Rectangle r2) returns boolean
{
    return (
        ( (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) ) 
    )
}

See also

References

  1. ^ Computer Graphics: Principles and Practice 2nd Edition in C, Foley et al, Addison-Wesley, 1997.