确定用户单击的是多边形内部还是外部 [英] Determine if the user clicked inside the polygon or outside of it

查看:71
本文介绍了确定用户单击的是多边形内部还是外部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Matlab中编写一个脚本,该脚本填充多边形并在用户单击多边形内部时在输出窗口中显示内部",而在用户单击多边形外部时在输出窗口中外部"显示.由于某些原因,即使我在多边形外部单击,它也会在内部打印.我把我的代码放在下面.

I am trying to write a script in Matlab that fills a polygon and prints "inside" to the output window when the user clicks inside the polygon and "outside" to the output window when the user clicks outside the polygon. For some reason it prints inside even when I click outside the polygon. I have put my code below.

xv = [ -3 3 3 -3]; %// x coords of polygon vertices. Arbitrary number
yv = [-5 -5 7 7]; %// y coords of polygon vertices. Same number as for x
fill(xv,yv,'b') %// draw polygon
axis([-10 10 -10 10])
[xp, yp] = ginput(1); %// get point coordinates
inside = inpolygon(xp,yp,xv,yv); %// is it inside?

while inside
    fprintf('Inside\n')
    [xp, yp] = ginput(1);
end
    fprintf('Outside\n')

推荐答案

代码缺少while循环中的行以重置inside的值,并检查用户输入是否仍在多边形中:

The code is missing the line in the while loop to reset the value of inside, checking to see if the user input is still in the polygon:

while inside
    fprintf('Inside\n')
    [xp, yp] = ginput(1);
    inside = inpolygon(xp,yp,xv,yv); %// is it inside?
end

% outside the polygon, so finished
fprintf('Outside\n')

因此,一旦用户在多边形外部单击,脚本便会终止.

And so once the user has clicked outside the polygon, the script terminates.

这篇关于确定用户单击的是多边形内部还是外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