2014-09-26 102 views
4

存在许多问题,已经涵盖如何检测线段和圆之间的碰撞。Matlab - 用于检测线段和圆之间的碰撞的函数失败

在我的代码,我使用Matlab的linecirc功能,然后比较它与我的线段的端部返回的交叉点,以检查点是线内(linecirc假定无限线,我不没有/想要)。

复制sprintf调用函数并将其添加到linecirc函数中可以看出它正在按照预期计算点。这些似乎正在被我的功能所迷失。

我的代码是下面:

function cutCount = getCutCountHex(R_g, centre) 
clf; 
cutCount = 0; 

% Generate a hex grid 
Dg = R_g*2; 
L_b = 62; 

range = L_b*8; 

dx = Dg*cosd(30); 
dy = 3*R_g; 
xMax = ceil(range/dx); yMax = ceil(range/dy); 
d1 = @(xc, yc) [dx*xc dy*yc]; 
d2 = @(xc, yc) [dx*(xc+0.5) dy*(yc+0.5)]; 

centres = zeros((xMax*yMax),2); 
count = 1; 

for yc = 0:yMax-1 
    for xc = 0:xMax-1 
     centres(count,:) = d1(xc, yc); 
     count = count + 1; 
     centres(count, :) = d2(xc, yc); 
     count = count + 1; 
    end 
end 

for i=1:size(centres,1) 
    centres(i,:) = centres(i,:) - [xMax/2 * dx, yMax/2 * dy]; 
end 

hold on 
axis equal 

% Get counter for intersected lines 
[VertexX, VertexY] = voronoi(centres(:,1), centres(:,2)); 
numLines = size(VertexX, 2); 
for lc = 1:numLines 
    segStartPt = [VertexX(1,lc) VertexY(1,lc)]; 
    segEndPt = [VertexX(2,lc) VertexY(2,lc)]; 
    slope = (segEndPt(2) - segStartPt(2))/(segEndPt(1) - segStartPt(1)); 
    intercept = segEndPt(2) - (slope*segEndPt(1)); 
    testSlope = isinf(slope); 
    if (testSlope(1)==1) 
     % Pass the x-axis intercept instead 
     intercept = segStartPt(1); 
    end 
    [xInterceptionPoints, yInterceptionPoints] = ... 
     linecirc(slope, intercept, centre(1), centre(2), L_b); 

    testArr = isnan(xInterceptionPoints); 
    if (testArr(1) == 0) % Line intersects. Line segment may not. 
     interceptionPoint1 = [xInterceptionPoints(1), yInterceptionPoints(1)]; 
     interceptionPoint2 = [xInterceptionPoints(2), yInterceptionPoints(2)]; 

     % Test if first intersection is on the line segment 
     p1OnSeg = onSeg(segStartPt, segEndPt, interceptionPoint1); 
     p2OnSeg = onSeg(segStartPt, segEndPt, interceptionPoint2); 
     if (p1OnSeg == 1) 
      cutCount = cutCount + 1; 
      scatter(interceptionPoint1(1), interceptionPoint1(2), 60, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'k'); 
     end 

     % Test if second intersection point is on the line segment 
     if (interceptionPoint1(1) ~= interceptionPoint2(1) || interceptionPoint1(2) ~= interceptionPoint2(2)) % Don't double count touching points 
      if (p2OnSeg == 1) 
       cutCount = cutCount + 1; 
       scatter(interceptionPoint2(1), interceptionPoint2(2), 60, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'k'); 
      end 
     end 
    end 
end 

% Plot circle 

viscircles(centre, L_b, 'EdgeColor', 'b'); 
H = voronoi(centres(:,1), centres(:,2)); 
for i = 1:size(H) 
    set(H(i), 'Color', 'g'); 
end 
end 

function boolVal = onSeg(segStart, segEnd, testPoint) 
bvX = isBetweenOrEq(segStart(1), segEnd(1), testPoint(1)); 
bvY = isBetweenOrEq(segStart(2), segEnd(2), testPoint(2)); 
if (bvX == 1 && bvY == 1) 
    boolVal = 1; 
else 
    boolVal = 0; 
end 
end 

function boolVal = isBetweenOrEq(end1, end2, test) 
    if ((test <= end1 && test >= end2) || (test >= end1 && test <= end2)) 
     boolVal = 1; 
    else 
     boolVal = 0; 
    end 
end 

它创建了一个六角形网格,然后计算具有固定半径(62在这种情况下)画出的圆与指定的中心之间的交叉数。

scatter调用显示函数计数的位置。 实施if(p1OnSeg == 1)块内sprintf呼叫指示我的功能已选择虚拟交叉点(虽然它然后与他们正确处理)

