[cairo] Hit detect surface?

chris nuernberger cnuernber at gmail.com
Fri Mar 3 14:29:32 PST 2006

It is a lot of drawing to find a hit.  This sort of method is supposed
to be the somewhat stupid brute force method of detecting the hit, it
is the lowest level way to go.  I think it is entirely appropriate for
this type of hit detection to be possible in cairo, however.

People who just want something to work will use this method, and
people who want to build more advanced structures for their hit tests
will do that.

There is a very similar system available through opengl.  A game or
something like that will usually have their own set of objects and do
hit detection themselves because they have build up BSP trees or
octrees or whatever completely independent of the rendering engine. 
But that doesn't make using the callback system invalid or useless by
any means, it is still simplest way to do the hit detection so in that
regard it needs to be stupid, robust and correct.

But not necessarily elegant or fast, if you want that then you roll your own.

On 3/3/06, Gustavo Carneiro <gjc at inescporto.pt> wrote:
> <quote who="Bill Spitzak">
> > Gustavo J. A. M. Carneiro wrote:
> >
> >>   But there is a stronger argument against this approach: some
> >> applications are more interested in "how far is the mouse pointer from
> >> every object", or "what is the object nearest to the pointer" than "give
> >> me the object under the mouse pointer".  Because most times it is not
> >> comfortable for the user to have to place the pointer exactly over an
> >> pixel that happens to be painted by some object.  For instance, I
> >> remember how early nautilus used a very precise (to the pixel) hit
> >> detection for file icons; guess what, usability studies later showed
> >> that users were annoyed by the fact that clicking on a CD icon wouldn't
> >> work in some cases, particularly when the pointer is exactly over the
> >> hole :)
> >
> > What I am proposing *is* to do this "nearest the pointer" type hit
> > detection.
> >
> > There is only one "pixel" in the surface, and the caller has to set the
> > transformation so that the region of interest is mapped to the area of
> > this pixel. You can thus hit-detect anything that draws into any
> > parallelogram-shaped region, though typically a square around the mouse
> > cursor will be used. If the square is 20 pixels on a side, this will
> > pick up everything within 10 pixels of the mouse.
> >
> > To find the nearest, you must draw several times, with smaller and
> > smaller squares, until either you reach a minimum size, or only one
> > object is detected (or it goes to zero, in which case you use the
> > previous pass). (actually now that I think about it, it may be more
> > efficient to start at the minimum size and go up until you hit something.)
>   It sounds like a lot of drawing just to find if the mouse pointer is
> near an object.  I suppose it could be useful in very complex shapes,
> but I would imagine it would be much more efficient in most cases to
> pretend an item is approximately a rectangle, circle, or another simple
> shape, and then use  plain old math to determine the distance from the
> mouse pointer to the shape.
>   I would therefore propose that a distance_to_point(x, y) virtual
> method/signal be included in the canvas item.  Implementations can then
> choose the best method to implement it, which can be simple math
> calculus or more complex cairo surface manipulations (for really complex
> shapes).
>   Regards.
> PS: sorry for entering this discussion without even looking at Sven's
> canvas code :-P
> _______________________________________________
> cairo mailing list
> cairo at cairographics.org
> http://cairographics.org/cgi-bin/mailman/listinfo/cairo

More information about the cairo mailing list