views:

365

answers:

2

I am developing a Mac OS X application which, as part of it's UI, will display many visual elements in it's main view which can be selected. These elements can be positioned really anywhere within the view. The UI will support various ways of selecting the elements: rectangular marquee selection, elliptical marquee selection, and 'free' lasso selection.

I already have rectangular and elliptical marquee selection working. The algorithm is pretty simple; an element is deemed 'selected' if the element's area intersects with the area of the rectangle/ellipse.

The lasso selection will work just as it does in modern image manipulation applications like Photoshop; the user can click-and-drag a path which will close itself, and the elements contained within the path drawn will be selected.

This algorithm will likely be much more complex than the rectangular/elliptical selection, since the form of the selection is unrestricted. I am wondering if anyone has experience writing something like this, or if you can point me in the right direction as to what kind of programming techniques are necessary, and what is the most efficient way this algorithm can work.

Thanks in advance.

+3  A: 

The only way I can think of is to treat the lasso outline as a polygon. Then you can use any standard point-inside-polygon test to check which elements to select.

You'll have to make a decision what to do when the polygon intersects itself (e.g. figure-8).

When constructing the polygon, to prevent it from getting too many points, maybe you can skip points that are too close to the previous point (maybe 3 pixels or so, depending on your application).

Thomas
Thanks, this is exactly what I needed. From the Wikipedia page, I found a resource showing sample code and the math behind the problem: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html. This algorithm works pretty well for me, I can even use the algorithm progressively as the user lasso selects a region with pretty good performance. Now I'm writing some logic to optimize how often points are added to the polygon; not just based on distance between the last point but also comparing the slopes/difference in angles of previous line segments.
CJ
+1  A: 

You're looking at a point in polygon problem: http://en.wikipedia.org/wiki/Point_in_polygon If you can guarantee that the polygon is convex (not likely), the algorithm becomes simpler.

Yann Ramin
Lasso selections typically aren't necessarily polygons--they can be any closed path. In Photoshop, for example, there is the Lasso tool and the Polygonal Lasso tool.
Ellie P.
A closed path is a polygon, with a large number of points.Photoshop's selector can be optimized easier due to the finite number of objects in the polygon, and the fact that one polygon unit = one object.
Yann Ramin