如何检查三个点是否形成一条直线? [英] How do I check to see if three points form a straight line?
问题描述
问题:创建具有六个输入的函数mylinecheck(a,b,c,d,e,f)
:
a,b,c,d,e,f
是实数,而a,c,e
不相等.该函数必须检查三个点(a,b)
,(c,d)
和(e,f)
是否都位于同一行上.如果是,则返回1.如果不是,则返回0.
Problem: Create the function mylinecheck(a,b,c,d,e,f)
which takes six inputs:
a,b,c,d,e,f
which are real numbers, and a,c,e
are not equal. The function must check if the three points (a,b)
, (c,d)
, and (e,f)
all lie on the same line. If so, return a 1. If not, return a 0.
我想我想做的就是告诉MATLAB检查坐标(c,d)
和(e,f)
是否为(a,b)
的倍数,如果不是,我将返回0.如果是,则将返回1.如果这是正确的思考过程,那么我不确定如何命令MATLAB这样做.任何建议将不胜感激.
I think what I want to do is tell MATLAB to check if coordinates (c,d)
and (e,f)
are multiples of (a,b)
, and then if not I will return a 0. If so, I will return a 1. If this is the right thought process, I'm not sure how to command MATLAB to do so. Any advice would be greatly appreciated.
推荐答案
(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
用于a
,b
和c
的固定值(我无法克服您的表示法;对混乱"表示抱歉),其中a
或b
为非零.因此,当且仅当
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;
这篇关于如何检查三个点是否形成一条直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!