Handles for multiple random squares?

1 view (last 30 days)
Samantha
Samantha on 14 Oct 2012
I am trying to write a script that plots N random squares, then allows the user to click on the figure window. If the point is in a square, the square turns red. I can plot N random squares and have created a function that determines if a point is in a square, but I can only get it to work for one square, not multiple squares. Here is my code so far:
N=2;
hold on
i=(1:N);
for i=1:N
b=rand(size(i));
d=rand(size(i));
c=[b,d];
e=rand(size(i));
h=plot_a_square(c,e);
end
while true
[x,y]=ginput(1);
p=[x,y];
z=zeros(size(i));
in_a_square(p,c,e)
for z=1;
set (h,'Color','r')
hold off
end
end
The plot_a_square code:
function h=plot_a_square(c,e)
x=[c(1)-.5*e,c(1)-.5*e, c(1)+.5*e,c(1)+.5*e,c(1)-.5*e];
y=[c(2)-.5*e,c(2)+.5*e, c(2)+.5*e,c(2)-.5*e,c(2)-.5*e];
h=plot(x,y);
end
The in_a_square code
function z=in_a_square(p,c,e)
if p>=c(1)-0.5*e & p<=c(1)+0.5*e & p>=c(2)-0.5*e & p<=c(2)+0.5*e
z=true;
else
z=false;
end
end
I know I need to create an array of the handles, and I think I need an array of the z values, but I don't know where to go from there. Thoughts?

Answers (0)

Categories

Find more on Polar Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!