本文介绍了与参考线成直角的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周前,我收到一段脚本,我认为它会计算一个点与参考线(以直角)的距离,并返回沿参考线的距离以及从参考线到该距离的距离点。说实话,这是我的想法,但我并不完全理解脚本,即使它只有26行。



因为我认为脚本是问题是为什么另一个依赖于它的脚本变得不稳定我认为它真的很方便真正理解正在发生的事情。我曾希望有人能够帮助我。



脚本(用Matlab编写,但如果需要,我也可以用python获得):


$ b $

 函数[sp,np] = locate(s,x,y,xp,yp)
ipp = 0;
ns =长度(s);
为ip = 1:length(xp); (i(i + 2)-x(i))/(s(i + 2)-s(i));对于i = 1的
:ns-2
cosa =
sina =(y(i + 2)-y(i))/(s(i + 2)-s(i)); (i)+(xp(ip)-x(i))* cosa +(yp(ip)-y(i))* sina;
sproj = s
if sproj ipp = ipp + 1;
sp(ipp)= sproj;
np(ipp)= - (xp(ip)-x(1))* sina +(yp(ip)-y(1))* cosa;
break
elseif sproj> = s(i)& sproj< = s(i + 2)
ipp = ipp + 1;
sp(ipp)= sproj;
np(ipp)= - (xp(ip)-x(i))* sina +(yp(ip)-y(i))* cosa;
break
elseif sproj> s(ns)
ipp = ipp + 1;
sp(ipp)= sproj;
np(ipp)= - (xp(ip)-x(ns))* sina +(yp(ip)-y(ns))* cosa;
break
end
end
end

s是沿着参考线的距离,x和y是参考线上的x和y点,xp和yp是到线的距离(以直角)的点的坐标以及沿着参考线的距离需要计算参考线。该脚本如下所示:

  dist(1)= 0; 
for i = 2:length(xref);
dist(i)= dist(i-1)+ sqrt((xref(i)-xref(i-1))^ 2+(yref(i)-yref(i-1))^ 2) ;
end

%%创建计算网格
ds =(dist(end)-dist(1))/(ns-1); %stepsize
s = 0:ds:dist(end); %距离
xr =样条(dist,xref,s);线格点的%x
yr =样条线(dist,yref,s); %y的线格点

%%初始线的计算位置
[si,ni] = locate(s,xr,yr,xi,yi);
n = interp1(si,ni,s,'linear','extrap');直角距离变化的百分比

有没有人可以通过解释第一次发生的事情来帮助我?如果我明白你要找的是什么,这是我的理由,但我不能真正理解它。 是这样的。



这个函数有三个点,前两个是x值的向量和y值的向量(我会称它们为p1和p2)和第三点(p3)。该函数返回从p3到与p1& p2相交的线的最小距离以及从(p1到交点)或(p2到交点)的距离的最小值。



示例:

定义行的两点是(1,2)和(5,10)。定义它们的线将是y = 2x。如果第三点是(1,0)。垂线是
y = - (x-1)/ 2。两条线的交点为(2/5,1/5)。
定义参考线的最近点为(1,2),从(2/5,1 / 5)到(1,2)的距离为1.7,距离(2/5,1 / 5)至(1,0)为0.89。该功能将返回[1.7,0.89]。



请让我知道这是你想要的。

<$ (x,y,xP,yP)
%StackExchange:计算交叉线(x(1),y(1))& (x(2),y(2))
%找到相交(xP,yP)的垂直线,其出现为
%(IP(1),IP(2)),然后计算最小距离从参考点
%(x(1),y(1))或(x(2),y(2))到(IP(1),IP(2))
%和从(IP(1),IP(2))到(xP,yP)

%计算参考线的斜率,垂线
%的斜率为-1 / m
m = diff(y)./ diff(x);

IP(1)是交点的x坐标
%此公式正在求解方程
%m *(x - x(1))+ y (1)=(m ^ 2 * x(1)+ xP + m *(yP)(1)= - (1 / m)*(x - xP)+ yP
% - y(1)))./(m ^ 2 + 1);
%IP(2)是交点
IP(2)= m *(IP(1) - x(1))+ y(1)的y坐标。

%从参考点到交点的最小距离
dRef = sqrt(min((IP(1) - x)。^ 2 +(IP(2) - y)。 ^ 2));

%从交点到兴趣点的距离
dInt = sqrt(sum(([xP,yP] - IP)。^ 2));

结束


