19

Three points $(x_1,y_1), (x_2,y_2)$ and $(x_3,y_3)$ whether fall in a straight line or not. How do I do that?

mahavir
  • 753
  • 6
    You take two of the points, assuming they are different, you can find the equation that defines the only line that crosses them and then you just need to check if the third point satisfies the equation. – Git Gud Mar 06 '14 at 16:14
  • 3
    Another way is to sum the distance between the two adjacent points, and see that if it equal to the distance between extreme points. – Sawarnik Mar 06 '14 at 16:51

7 Answers7

24

if slopes of lines with any two point will be same , then they are co-linear

i.e. $$\frac{y_2-y_1}{x_2-x_1}=\frac{y_3-y_1}{x_3-x_1}$$

mahavir
  • 753
9

Another way: a certain cross product must be zero, or $$ \left( (x_2-x_1)\hat{i}+(y_2-y_1)\hat{j}\right) \times \left((x_3-x_1) \hat{i}+(y_3-y_1)\hat{j}\right)=\vec{0}$$

$$\implies (x_2-x_1)(y_3-y_1)-(y_2-y_1)(x_3-x_1) = 0$$

Macavity
  • 46,381
  • -1. Have you ever checked your formula ? For $(x_3,y_3)=(2,2),(x_2,y_2)=(1,1),(x_1,y_1)=(0,0)$, which are on a line, your formular is false. I'm surprised no one ever remarked on this. –  May 19 '16 at 16:12
  • @user10324 Obviously it must be the cross product. Edited. – Macavity May 19 '16 at 16:36
  • Ok, I removed the -1, due to your fast correction. (But it is not that obvious that it must be the cross product, if you're not fluent with analytic geometry!) –  May 20 '16 at 05:01
3

Translate the points to $(0,0)$, $(x_2-x_1,y_2-y_1)$, $(x_3-x_1,y_3-y_1)$. Now, the condition is equivalent to the linear dependence of $(x_2-x_1,y_2-y_1)$, $(x_3-x_1,y_3-y_1)$, i.e.: $$\left|\matrix{x_2-x_1&y_2-y_1\cr x_3-x_1&y_3-y_1}\right| = 0.$$

  • 2
    An equivalent, nicely memorable formulation would be $\begin{vmatrix}1&x_1&y_1\1&x_2&y_2\1&x_3&y_3\end{vmatrix}=0$. – ccorn May 19 '16 at 20:10
3

Any two points are collinear in the cartesian plane and form an equation of the form $ax+by=c$. Simply test any two distinct pairs of numbers, find the associate $a$ and $b$, and see if they are the same.

Emily
  • 35,688
  • 6
  • 93
  • 141
2

This answer is about approximate collinearity, and can be used when the coordinates are not exact (floating-point).

The angle between the line segments $12$ and $23$ is given by

$$\arctan\frac{y_2-y_1}{x_2-x_1}-\arctan\frac{y_3-y_2}{x_3-x_2}.$$

To compare this angle to a small tolerance $\delta$, we cancompare the tangents, like

$$\left|\tan\left(\arctan\frac{y_2-y_1}{x_2-x_1}-\arctan\frac{y_3-y_2}{x_3-x_2}\right)\right|\le\tan(\delta).$$

This can be rewritten as

$$\left|(y_2-y_1)(x_3-x_2)-(y_3-y_2)(x_2-x_1)\right|\le\\ \left|((x_2-x_1)(x_3-x_2)+(y_2-y_1)(y_3-y_2))\right|\tan(\delta).$$

On the left, you recognize a cross product (double area of the triangle) and on the right a dot product (giving a relative area information).

Obviously, with $\delta=0$, you find the same expression as in the other posts.

1

The easiest way to do it is to find out if y2-y1/x2-x1 y3-y1/x3-x1 are equal (The slope of points 1 and 2 vs the slope of points 1 and 3)

0

Equation of a straight line is a linear function ($y=ax+b$) now if the system $$y_1=ax_1+b\\y_2=ax_2+b\\y_3=ax_3+b$$ has solutions it works,otherwise not

kingW3
  • 13,496