一条折线的哪一侧是一个点。

4
我正在尝试编写代码,以便识别随机折线(非闭合)上的随机点位于哪一侧。折线被认为是无限的,第一个和最后一个线段延伸到无穷远。如果它们相交,则应将其视为多边形(我尚未为此情况编写代码)。逻辑如下:
1. 定义折线的顶点,距离待考虑点最近。变量minimum2p是到该顶点的距离,I2p是该顶点的索引。
2. 定义折线的线段,距离待考虑点最近。仅计算与从待考虑点垂直相交的那些线段(变量count_s)。变量minimum2s是到线段的最小距离;I2p是该线段第一个顶点的索引;flag是布尔变量,存储有关相交垂线的信息。

3. 接下来只需要选择适当的片段进行比较,例如使用链接link-1link-2link-3中的想法。我尝试了这里的方法,但它在许多特殊情况下都不起作用。对于折线的内部点,我使用最佳答案。因此,我的方法如下:

4. 首先,检查它是折线的第一个还是最后一个顶点。如果是这种情况,则所选的段要么是第一个要么是最后一个,但前提是没有其他比第一个或最后一个更接近的段。如果有另一个段,则我选择那个段。

5. 接下来,如果步骤4不成立,我会检查折线的内部顶点。如果附近也有一个接近的线段,那么我将比较索引I2pI2s(如果后者存在)。如果它们相同,则在选择要比较的正确线段时没有歧义。如果它们不同,则更偏爱最靠近的线段而不是最靠近的顶点。

6. 最后,如果附近没有线段(从穿过线段的点的垂直意义上),则对于内部顶点,我会应用这里的最佳答案的想法(链接)

这里是不同折线的结果,它们由其顶点的X和Y坐标定义,分别存储在'polylineX'和'polylineY'中(红色表示“左”位置,灰色表示“右”位置,黑色表示折线上的位置,蓝色线表示折线)。

您可以注意到,对于相对平滑的折线,代码可以正常工作。但是,对于更锐利或某些方面复杂的折线,代码无法正常工作。我在我的代码中漏掉了什么?应该添加什么条件以考虑某些情况?

代码如下:

clear all
close all 
clc
clf
polylineX = [0 1 2 3 4 5 6 7 8];
polylineY = [-10 20 -13 18 -17 16 -21 23 -25];
hold on
title(['polylineX=[',num2str(polylineX),'], polylineY=[',num2str(polylineY),']'])
chosen = 0;
span = 60;

for ii = 10:70
for jj = 30:60
    ii
    jj
    position = -2;
    point = [(jj-round(span/2))/1 (ii-round(span/2))/1];

    axis equal
    plot(polylineX,polylineY,'.-','MarkerSize',1,'LineWidth',1);

    distance2p = zeros(1,length(polylineX)); % distances from the point to the points (2p) of the polyline
    distance2s = zeros(1,length(polylineX)-1); % distances from the point to the segments (2s) of the polyline
    flag = zeros(1,length(polylineX)-1);
    count_s = 0; % counter of segments, which are intersected by the normal pointing from the 'point'
    k = 0;

    for i = 1:length(polylineX)-1
        pos = sign((polylineX(i+1) - polylineX(i)) * (point(2) - polylineY(i)) -...
                   (polylineY(i+1) - polylineY(i)) * (point(1) - polylineX(i)));

        % computing the distances from the 'point' to all segments and mark if
        % the distance vectors intersect the segments
        [flag(i),distance2s(i)] = distanceToLine([polylineX(i) polylineX(i+1)],[polylineY(i) polylineY(i+1)],[point(1) point(2)]);

        if flag(i)
            if k == 0
                minimum2s = distance2s(i);
                I2s = i;
            end;
            k = 1;
            count_s = count_s + 1; % count segments, which are intersected by the normal pointing from the 'point'
            if distance2s(i) < minimum2s
                I2s = i;
                minimum2s = distance2s(i);
            end;
        end;
    end;


    % first compute the distances between the 'point' under consideration and the
    % points of the given polyline
    for i  = 1:length(polylineX)
        distance2p(i) = sqrt((point(1)-polylineX(i))^2+(point(2)-polylineY(i))^2);
    end;
    [minimum2p,I2p] = min(distance2p);
    clear k pos i

    % now we need to choose which segment of the polyline to compare our 'point' with. These
    % segments are either adjacent to that point of the polyline, which is the closest
    % to the 'point' of interest, or the closest to the 'point' segment, which
    % has an intersection with the normale pointing from the 'point'.

    if I2p == 1 % if the 'point' is near the start of polyline
        if exist('minimum2s','var')
            if I2p == I2s
                chosen = I2p;
            else
                chosen = I2s;
            end;
        else
            chosen = I2p;
        end;

    elseif I2p == length(polylineX) % if the 'point' is near the end of polyline
        if exist('minimum2s','var')
            if I2s == I2p-1
                chosen = I2p - 1;
            else
                chosen = I2s;
            end;
        else
            chosen = I2p - 1;
        end;
    else
        if exist('minimum2s','var')
            if I2p == I2s
                chosen = I2p;

            else
                chosen = I2s;
            end;
        else
                pos1 =  sign((polylineX(I2p) - polylineX(I2p-1)) * (point(2) - polylineY(I2p-1)) -...
                 (polylineY(I2p) - polylineY(I2p-1)) * (point(1) - polylineX(I2p-1)));
                % position of the second segment relative to the first segment
                pos2 =  sign((polylineX(I2p) - polylineX(I2p-1)) * (polylineY(I2p+1) - polylineY(I2p-1)) -...
                             (polylineY(I2p) - polylineY(I2p-1)) * (polylineX(I2p+1) - polylineX(I2p-1)));
                if (pos1 == 1 && pos2 == 1) || (pos1 == -1 && pos2 == -1)
                    chosen = I2p;
                elseif pos1 == 0 || pos2 == 0
                    chosen = I2p;
                else
                    chosen = I2p - 1;
                end;
        end;
    end;

    position = sign((polylineX(chosen+1) - polylineX(chosen)) * (point(2) - polylineY(chosen)) -...
                    (polylineY(chosen+1) - polylineY(chosen)) * (point(1) - polylineX(chosen)));

    if position == 1
        plot(point(1),point(2),'r.','MarkerSize',5)
    elseif position == -1;
        plot(point(1),point(2),'.','Color',[0.9 0.9 0.9],'MarkerSize',5) % gray color
    elseif position == 0
        plot(point(1),point(2),'k.','MarkerSize',5)
    elseif position == -2
        plot(point(1),point(2),'g.','MarkerSize',5)
    end;

    pause(0.00000001)
    clear chosen  count_s distance2p distance 2s flag I2p I2s minimum2p minimum2s point pos1 pos2 position

