2

I have a problem in which i need to verify if a point had crossed a line-path,
a line path is collection of line(y=ax+b).
Does anyone know some known algorithim for this?

so i solved it like this: i added 2 points in the start and the end of the path- so now it is a polygon i added the 2 points in 90 degrees to the points in a fixed distance. and i used the ray algorithim.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1763180
  • 115
  • 11
  • Make your own algorithm! A point can have 2 states, either on one side of the line, or the other (or on it...I guess). Just check if a point ever changes state. – Jiminion Jul 24 '13 at 13:50
  • By "collection of line" do you mean a collection of line segments? Are the segments connected? If you don't mean segments, what does it mean for a point to "cross" three lines? Is the point moving? – Joni Jul 24 '13 at 13:52
  • joni: i mean connected segments, a point can cross one line of the segment but in fact it hasnt crossed the segmant. – user1763180 Jul 24 '13 at 13:55
  • Get some inspiration here: http://stackoverflow.com/questions/385305/efficient-maths-algorithm-to-calculate-intersections – fvu Jul 24 '13 at 13:56
  • @user1763180: You mean like a ray crossing a polygon from side to the other?Just that the polygon is represented by a set of segments? – Aravind Jul 24 '13 at 14:25
  • you can get the angle of the line, based on this you can 'un-rotate' the point you want to test - basically have the point in a coordinate space perpendicular to the line, regardless of the line's orientation and in that coordinate space, check if it's above or bellow. another way would be to check the line's angle: if it's horizontal or vertical that should be obivious. For the other one you need to check which is the lower point in your line and if the test point's y position is higher than that. – George Profenza Jul 30 '13 at 10:51

4 Answers4

1

There are simple algorithms to know if a point is inside or outside a polygon: http://en.wikipedia.org/wiki/Point_in_polygon This can be adapted to a line path setting by pushing some edges of the polygon to infinity (in practice, you can put your line path in a large box and cansider the polygon formed by the part of the box that is on the right (or left, as you want) side of the line).

Thomash
  • 6,339
  • 1
  • 30
  • 50
1

Given a input point (x_1, y_1) , and your line is of the form y = ax + b, then you can tell where your input point locates by putting x_1 into the line equation:

if y_1 == a * x_1 + b then (x_1, y_1) is on the line
if y_1 < a * x_1 + b then (x_1, y_1) locates below the line
if y_1 > a * x_1 + b then (x_1, y_1) locates above the line

So you can tell whether a point has crossed a line by keeping track of the above result of that point.

keelar
  • 5,814
  • 7
  • 40
  • 79
0

so i solved it like this: i added 2 points in the start and the end of the path- so now it is a polygon i added the 2 points in 90 degrees to the points in a fixed distance. and i used the ray algorithim.

edit: its not always 90 degrees, it's depens on the angle between point start and point end

user1763180
  • 115
  • 11
0

I know of two approaches:

  1. use adapted point inside polygon algorithm
    • you must know which way is which side (crossed/non-crossed side)
    • so create a vector dir which points from line patch to the crossing side
    • it can be average normal (or normal of start-end point line)
    • cast ray from tested point in dir direction
    • count intersections with your line patch
    • if intersection occurs exactly on a connection point of two lines count it only once
    • at the end if the count is nonzero and odd then point has crossed line patch
    • this is robust error prone but little bit slow
    • see the left picture
  2. if your line patch is not too complex shaped use winding rule (scalar vector multiplication)
    • line patch lines must be in single direction from start to end !!!
    • select lines from patch that are close to your point (1-5 should be sufficient)
    • ideally at the same height on the right picture
    • of course in real it can be rotated so select lines by the distance (no need to sqrt it)
    • compute dot product for selected lines like this:
    • line P0,P1, point P -> dot product = ((P1-P0).(P-P1))
    • dot product of 2 vectors ((x0,y0,z0).(x1,y1,z1))=(x0*x1+y0*y1+z0*z1)
    • the polarity of the result means the winding direction CW/CCW
    • if all the windings are correct than the point is not crossed
    • if the line patch shape is complex then problems can occur if point is too close to it
    • in that case test only lines of the same 'height' or use method 1.
    • to get the 'height' compute the distance between point and the line start point in lines direction
    • if its from zero to size of line that its OK else do not use it
    • also can be done with dot product on normal vector to line direction

Line patch crossing

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • sorry for the small text in picture i am too lazy to redraw it if you have a problem reading it save the picture on disk (image is just scaled to be small on this site) or zoom in your brownser – Spektre Oct 05 '13 at 08:39