user3681360
user3681360

Reputation: 11

Detect user mouse click in matlab

I am trying to make a game in matlab where the user selects a shape and that shape translates and scales down a screen. The user then has to click on the shape and a point is added to their score. I was wondering how to make the program detect the users mouse click on the shape but end the program if they click on white space.

Upvotes: 0

Views: 774

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

You can go along these lines. The following code basically uses:

  • fill to draw a polygonal shape;
  • ginput to get cursor position;
  • inpolygon to check if that position is inside the polygon.

Code:

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

Upvotes: 3

Related Questions