end;
end;

2
“左侧”和“右侧”是如何定义的?您可能更想考虑“与另一个点在同一侧”。为此,请定义一些任意参考点(不在该直线上),并计算连接线与折线相交的次数(类似于检查点是否包含在多边形内)。 - Nico Schertler
这个查找旋转后线/点左侧的点的位置或许能有所帮助。 - Spektre
折线的方向由前一个顶点到下一个顶点的向量定义。如果您站在第一个顶点面向下一个顶点,那么左侧在您的左边,右侧在您的右边。 - Capo Pavel Mestre
4个回答

2

我的朴素想法是将点到线的角度进行整合。从无限远处的一侧开始积分,然后经过所有点到达另一侧的无限远处。这只是一堆atan2函数。

曲线的侧面是由积分符号的正负确定的。即使曲线重叠,这种方法也应该有效。

在Python中:

from math import atan2,pi
#import matplotlib.pyplot as plt

# difference of two angles should be always -pi < x < pi
def fixed_angle(a):
    if a > pi:
       return a - 2*pi
    elif a < (-1*pi):
       return a + 2*pi
    assert(-1*pi < a < pi) # just check, to be sure
    return a

# polyline
xs = [0, 1, 2, 3, 4, 5, 6, 7, 8];
ys = [-10, 20, -13, 18, -17, 16, -21, 23, -25];

# start point
x = 4
y = 0

#from first two points
angle_start = atan2(ys[0]-ys[1],xs[0]-xs[1])
#last angle is angle of last section
angle_end = atan2(ys[-1]-ys[-2],xs[-1]-xs[-2]) 

integ = 0
prev =  angle_start
for i in range(len(xs)):
    a = atan2(ys[i]-y,xs[i]-x)
    integ += fixed_angle(a-prev)
    prev = a

integ += fixed_angle(angle_end - prev)

if integ > 0:
    print("point is left")
else:
    print("point is right")

#plt.plot(xs,ys)
#plt.show()

1
一种不计算角度的方法是考虑测试点和连续折线段所形成的三角形的有向面积。
可以通过一种约定来理解三角形的有向面积,即其中一个顶点在相对线段的“左侧”或“右侧”。对于三个顶点(xi, yi),其计算公式为0.5 * (-x2*y1 + x3*y1 + x1*y2 - x3*y2 -x1*y3 + x2*y3)。关键是,遍历顶点的顺序会影响符号。
假设折线段是一个列表[v1, v2, ...., vn]。对于每个相邻顶点(v_i, v_(i+1)),计算三角形(v_i, 测试点, v_(i+1))的有向面积。似乎需要关注最小绝对面积的三角形:也就是说,该点“最接近”折线段的三角形。根据您的约定,考虑其有向面积,判断该三角形是否在折线段的左侧或右侧。

*编辑-零面积三角形意味着您的测试点与折线段共线; 您需要单独测试它是否实际上在该段上。


1
如何考虑一种基于“绕数”概念的替代方案?这将涉及计算您边界折线段所形成的所有角度总和,从测试点的视角来看。
如果您想象一下,通过在无限远处加上一个半圆来延长这个折线,以形成一个封闭轮廓,则这些角度之和对于轮廓外的点为零,对于轮廓内的点为2 * Pi。显然,半圆是在左半平面还是右半平面将决定哪一侧是“内部”或“外部”。如果从角度总和中排除半圆本身,因为它会贡献+/-Pi,则线的一侧应具有+Pi的角度总和,而另一侧应给出-Pi。它是+Pi还是-Pi取决于折线顶点序列定义的方向。

这种方法的一个微妙之处在于计算折线每个线段所形成的(有符号)角度。我认为可以通过查看连接测试点和给定线段两端的两个向量来完成此操作。通过对这些向量进行点积(并在除以它们的长度后),您可以计算出该线段所围角度的余弦值。取反余弦将在正负角度之间产生歧义。但是,通过对两个向量进行叉积(将它们的z分量视为零),您可以计算出角度的正弦值,其符号将允许您计算出反余弦的正确分支。


0

enter image description here

我的解决方案在图中有详细说明。

  1. 将点(例如P1)投影到折线[O1,O2,O3,O4]上,投影点为P'1,位于线段O1O2上。
  2. 如果O1P'1P1和P1P'1O2都是逆时针排列的,则点P1位于折线的左侧。
  3. 如果点(例如P2)位于折线的右侧,则O3P'2P2和P2P'2O4都是顺时针排列的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接