在matlab中检测用户鼠标单击

时间:2014-05-27 20:46:50

标签: matlab matlab-figure figure

我正在尝试在matlab中制作一个用户选择形状的游戏,并且该形状可以平移和缩小屏幕。然后,用户必须单击形状,并将一个点添加到他们的分数中。我想知道如何让程序检测用户鼠标点击形状,但如果点击白色空间则结束程序。

1 个答案:

答案 0 :(得分:3)

你可以沿着这些方向前进。以下代码基本上使用:

  • fill绘制多边形;
  • ginput获取光标位置;
  • inpolygon检查该位置是否在多边形内。

代码:

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?
if inside
    %// it's inside. Do something accordingly
else
    %// it's outside. Do something else
end
相关问题