Sign in to follow this  
giugio

picking in 2d

Recommended Posts

giugio    246
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 this post


Link to post
Share on other sites
Yours3!f    1532
[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 this post


Link to post
Share on other sites
lauris71    841
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this