Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: Write a function in Matlab that fit randomly chosen circular disk of

ID: 3745235 • Letter: P

Question

Problem 1: Write a function in Matlab that fit randomly chosen circular disk of various radius with centers at random locations within in a box of dimensions: length = L and height -H. Disk radius are integers randomly choosen from {1,2,3, ..., max_rad} where . max rad is a user-defined parameter. * Disk centers are randomly located at integer coordinates within the box. » None of the disk should overla » All disk should be contained within the boundarv of the box * You can assign different colors to disks with different radius when plotting the results. · Your function should try to fit as many disks as possible within the domain: L × H * Your function should plot the disks and should return the data, i.e. radius and centers, * You can used matlab function randi () to generate random numbers The best code will be able to fit the most # of disks. of the plotted disks. e.g. xc- randi (L) will gave a random integer between 1 and L; e.g. yc randi (H) will gave a random integer between 1 and H; e.g. rad- randi (max rad) will gave a random integer between 1 and max rad; * Try to make your function as robust as possible. Test various cases! function radius,centers -Ilt disks (L,H, max rad,max num % Inputs % L-> Length of box % H-> Height of box % max rad-> maximum radius of disk % max num -> maximum number of disk to be fitted % Note: Your function might not be able to fit the max num specified The goal is to fit as many disk as possible. You need to build some reasonable logic to accomplish this task You can add more inputs/outputs if necessary end

Explanation / Answer

function [radius, centers] = fit_disks(L, H, max_rad, max_num)
r=[]; %vector which stores the value of radius of circle which fit in the box
x=[]; %vector which stores the value of x-coordinate of circle which fit in the box
y=[]; %vector which stores the value of y-coordinate of circle which fit in the box
i=0; % counter value to record the no. of disks which fit the box
n=max_num;
l=L;h=H;
r_max = max_rad;
%to find the no. of disks which fit the box
while n>0
r1=randi(r_max);
x1=randi(l);
y1=randi(h);
if((r1+x1<=l) && (x1-r1>=0) && (r1+y1<=h) && (y1-r1>=0))
if i==0
r=[r r1];
x=[x x1];
y=[y y1];
i=i+1;
else
k=1;
%to check the circle overlapping with other circles
for j=1:i
k=k*((abs(x(j)-x1) >= r(j)+r1) && (abs(y(j)-y1) >= r(j)+r1));
end
if(k==1)
r=[r r1];
x=[x x1];
y=[y y1];
i=i+1;
end
end
%i=i+1;
end
n=n-1;
end
%Code to plot the disks which fit in different colors
color=['r','g','b','y','m','c','k']; %different colors
for p=1:length(r)
hold on;
th=0:pi/100:2*pi;
xunit=r(p)*cos(th)+x(p);
yunit=r(p)*sin(th)+y(p);
plot(xunit,yunit,color(randi(size(color))));axis([0 l 0 h]);
hold off;
end
radius = r;
centers = [x '|' y]; % '|' is a seperator to separate x and y coordinatess
end