MATLAB: Counting Plots on a Figure

areaawesomecalculating pointscalculationscircleMATLABplottingpointsrangerectangle

Hello. I am currently attempting to write a program that generates a rectangular shape, then plots 4 circles inside the rectangle (a shooting target is the desired look). After than, the user inputs how many "shots" to be fired, and that number of inputs will be displayed as smaller black circles inside the rectangle. I am trying to count how many of the smaller black circles came into contact with, or are within the larger red circle. Below is the code:
%{ * * TITLE: Target Range Plotting %}
clc
clear
close all
%generating start values for code
start_message = sprintf('Welcome to the range, how many shots are ya'' firin'' today?');
go_again = 1;
while go_again == 1
%EC-how many bullets you want to fire using inputdlg + validate entry
number_shots = str2double(inputdlg(start_message,'Shot Count'));
while number_shots <= 0 || mod(number_shots,1) ~= 0
new_message = sprintf('Now that don''t seem like a good numba to me. Try usin'' positive whole numbas only.');
number_shots = str2double(inputdlg(new_message,'Shot Count'));
end
%given variables
xmin = 0;
xmax = 30;
ymin = 0;
ymax = 100;
%generating x and y arrays
xarray = [xmin-5 xmin-5 xmax+5 xmax+5 xmin-5];
yarray = [ymax-5 ymin ymin ymax-5 ymax-5];
%plotting box
plot(xarray,yarray); %changing axis to see the box
hold on
title('Shooting Target');
xlabel('Close Figure When Done');
%plotting circle
k = 20;
for ct = 1:4
drawCircle(k,[xmax/2,ymax-25],xmin,xmax,ymin,ymax,'r');
k= k-5;
end
%plotting number of shots user wanted
for ctt = 1:number_shots
drawCircle(1,[(rand*(xmax +10) -5) rand*(ymax-5)],xmin,xmax,ymin,ymax,'k');
end
axis([xmin-40 xmax+40 ymin ymax]) %displaying total graph in a nicer view
uiwait %waiting until graph is closed to display next message
message = sprintf('Nice shootin'' Tex! Your results:\n\nTimes Hit:\nTimes Missed:\nAccuracy:\n');
endmsg = questdlg(message,'Results','Shoot Again','Leave the Range','Leave the Range');
switch endmsg %determining if the program is re-run or not
case 'Shoot Again'
go_again = 1;
start_message = 'Great to see y''again! How many times ya'' shootin'' this time?';
otherwise
go_again = 0;
msgbox('Thanks for shootin!');
end
end
Function drawCircle File:
function drawCircle(radius,center,xmin,xmax,ymin,ymax,color)
%draws circle with a given center and generates 4 circles (target)
%by: **
theta = linspace(0,360);
y=radius*sind(theta)+center(2);
x=radius*cosd(theta)+center(1);
plot(x,y,color);
axis([xmin,xmax,ymin,ymax])

Best Answer

If you have two circle origins with radius:
Pt1 = [20 30];
Pt2 = [15 20];
rad1 = 7;
rad2 = 12;
You can determine if they overlap by checking the distance between the points. If that distance is greater than the sum of their radii, then they overlap:
pt2ptDist = sqrt(sum((Pt1-Pt2).^2));
if pt2ptDist < rad1+rad2
disp('HIT!')
end
But in your code, you never store the origins of the circles... you only draw them:
for ctt = 1:number_shots
drawCircle(1,[(rand*(xmax +10) -5) rand*(ymax-5)],xmin,xmax,ymin,ymax,'k');
end
Instead, you should store the variable locations and use the formula I gave above to determine if each stored location makes a hit with the other circles.