## Is point on line segment?

Put your problem here if it does not fit any of the other categories.

### Is point on line segment?

Does anyone have code for this? I've found a few different ones written in different languages, but they never work after I port them.
Jorenm
Junior Developer

Posts: 22
Joined: Sat Nov 28, 2009 9:16 am

Ok I am wrong. This one I ported works I just had to adjust the sensitivity for fingers on a touch screen versus mouse on a screen. Here's the code in case someone else has the same question!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
1.         public static boolean isPointOnLine(float lox, float loy, float ltx, float lty, float x, float y) {
2.
3.                    //determine if point is on line
4.
5.                    Float dx = x - lox;
6.
7.                    Float dy = y - loy;
8.
9.                    Float tx = ltx - lox;
10.
11.                    Float ty = lty - loy;
12.
13.
14.
15.                    //normalise the line vector
16.
17.                    Float t1 = new Float(1/Math.sqrt(tx*tx+ty*ty));
18.
19.
20.
21.                    tx *= t1;
22.
23.                    ty *= t1;
24.
25.
26.
27.                    //calculate inverse length of secondary vector
28.
29.                    Float dl = new Float(1/Math.sqrt(dx*dx+dy*dy));
30.
31.
32.
33.                    //take dot product of normalised line vector, and rotated normalised secondary vector
34.
35.                    Float dot = (dy*tx-dx*ty)*dl;
36.
37.                    //Increase these values for less or more picky
38.
39.                    if (dot < -0.2 || dot > 0.2) {
40.
41.                       return false;
42.
43.                    }
44.
45.
46.
47.                    //calculate distance along line segment by taking dot product of normalised line vector and un-normalised secondary vector
48.
49.                    Float dis = tx*dx+ty*dy;
50.
51.                    if (dis < 0 || dis > 1 / t1) {
52.
53.                       return false;
54.
55.                    }
56.
57.                 return true;
58.
59.         }
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
Jorenm
Junior Developer

Posts: 22
Joined: Sat Nov 28, 2009 9:16 am

Return to Other Coding-Problems

### Who is online

Users browsing this forum: Google [Bot] and 6 guests