且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查三个点是否形成一条直线?

更新时间:2023-01-18 11:34:48

(x1,y1)(x2,y2)(x3,y3)点在且仅当满足时位于同一行上

The points (x1,y1), (x2,y2), and (x3,y3) lie on the same line if and only if they satisfy

a x + b y + c = 0

用于abc的固定值(我无法克服您的表示法;对混乱"表示抱歉),其中ab为非零.因此,当且仅当

for fixed values of a, b, and c (I cannot get over your notation; sorry for the "confusion"), where a or b are nonzero. Hence they lie on the same line if and only if

a x1 + b y1 + c = 0         [x1 y1 1][a]   [0]
a x2 + b y2 + c = 0   <=>   [x2 y2 1][b] = [0]
a x3 + b y3 + c = 0         [x3 y3 1][c]   [0],

即具有矩阵的齐次线性系统

that is, the homogeneous linear system with the matrix

    [x1 y1 1]
X = [x2 y2 1]
    [x3 y3 1]

具有非零解.仅当X为单数时,才有可能.通过消除X的最后一列,您可以发现X是单数的,当且仅当矩阵

has a nonzero solution. This is possible only if X is singular. By eliminating the last column of X you can find that X is singular if and only if the matrix

Y = [x2-x1 y2-y1]
    [x3-x1 y3-y1]

是单数.

要在Matlab中可靠地检查矩阵的奇异性,可以使用SVD或等效的函数rank.因此,您的功能可以按以下方式实现:

To reliably check for the singularity of a matrix in Matlab, you can use SVD or, equivalently, the function rank. Hence your function could be implemented as follows:

function [result] = mylinecheck(x1,y1,x2,y2,x3,y3)

result = rank([x2-x1, y2-y1; x3-x1, y3-y1]) < 2;