Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to implement a lasso selection tool?

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.

like image 529
CJ. Avatar asked Jan 16 '10 23:01

CJ.


People also ask

How do you make a selection using the Lasso Tool?

This lasso tool creates a straight line in between each mouse click. So, just simply click on any edge of the area that you want to select and release your mouse pointer, this will create a starting point or anchor point. Now, when you drag your mouse you will see a line(like a string) attached to your pointer.

What are the 3 types of lasso tools?

In fact, there are three kinds of lasso tools: the standard tool, the polygonal tool, and the magnetic lasso tool.

How does Lasso selection work?

Click somewhere along the edge you want to select and begin to drag your cursor along that edge. The Lasso Tool will create a path behind your cursor to be used as your new selection area. Continue along the edge you want to cut out until you loop back to the starting point of your path.

What is Lasso Tool and how it works?

The lasso tool operates on the active layer of an image, and is used by clicking and dragging to trace the edges of a selection. Most software supports multiple closed contours, which can be selected by crossing over the edge path multiple times.


2 Answers

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).

like image 92
Thomas Avatar answered Sep 18 '22 12:09

Thomas


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.

like image 41
Yann Ramin Avatar answered Sep 16 '22 12:09

Yann Ramin