alex toader
alex toader

Reputation: 2418

How to tell whether a point is more to the right of a segment?

enter image description here

How can I tell what is the first segment to the right of AB? So we start walking from A, reach B and then I need to go most right - to C. But how do I find this mathematically? Bear in mind that there could be more segments from B in any direction. (some of them or all of them could be to the left of B - in my image C and D are to the right)

We do have all coordinates of vertices - x and y!

I am thinking to

Upvotes: 0

Views: 41

Answers (1)

MBo
MBo

Reputation: 80177

You have coordinates AX, AY, BX, BY and PX, PY for every other point.

It is necessary to calculate minimal angle between vectors P-B and A-B among all points P[i]

abx = AX - BX
aby = AY - BY  //calculate once

pbx = PX - BX
pby = PY - BY  //calculated for every point

angle = atan2(-pbx*aby+pby*abx, pbx*abx+pby*aby)
if angle < 0:
    angle += 2*Math.pi  //to make range 0..2*Pi

Calculate angle for every point (uses function atan2, Math.atan2 in some languages and cross product and dot product of vectors), then choose minimum value - it corresponds "the most right turn"

Python code

Upvotes: 1

Related Questions