if (interceptionPoint1(1) > -26 && interceptionPoint1(1) < -25) 
       sprintf('p1 = [%f, %f]. Vx = [%f, %f], Vy = [%f, %f].\nxint = [%f, %f], yint = [%f, %f]',... 
        interceptionPoint1(1), interceptionPoint1(2), VertexX(1,lc), VertexX(2,lc), VertexY(1,lc), VertexY(2,lc),... 
        xInterceptionPoints(1), xInterceptionPoints(2), yInterceptionPoints(1), yInterceptionPoints(2)) 
      end 

输出

p1 = [-25.980762, 0.000000]. Vx = [-25.980762, -25.980762], Vy = [-15.000000, 15.000000]. 
xint = [-25.980762, -25.980762], yint = [0.000000, 0.000000] 

甲图为奇怪点。

enter image description here

对不起,很长的问题,但 - 在这些被检测的原因。它们不在圆上(显示mylinecirc函数中的值可以检测交点在(-25,55)和(-25,-55)左右(如无限长线所预期的那样)

正在移动圈子可以删除这些点,但有时这会导致其他问题的检测这是怎么回事

编辑:?旋转我的格子图案由[Vx, Vy] = voronoi(...)创建,然后删除点具有非常大的值(即那些去接近无穷大等)似乎已经解决了这个问题,为了避免出现在“斜率”和“截距”中的NaN值,“大”值点的去除似乎是必要的,我的猜测是这与可能的轻微倾斜有关。旋转,再加上期望的拦截溢出。

添加的示例代码如下。我也在Jan de Gier的代码中进行了编辑,但是这对问题没有任何影响,因此在问题代码中没有更改。

%Rotate slightly 
RotAngle = 8; 
RotMat = [cosd(RotAngle), -sind(RotAngle); sind(RotAngle), cosd(RotAngle)]; 

for i=1:size(centres,1) 
    centres(i,:) = centres(i,:) - [floor(xMax/2) * dx, floor(yMax/2) * dy]; %Translation 
    centres(i,:) = (RotMat * centres(i,:)'); %Rotation 
end 


% Get counter for intersected lines 
[VertexX, VertexY] = voronoi(centres(:,1), centres(:,2)); 

% Filter vertices 
numLines = size(VertexX, 2); 
newVx = []; 
newVy = []; 
for lc = 1:numLines 
    testVec = [VertexX(:,lc) VertexY(:,lc)]; 
    if ~any(abs(testVec) > range*1.5) 
     newVx = [newVx; VertexX(:,lc)']; 
     newVy = [newVy; VertexY(:,lc)']; 
    end 
end 
VertexX = newVx'; 
VertexY = newVy'; 
numLines = size(VertexX, 2); 

不断赞赏的答案或建议,以澄清为什么这是/正在发生。 示例值,导致这是getCutCountHex(30, [0,0])...(35, [0,0])

回答

1

我不能重现你的问题,但我没有通知的事情是,你的onSeg()函数可能是错误的:如果测试点在于矩形两则返回true四个角点是segStart和segEnd。

当且仅当一个点上,则返回true的函数(或更精确的:足以接近)的线段(segStart,segEnd)可以是:

function boolVal = onSeg(segStart, segEnd, testPoint) 

    tolerance = .5; 

    AB = sqrt((segEnd(1)-segStart(1))*(segEnd(1)-segStart(1))+(segEnd(2)-segStart(2))*(segEnd(2)-segStart(2))); 
    AP = sqrt((testPoint(1)-segEnd(1))*(testPoint(1)-segEnd(1))+(testPoint(2)-segEnd(2))*(testPoint(2)-segEnd(2))); 
    PB = sqrt((segStart(1)-testPoint(1))*(segStart(1)-testPoint(1))+(segStart(2)-testPoint(2))*(segStart(2)-testPoint(2))); 

    boolVal = abs(AB - (AP + PB)) < tolerance; 

end 

了一种方法,我在的一个发现anwers这里:Find if point lays on line segment。我希望能解决你的问题。

+1

你是正确的,这是一个点是否是线段更准确的评估,因此+1,但由于传递给这个函数的值是“应该”(虽然更好的安全比遗憾,所以改为你的函数)是点在相同的截距和斜率的无限线,如果该点是在矩形内,也就行 – chrisb2244 2014-09-29 06:15:03

+0

在再现的问题而言,调用'CC = getCutCountHex(30, [0,0])给出了错误的答案,但是......(33,[0,0])产生了图中可见的8个交点 – chrisb2244 2014-09-29 06:16:10