1

I talking about 3D I have 2 points on a plane(A and B) and I made a Vector AB (B-A) from them. I have made a line with point A and Vector AB. On the same plane I have another point. I want to know if the point is on the right side or on the left side of the line I have made. Thanks for help..

AvishayDev
  • 11
  • 2
  • Important note: the right and left of a vector have a precise meaning; the right and left of a line is ambiguous. Consider for instance A = (0, 0) and B = (0, -1): the left half-plane is on the right of vector AB and the right half-plane is on the left of vector AB. – Stef Jul 30 '21 at 14:58

1 Answers1

1

You have got your two points A, B, and a third point C.

You may already know that you can find the cosinus of an angle, using the dot product:

cos(AB, AC) = AB . AC / (||AB|| ||AC||)

Where . denotes the dot product and || || the Euclidean norm.

However, what you want is not the cosinus of the angle, but the sinus of the angle. The sinus sin(AB, AC) will be positive if C is on the left of AB, and negative if C is on the right of AB.

Let's call D the point obtained by taking the image of B by the rotation of centre A and angle +Pi/2 (rotation by a quarter-turn counterclockwise).

Then it turns out that:

sin(AB, AC) = cos(AD, AC)

Furthermore, the coordinates of vector AD are easy to compute: if vector AB has coordinates (x,y), then vector AD has coordinates (-y, x).

Putting all this together, we get the following formula:

sin(AB,AC) = ((yA - yB)*(xC-xA) + (xB-xA)*(yC-yA)) / sqrt(((xB-xA)**2+(yB-yA)**2)*((xC-xA)**2 + (yC-yA)**2))

This is a number between -1 and +1. If all you care about is a "left" or "right" answer, then you only want the sign of sin(AB,AC); and the denominator of this expression is always positive. Hence you only need to compute the numerator:

n = ((yA - yB)*(xC-xA) + (xB-xA)*(yC-yA))
if n > 0:
  print("C is on the LEFT of AB")
else if n < 0:
  print("C is on the RIGHT of AB")
else if n == 0:
  print("C is on AB")
Stef
  • 13,242
  • 2
  • 17
  • 28