0

I have two points A(x1,y1) & B(x2,y2) and I need to check if point c(x3,y3) falls on the straight line formed by point A & B.

A------C--------------------B then yes C is between A & B

A---------------------------B C
in second case C isn't between A & B.

MBo
  • 77,366
  • 5
  • 53
  • 86
user101
  • 21
  • 2
  • I reverted your edit because it changes the problem drastically. You can make another question or use clue from my comment – MBo Sep 25 '20 at 18:38
  • okay. thanks.Ill go through the link that you've shared. – user101 Sep 26 '20 at 16:11

3 Answers3

0

Using complex numbers, we define a similarity transformation that maps A to 0 and B to 1:

W = (Z - Za) / (Zb - Za)

Then Wc = (Zc - Za) / (Zb - Za) is a complex number that should have a zero or tiny imaginary value, and a real value between 0 and 1.

0

Make two vectors

cax = x1 - x3
cay = y1 - y3

cbx = x2 - x3
cby = y2 - y3

and check that angle between them is Pi:

Calculate dot and cross product of these vectors

dot = cax * cbx + cay * cby
cross = cax * cby - cay * cbx

Point C lies on segment AB if cross is zero and dot is negative

dot < 0
abs(cross) < eps

where eps is small value like 1e-6 to compensate floating point calculation errors.

MBo
  • 77,366
  • 5
  • 53
  • 86
  • thanks, got it! I wanted to know how are the dot & cross products are gonna be affected if the point C falls on a line parallel to the one formed through point A & B. Actually point A & B both are representing coordinates of a road and I need to check if point C also falls on the same road or not. I don't have any other info available about the road. Point A & B are mine (as in from my database) but point C is coming from a third party so C won't always fall exactly on the straight line between A&B. – user101 Sep 24 '20 at 09:35
  • You can calculate distance from C to AB line as length of projection of C onto AB [as described here](https://stackoverflow.com/questions/23772990/find-point-with-vector-projection/23773980#23773980) |CD| = |AC x AB|/|AB| – MBo Sep 25 '20 at 18:14
0

if you are sure that all three point are aligned, you can use the dot product between vectors AC and AB, and the dot product between vectors BA and BC.

Reminder: Vector AB = (xB - xA, yB - yA) Reminder: dot(AB, AC) = xABxAC + yAByAC

if dot(AB, AC) > 0, then it means C is in direction of B from A if dot(BA, BC) > 0, then it means C is in direction of A from B if both conditions above are satisfied, it means C is between A and B

Pierre Baret
  • 1,773
  • 2
  • 17
  • 35