# Closest point on a line segment from point outside the line From the above pic I found the value x from line (p1,p2) and point a.

Using `y=mx+b` and an imaginary red line which is perpendicular to the black line having slope -1/m

and the intersecting point x, the problem is that x is not actually intersecting.

See pic below • What software are you using to get the location of `x`? How far off the line is `x` falling? – Erica Jul 23 '14 at 12:00
• I am using Google map. the point x is calculated by mathematical formula.when map zoom in to the deepest location I found that two lines are not intersecting.I thing the points contain large precisions – Virender Sehwag Jul 23 '14 at 12:06
• The mismatch is probably caused by insufficient decimal accuracy during the calculation, and/or in Google Map's rendering of the location. – Erica Jul 23 '14 at 12:10
• If it cause by math problem any solution ? Erica ... – Virender Sehwag Jul 23 '14 at 12:14
• The precision can be increased by using more decimals, but is not likely to ever be perfect. At some point, Google will be the problem -- e.g., you upload a point with 100 decimal places, they will simply round it off to less. – Erica Jul 23 '14 at 12:22

I wrote this for python and return a qgisPoint:

``````def nearestPoint(A, B, p):
A0 = A
A1 = A
B0 = B
B1 = B
p0 = p
p1 = p
AB0 = B0-A0
AB1 = B1-A1
AB_squared = (AB0*AB0+AB1*AB1)+0.0
if (AB_squared == 0):
return A
else:
Ap0 = p0-A0
Ap1 = p1-A1
t = (Ap0*AB0+Ap1*AB1)/AB_squared
if (t<0):
return (A0,A1);
elif (t>1):
return (B0,B1);
else:
return QgsPoint(A0+t*AB0,A1+t*AB1)
``````

AB are from the line segment and p is the point of interest