• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# picking in 2d

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1giugio  Members

Posted 15 November 2012 - 05:57 AM

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, 15 November 2012 - 06:12 AM.

### #2Yours3!f  Members

Posted 15 November 2012 - 07:29 AM

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.

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.

### #3Lauris Kaplinski  Members

Posted 17 November 2012 - 03:56 AM

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.

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);
}

Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.