Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if 3 points are exactly collinear in Z^2

Tags:

c

Another collinear-points question. This one's twist is, I'm using integer arithmetic, and I'm looking for exact collinearity, not a fuzzy epsilon-based test.

With inline assembly, I can get an exact answer: the x86 multiply instruction gives access to both the high and low parts of the product, both of which matter in calculating the cross product (X - A) x (B - A); I can simply OR the two halves together and test for zero. But I'm hoping there's a way to do it in C, that's:

  1. Overflow-proof
  2. Portable
  3. Elegant

in roughly that order. And at the same time, a way to do it that is/does NOT:

  1. involve casting to double
  2. involve using a bigger integer type - assume that I'm already using the biggest integer type available for my coordinate component type
  3. yield either false positives or false negatives.

I'm not concerned in this question about whether X is beyond the segment AB; that's just four uninteresting comparisons.

My nightmare scenario is that I'll have to break each coordinate component into two halves, and do long multiplication explicitly, just so I can keep track of all the high halves in the partial products. (And then having to do add-with-carry explicitly.)

like image 405
Bernd Jendrissek Avatar asked Feb 28 '12 00:02

Bernd Jendrissek


People also ask

How do you prove Collinearity with 3 points?

If two lines have the same slope pass through a common point, then the two lines will coincide. In other words, if A, B, and C are three points in the XY-plane, they will lie on a line, i.e., three points are collinear if and only if the slope of AB is equal to the slope of BC.

How do you know if points are collinear or not?

Three points are collinear if the value of the area of the triangle formed by the three points is zero. Substitute the coordinates of the given three points in the area of the triangle formula. If the result for the area of the triangle is zero, then the given points are said to be collinear.


1 Answers

After some comparisons and simple checks, you can get 2 couple of positive numbers (x1,y1), (x2,y2), that you want to check if x1*y2==x2*y1.

You can use the Euclidean algorithm to find the GCD of x1 and y1, and divide them both by the GCM. Do the same thing for (x2,y2). If you got the same couple in both cases, then both vectors have the same direction.

like image 81
asaelr Avatar answered Sep 27 '22 20:09

asaelr