2

I know that this question has been up before, but I'll give it a shot since it's not exactly the same question.

Imagine a straight line consisting of a start point and an end point. The orientation of the line in 2D can be whatever we choose. Now imagine that we are traveling along this line, but not exactly on the line. I would like to give the directional (e.g right, left) feedback so that they know what direction to head to stay on the line.

I've been reading and trying this solution: https://math.stackexchange.com/questions/274712/calculate-on-which-side-of-straign-line-is-dot-located

I'm currently stuck when trying to grasp what happens if the we travel in the opposite direction.. I's there a solution that will always give us the correct direction? No matter the line's orientation and our traveling direction. I have the possibility to place one or more reference points in the plane that are known.

Simtrax
  • 127
  • 9

1 Answers1

1

In the answer you point to it gives the equation you'll need to work out if you are to the right or left of a line from (x1,y1) to (x2,y2). So for your question if you are travelling in the opposite direction you simply take the "end" of the line and use it as the start point and the "start" of the line and use it as the end point.

From an algorithmic point of view it doesn't matter which "direction" you are travelling in. You always know your last point ((x1,y1)) and the next point you are aiming for ((x2,y2)) so you simply put those into

d=(x−x1)(y2−y1)−(y−y1)(x2−x1)

along with your current position ((x,y)) to get either a +ve or -ve number for left or right.

Ian Turton
  • 81,417
  • 6
  • 84
  • 185