平面俩线段求交点(坐标系转换实现 Rotation Matrix)MATLAB

平面内任意生成俩线段L1 ,L2  编程判断两条线段是否相交,若相交,求出交点。

用转换坐标系的思路,Rotation Matrix:

  1. 将一根线段,变成新的坐标系的X轴。
  2. 判断第二根线段是否穿过新X轴(第一条线段所在直线)。
  3. 没有穿过说明俩线段没有交点。
  4. 如果穿过,再判断重合点位置分析是否俩线段有交点并求交点坐标。
    1. 最后将交点坐标转换为到主坐标系。

 

使用公式(公式适用3维,这里只用到2维平面 [3*1]=[3*3]*[3*1])

可以实现,新 老 坐标转换

 MATLAB CODE:

clear;clc;

% 思路:将一根线段,变成新的坐标系的X轴,判断第二根线段是否与X轴有重合。
% 再判断重合点位置分析是否有交点或求交点坐标

x1 = [1,10]; y1 = [1,15];    % 线段一  任意设置
x2 = [-5,23]; y2 = [3,6];    % 线段二  任意设置

plot(x1,y1,'-*b',x2,y2,'-or'); %'-*b'——>线性,颜色,标记
hold on

alpha = atand(abs(y1(1)-y1(2))/abs(x1(1)-x1(2)));  % 线段一与x轴夹角  度

% 根据不同情况转换坐标系生成 vec(3*3)新坐标系原点为线段1其中一个端点
%
% Rotation Matrix (2D): [x,y,1]' = vec * [x_new,y_new,1]'
%
%  vec = [dot(X1,X), dot(Y1,X), x;       X1(线段1),Y1为新坐标系坐标向量
%         dot(X1,Y), dot(Y1,Y), y;       X,Y 为原始坐标系坐标向量
%         0        , 0        , 1]       x,y 为线段1其中一个端点坐标
%
% 自定义选择原则:线段1端点x坐标小的为新坐标原点
if x1(1)<x1(2)  
    if y1(1)<y1(2)
        vec = [cosd(alpha),-sind(alpha),x1(1);
               sind(alpha),cosd(alpha),y1(1);
               0,0,1];
    else
        vec = [cosd(alpha),sind(alpha),x1(1);
               -sind(alpha),cosd(alpha),y1(1);
               0,0,1];
    end
elseif x1(1)>x1(2)
    if y1(2)<y1(1)
        vec = [cosd(alpha),-sind(alpha),x2(1);
               sind(alpha),cosd(alpha),y2(1);
               0,0,1];
    else
        vec = [cosd(alpha),sind(alpha),x2(1);
               -sind(alpha),cosd(alpha),y2(1);
               0,0,1];
    end
else
    sprintf("points coincide");
end

% Rotation Matrix (2D): [x,y,1]' = vec * [x_new,y_new,1]'
% 生成第二个线段在新坐标的位置 
p1 = inv(vec)*[x2(1);y2(1);1];   % 新坐标[x_new,y_new,1]'
p2 = inv(vec)*[x2(2);y2(2);1];

% 判断交点
if p1(2)*p2(2)>0   % 俩点同向无交点
    sprintf("no intersection point")
else   % 求线段2所在直线与新x轴交点
    k=((p1(2)-p2(2))/(p1(1)-p2(1)));
    b=p1(2)-k*p1(1);
    iPoint=-b/k;   % 新x轴交点坐标
    if iPoint<0  % 判断交点是否在线段1上
        sprintf("no intersection point")
    else
        if iPoint > sqrt((y1(1)-y1(2))^2+(x1(1)-x1(2))^2)
            sprintf("no intersection point")
        else   % 如果在,求其点并转换为老坐标系坐标
            oldPoint = vec*[iPoint;0;1];
            plot(oldPoint(1),oldPoint(2),'*y')
            sprintf("intersection point: %i, %i",oldPoint(1),oldPoint(2))
        end
    end
end

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
可以使用以下的C#代码来两条线段交点: ```csharp using System; class Program { static void Main(string[] args) { // 定义线段的起点和终点坐标 double x1 = 1, y1 = 1; double x2 = 4, y2 = 5; double x3 = 2, y3 = 3; double x4 = 5, y4 = 7; // 计算线段交点 double[] intersectionPoint = GetIntersectionPoint(x1, y1, x2, y2, x3, y3, x4, y4); // 输出交点坐标 Console.WriteLine("Intersection Point: ({0}, {1})", intersectionPoint[0], intersectionPoint[1]); Console.ReadLine(); } static double[] GetIntersectionPoint(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double[] intersectionPoint = new double[2]; // 计算线段的参数 double dx1 = x2 - x1; double dy1 = y2 - y1; double dx2 = x4 - x3; double dy2 = y4 - y3; // 计算两条线段的斜率 double slope1 = dy1 / dx1; double slope2 = dy2 / dx2; // 计算两条线段的截距 double intercept1 = y1 - slope1 * x1; double intercept2 = y3 - slope2 * x3; // 若两条线段平行,则没有交点 if (slope1 == slope2) { Console.WriteLine("No intersection point"); return intersectionPoint; } // 计算交点的x坐标 double intersectionX = (intercept2 - intercept1) / (slope1 - slope2); // 计算交点的y坐标 double intersectionY = slope1 * intersectionX + intercept1; // 将交点坐标存储到数组中 intersectionPoint[0] = intersectionX; intersectionPoint[1] = intersectionY; return intersectionPoint; } } ``` 这段代码通过两条线段的斜率和截距,然后利用斜截式方程计算交点的坐标。如果两条线段平行,则没有交点。运行以上代码将输出交点的坐标。请根据实际情况修改线段的起点和终点坐标。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值