如何在MATLAB上找到从点到边缘的距离

时间:2017-08-04 23:56:04

标签: matlab

我的任务是创建一个MATLAB脚本来确定从中心(由黑点表示)到数据点(表示为蓝点)的距离。确定距离后,我必须确定蓝点和黑线之间的差异。重要的是,当确定蓝点和黑线之间的差异时,来自中心的矢量具有相同的方向。

我目前仍然坚持这一部分并且正在寻求任何建议来帮助我开始。

真的很感激任何帮助。

enter image description here

1 个答案:

答案 0 :(得分:1)

我将提供一个解决方案,假设你不能保证一条曲线中的每个坐标都有另一条曲线上的对应物落在同一条中心的光线上。

令:

origin % Center coordinate
c1 % N x 2 matrix where each row in a coordinate for curve 1
c2 % N x 2 matrix where each row in a coordinate for curve 2

方法

% Vectors from center point to each point on both curves
v1 = c1 - repmat(origin, size(c1, 1), 1)
v2 = c2 - repmat(origin, size(c2, 1), 1)

% Angles made by each coordinate with respect to center point
a1 = atan2(v1(:, 2), v1(:, 1))
a2 = atan2(v2(:, 2), v2(:, 1))

% Understand that, two points that pass through the same infinite ray 
% from the origin would have the same angle. Hence, we need to find all
% pairs of coordinates based on where `a1` and `a2` have the same value.

dists = zeros(size(c1, 1), 1)
matchingPoints = zeros(size(c1, 1), 2); % Just for extra information.

for i = 1:len(c1)
    matchInd = find(a2 == a1(i))
    % Can also use some error margin to overcome floating-point rounding-off errors.
    % Example: matchInd = find(abs(a2 - a1(i)) <= 1e-6)

    if isempty(matchInd)
        % Basically there was no point along that ray in second curve
        dists(i) = NaN;
        matchingPoints(i, :) = [NaN, NaN];
    else
        dists(i) = norm(c1(i) - c2(matchInd))
        matchingPoints(i, :) = c2(matchInd, :);
    end
end

最后,位于同一射线上的两条曲线上的一对点分别位于c1matchingPoints中,相应的距离位于dists