You can use the ginput()
function to get user clicks and then plot a rectangle based on the coordinates of the two corner points.
Here's an example code:
main.m189 chars10 lines
ginput()
returns the x and y coordinates of n
points clicked by the user. In this case, we only require two points to define a rectangle. The abs()
function is used to make sure that the width and height values are positive even if the user clicks in reverse order. Finally, rectangle()
is used to plot the rectangle using the Position
property, which specifies the position and size of the rectangle in the form [x, y, width, height]
.
Note that this code will plot the rectangle in the current figure, so you may want to create a new figure beforehand using the figure()
function.
gistlibby LogSnag