# picking in 2d

## Recommended Posts

hello.
I implemented a simple 2d drawing sysytem that draws points and lines(for now)
I must implement a picking in 2d for intersection of a mouse click and a point and a line.
Has a sense create a ray for the intersection in 2d?
how i can implement these algorithm?
I also have to consider the modelview matrix and the scale/rotate that i can apply to the document .
there is an example?
thanks. Edited by giugio

##### Share on other sites
[quote name='giugio' timestamp='1352980635' post='5001199']
hello.
I implemented a simple 2d drawing sysytem that draws points and lines(for now)
I must implement a picking in 2d for intersection of a mouse click and a point and a line.
Has a sense create a ray for the intersection in 2d?
how i can implement these algorithm?
I also have to consider the modelview matrix and the scale/rotate that i can apply to the document .
there is an example?
thanks.
[/quote]

hey there, I don't really know how to deal with lines (approximate them as thin quads maybe?), but I've recently done this using triangles/quads:
http://www.gamedev.net/topic/633528-2d-picking-theory/

you might get an idea.

##### Share on other sites
Mathematically speaking you cannot find the intersection of mouse and point/line because the latter do not have an area. What you probably need is finding the distance between mouse cursor and your object - line or point. And if the distance is below certain value you treat your object as "selected".

To find distance between two pints you can simply use Euclidean distance formula.

Here is an example (in C) how to find distance between line segment and point. You have to test for three possible cases - the closest point to cusrsor is one endpoint, other endpoint or certain mid-line point.

[code]
static double
nr_line_point_distance2 (float Ax, float Ay, float Bx, float By, float Px, float Py)
{
double Dx, Dy, s;
double dist2;
Dx = Bx - Ax;
Dy = By - Ay;
s = ((Px - Ax) * Dx + (Py - Ay) * Dy) / (Dx * Dx + Dy * Dy);
if (s <= 0.0) {
dist2 = (Px - Ax) * (Px - Ax) + (Py - Ay) * (Py - Ay);
} else if (s >= 1.0) {
dist2 = (Px - Bx) * (Px - Bx) + (Py - By) * (Py - By);
} else {
double Qx, Qy;
Qx = Ax + s * Dx;
Qy = Ay + s * Dy;
dist2 = (Px - Qx) * (Px - Qx) + (Py - Qy) * (Py - Qy);
}
return sqrt (dist2);
}[/code]

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627643
• Total Posts
2978362

• 10
• 12
• 22
• 13
• 33