A few weeks back I received a script that, I think, calculates the distance of a point from a reference line (at right angle) and returns the distance along the reference line as well as the distance from the reference line to that point. To be honest, this is what I think it does, however I do not fully understand the script even though it is only 26 lines long.

Since I think the script is the problem why another script, which depends on it, gets unstable I figured it is actually handy to really understand what is going on. I had hoped someone could help me with this.

The script (written in Matlab but I've also got it in python if necessary):

function [sp,np]=locate(s,x,y,xp,yp)
ipp=0;
ns=length(s);
for ip=1:length(xp);
    for i=1:ns-2
        cosa=(x(i+2)-x(i))/(s(i+2)-s(i));
        sina=(y(i+2)-y(i))/(s(i+2)-s(i));
        sproj=s(i)+(xp(ip)-x(i))*cosa+(yp(ip)-y(i))*sina;
        if sproj<s(1)
            ipp=ipp+1;
            sp(ipp)=sproj;
            np(ipp)=-(xp(ip)-x(1))*sina+(yp(ip)-y(1))*cosa;
            break
        elseif sproj>=s(i)&sproj<=s(i+2)
            ipp=ipp+1;
            sp(ipp)=sproj;
            np(ipp)=-(xp(ip)-x(i))*sina+(yp(ip)-y(i))*cosa;
            break
        elseif sproj>s(ns)
            ipp=ipp+1;
            sp(ipp)=sproj;
            np(ipp)=-(xp(ip)-x(ns))*sina+(yp(ip)-y(ns))*cosa;
            break
        end
    end
end

Here s is the distance along the reference line, x and y are the x and y points on the reference line and xp and yp are the coordinates of the points of which the distance to the line (at right angle) as well as the distance along the reference line need to be calculated. The script is called as follows:

dist(1)=0;
for i=2:length(xref);
    dist(i)=dist(i-1)+sqrt((xref(i)-xref(i-1))^2+(yref(i)-yref(i-1))^2);
end

%% Create computational grid
ds=(dist(end)-dist(1))/(ns-1);  % stepsize
s=0:ds:dist(end);               % distance
xr=spline(dist,xref,s);            % x of line grid points
yr=spline(dist,yref,s);            % y of line grid points

%% Compute locations of initial line
[si,ni]=locate(s,xr,yr,xi,yi);
n=interp1(si,ni,s,'linear','extrap');    % distance change at right angle

Is there anyone that can help me by explaining what exactly happens in the first script, cause I can't really get my head around it.

解决方案

If I understand what you're looking for, this is it.

This function takes in three points, the first two as a vector of x values and a vector of y values (I'll call them p1 and p2) and a third point (p3). The function returns the minimum distance from p3 to the line intersecting p1&p2 and the minimum of the distance from (p1 to the intersection point) or (p2 to the intersection point).

Example:

The two points to define the line are (1,2) and (5,10). The line defining them would be y = 2x. If the third point were (1,0). The perpendicular line isy = -(x-1)/2. The intersection point of the two lines would be (2/5, 1/5).The closest point defining the reference line would be (1,2) and the distance from (2/5, 1/5) to (1,2) is 1.7 and the distance from (2/5, 1/5) to (1,0) is 0.89. The function would return [1.7, 0.89].

Please let me know if this is what you want.

function [ dRef, dInt ] = StackExchange( x, y, xP, yP )
%StackExchange: Calculate line intersecting (x(1), y(1)) & (x(2), y(2))
%finds perpendicular line which intersects (xP, yP) which occurs as
%(IP(1), IP(2)) then calculated the minimum distance from the reference points
%(x(1), y(1)) or (x(2), y(2)) to (IP(1), IP(2))
%and from (IP(1), IP(2)) to (xP, yP)

%Calculate  slope of reference line, the slope of the perpendicular line
%will be -1/m
m = diff(y) ./ diff(x) ;

%IP(1) is the x-coordinate of the intersection point
%This formula is solving the equation
% m * (x - x(1)) + y(1) = -(1/m) * (x - xP) + yP
%for x
IP(1) = (m^2 * x(1) + xP + m * (yP - y(1))) ./ (m^2 + 1);
%IP(2) is the y-coordinate of the intersection point
IP(2) = m * (IP(1) - x(1)) + y(1);

%Minimum distance from the reference points to the intersection point
dRef = sqrt(min((IP(1) - x).^2 + (IP(2) - y).^2));

%Distance from the intersection point to  point of interest
dInt = sqrt(sum(([xP, yP] - IP).^2));

end

这篇关于与参考线成直角的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 10:07