声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3865|回复: 11

[编程技巧] 诸位 请问怎么可以求得两条直线的交点坐标

[复制链接]
发表于 2011-4-18 11:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
我需要求得两条直线的交点坐标的具体数值
我该怎么做呢 请诸位指点一下
回复
分享到:

使用道具 举报

发表于 2011-4-18 11:12 | 显示全部楼层
可否考虑使用两直线的数学表述,用matlab解方程求出交点!
 楼主| 发表于 2011-4-18 12:04 | 显示全部楼层
回复 2 # 雨人 的帖子

这个也可以 但是有没有什么更简单点的方法
发表于 2011-4-18 15:21 | 显示全部楼层
aa=solve('y=x','y-2=-x');
[aa.x, aa.y]

评分

1

查看全部评分

发表于 2011-4-18 21:22 | 显示全部楼层
本帖最后由 zhouyang664 于 2011-4-19 22:11 编辑

你看看这个行不行?

function P = InterX(L1,varargin)
%INTERX Intersection of curves
%   P = INTERX(L1,L2) returns the intersection points of two curves L1
%   and L2. The curves L1,L2 can be either closed or open and are described
%   by two-row-matrices, where each row contains its x- and y- coordinates.
%   The intersection of groups of curves (e.g. contour lines, multiply
%   connected regions etc) can also be computed by separating them with a
%   column of NaNs as for example
%
%         L  = [x11 x12 x13 ... NaN x21 x22 x23 ...;
%               y11 y12 y13 ... NaN y21 y22 y23 ...]
%
%   P has the same structure as L1 and L2, and its rows correspond to the
%   x- and y- coordinates of the intersection points of L1 and L2. If no
%   intersections are found, the returned P is empty.
%
%   P = INTERX(L1) returns the self-intersection points of L1. To keep
%   the code simple, the points at which the curve is tangent to itself are
%   not included. P = INTERX(L1,L1) returns all the points of the curve
%   together with any self-intersection points.
%   
%   Example:
%       t = linspace(0,2*pi);
%       r1 = sin(4*t)+2;  x1 = r1.*cos(t); y1 = r1.*sin(t);
%       r2 = sin(8*t)+2;  x2 = r2.*cos(t); y2 = r2.*sin(t);
%       P = InterX([x1;y1],[x2;y2]);
%       plot(x1,y1,x2,y2,P(1,:),P(2,:),'ro')

%   Author : NS
%   Version: 3.0, 21 Sept. 2010

%   Two words about the algorithm: Most of the code is self-explanatory.
%   The only trick lies in the calculation of C1 and C2. To be brief, this
%   is essentially the two-dimensional analog of the condition that needs
%   to be satisfied by a function F(x) that has a zero in the interval
%   [a,b], namely
%           F(a)*F(b) <= 0
%   C1 and C2 exactly do this for each segment of curves 1 and 2
%   respectively. If this condition is satisfied simultaneously for two
%   segments then we know that they will cross at some point.
%   Each factor of the 'C' arrays is essentially a matrix containing
%   the numerators of the signed distances between points of one curve
%   and line segments of the other.

    %...Argument checks and assignment of L2
    error(nargchk(1,2,nargin));
    if nargin == 1,
        L2 = L1;    hF = @lt;   %...Avoid the inclusion of common points
    else
        L2 = varargin{1}; hF = @le;
    end
      
    %...Preliminary stuff
    x1  = L1(1,:)';  x2 = L2(1,:);
    y1  = L1(2,:)';  y2 = L2(2,:);
    dx1 = diff(x1); dy1 = diff(y1);
    dx2 = diff(x2); dy2 = diff(y2);
   
    %...Determine 'signed distances'   
    S1 = dx1.*y1(1:end-1) - dy1.*x1(1:end-1);
    S2 = dx2.*y2(1:end-1) - dy2.*x2(1:end-1);
   
    C1 = feval(hF,D(bsxfun(@times,dx1,y2)-bsxfun(@times,dy1,x2),S1),0);
    C2 = feval(hF,D((bsxfun(@times,y1,dx2)-bsxfun(@times,x1,dy2))',S2'),0)';

    %...Obtain the segments where an intersection is expected
    [i,j] = find(C1 & C2);
    if isempty(i),P = zeros(2,0);return; end;
   
    %...Transpose and prepare for output
    i=i'; dx2=dx2'; dy2=dy2'; S2 = S2';
    L = dy2(j).*dx1(i) - dy1(i).*dx2(j);
    i = i(L~=0); j=j(L~=0); L=L(L~=0);  %...Avoid divisions by 0
   
    %...Solve system of eqs to get the common points
    P = unique([dx2(j).*S1(i) - dx1(i).*S2(j), ...
                dy2(j).*S1(i) - dy1(i).*S2(j)]./[L L],'rows')';
              
    function u = D(x,y)
        u = bsxfun(@minus,x(:,1:end-1),y).*bsxfun(@minus,x(:,2:end),y);
    end
end

点评

抱歉没仔细看! 是怕原创评分给少了!  发表于 2011-4-20 22:24
我觉得注释中有作者,然后我就没注明,失误了! % Author : NS % Version: 3.0, 21 Sept. 2010  发表于 2011-4-19 22:10
原创??  发表于 2011-4-18 22:25

评分

1

查看全部评分

 楼主| 发表于 2011-4-20 19:04 | 显示全部楼层
回复 5 # zhouyang664 的帖子

谢谢诸位的帮助
发表于 2011-4-20 20:11 | 显示全部楼层
回复 6 # 尚慧娟 的帖子

楼主解决了问题,可否分享下自己的方法!
发表于 2011-4-21 10:49 | 显示全部楼层
楼主分享啊~~~~~
发表于 2011-4-21 10:50 | 显示全部楼层
二元一次方程组 你懂的
发表于 2011-4-24 07:59 | 显示全部楼层
用参数方程表达直线,然后形成方程组,解方程组,程序十分简单!
发表于 2011-4-24 14:34 | 显示全部楼层
回复 1 # 尚慧娟 的帖子

使两条直线的方程相减等于0,然后求解方程
 楼主| 发表于 2011-5-6 19:46 | 显示全部楼层
回复 11 # yxlnbu 的帖子

谢谢诸位了 问题已经解决了
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-28 16:09 , Processed in 0.061305 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表