在MATLAB中找到两个向量之间的交点 [英] Find point of intersection between two vectors in MATLAB

查看:1047
本文介绍了在MATLAB中找到两个向量之间的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的MATLAB问题.找到两个向量之间的交点的最简单方法是什么.我不熟悉各种MATLAB函数-似乎应该为此使用一个.

I have a very simple MATLAB question. What is the easiest way to find the point of intersection between two vectors. I am not familiar with the various MATLAB functions -- it seems like there should be one for this.

例如,如果我有一个向量从(0,0)到(6,6),另一个向量从(0,6)到(6,0),我需要确定它们在(3)处相交, 3).

For example, if I have one vector from (0,0) to (6,6) and another vector from (0,6) to (6,0), I need to determine that they intersect at (3,3).

推荐答案

一种解决方案是使用

One solution is to use the equations derived in this tutorial for finding the intersection point of two lines in 2-D (update: this is an internet archive link since the site no longer exists). You can first create two matrices: one to hold the x coordinates of the line endpoints and one to hold the y coordinates.

x = [0 0; 6 6];  %# Starting points in first row, ending points in second row
y = [0 6; 6 0];

上述来源的方程式可以如下进行编码:

The equations from the above source can then be coded up as follows:

dx = diff(x);  %# Take the differences down each column
dy = diff(y);
den = dx(1)*dy(2)-dy(1)*dx(2);  %# Precompute the denominator
ua = (dx(2)*(y(1)-y(3))-dy(2)*(x(1)-x(3)))/den;
ub = (dx(1)*(y(1)-y(3))-dy(1)*(x(1)-x(3)))/den;

现在您可以计算两条线的交点:

And you can now compute the intersection point of the two lines:

xi = x(1)+ua*dx(1);
yi = y(1)+ua*dy(1);

对于问题中的示例,上面的代码按预期给出了xi = 3yi = 3.如果要检查交点是否位于线的端点之间 (即它们是有限线 segments ),则只需检查值uaub都在0到1之间:

For the example in the question, the above code gives xi = 3 and yi = 3, as expected. If you want to check that the intersection point lies between the endpoints of the lines (i.e. they are finite line segments), you just have to check that the values ua and ub both lie between 0 and 1:

isInSegment = all(([ua ub] >= 0) & ([ua ub] <= 1));

我在上面链接的本教程中的其他几点:

A couple more points from the tutorial I linked to above:

  • 如果分母den为0,则这两行是平行的.
  • 如果uaub的等式的分母和分子均为0,则这两行重合.
  • If the denominator den is 0 then the two lines are parallel.
  • If the denominator and numerator for the equations for ua and ub are 0 then the two lines are coincident.

这篇关于在MATLAB中找到两个向量之间的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