8
$\begingroup$

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?

$\endgroup$
  • 4
    $\begingroup$ 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. $\endgroup$ – Git Gud Mar 6 '14 at 16:14
  • 1
    $\begingroup$ Another way is to sum the distance between the two adjacent points, and see that if it equal to the distance between extreme points. $\endgroup$ – Sawarnik Mar 6 '14 at 16:51
15
$\begingroup$

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}$$

$\endgroup$
  • $\begingroup$ How do you use this equation if any of the xs cancel out to 0, resulting in diving by 0? $\endgroup$ – Mirror318 Aug 2 '17 at 0:42
  • $\begingroup$ If a pair of x's cancel out, that means those two points are part of a vertical line (same x values, different y values) $\endgroup$ – lfalin Oct 13 '17 at 15:11
5
$\begingroup$

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$$

$\endgroup$
  • $\begingroup$ -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. $\endgroup$ – user10324 May 19 '16 at 16:12
  • $\begingroup$ @user10324 Obviously it must be the cross product. Edited. $\endgroup$ – Macavity May 19 '16 at 16:36
  • $\begingroup$ 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!) $\endgroup$ – user10324 May 20 '16 at 5:01
3
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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.$$

$\endgroup$
  • 1
    $\begingroup$ 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$. $\endgroup$ – ccorn May 19 '16 at 20:10
2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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)

$\endgroup$
0
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.