I know that, linestring is a vector and if i want to check that, if point coordinates are between linestring beginning and ending coordinates then i need to use vector comparison (x2 > x && x2 < x1 && y2 > y && y2 < y1) that comparison show me if x2(Point) coordinate is between a linestring(vector) codrinates.

But I don't know how do compare linestring coordinates and point coordinates,

I wonder if somebody can give me some tips on how do compare if a point corodinate is between linestring coordinates?

  • y=mx+c where m = y1-y0/x1-x0 and c is the intercept – Ian Turton Mar 16 '15 at 15:50
  • Are you trying to find the shortest distance from a point to a set of connected line segments (aka "linestring")? With the special case that if the distance is zero then the point is "on the line"? – Spacedman Jan 1 '16 at 16:38

If you are trying to solve a point in polygon problem, there are plenty of examples just within a google's reach.

If it is simply to find out whether the point coincides with one of the linestring vertices, a simple in operator (for Python) can do the job pretty well - or you can iterate the vertices and compare using abs() to introduce some coordinate precision tolerance.

If you want to know if the point lies on a polyline, iterate through the segment in the following way (Pythonic pseudocode):

function lies_on_polyline(point, vertices):
  for i from 0 to vertices.length - 2: // finish on last-but-one
    if lies_on_segment(point, vertices[i], vertices[i+1]):
      return True
  return False

function lies_on_segment(point, start, end):
  deltax = end.x - start.x
  if deltax == 0:
    liesInXDir = (point.x == start.x)
    t = (point.x - start.x) / deltax
    liesInXDir = (t >= 0 and t <= 1)
  if liesInXDir:
    deltay = end.y - start.y
    if deltay == 0:
      return (point.y == start.y)
      t = (point.y - start.y) / deltay
      return (t >= 0 and t <= 1)
    return False
  • im using javascript, but i only need to find that if point C is on the line segment of AB, but how i find if C is not on the line segment but near that ? I need to make two cases one is if C is on the line and second is if C is near a line. – Kevin Laanemägi Mar 17 '15 at 13:17
  • Edited. Should fulfill your needs. – Jan Šimbera Mar 19 '15 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.