dimanche 16 décembre 2018

Matlab generating random numbers and overlap check

I wrote a code for generating random number of rods on Matlab within a specified domain and then saving the output in a text file. I would like to ask for help on adding the following options to the code; (i) if the randomly generated rod exceeds the specified domain size, the length of that rod should be shortened so that to keep it in that particular domain. (ii) i would like to avoid the overlapping of the newly generated number (rod) with that of the previous one, in case of overlap generate another place for the new rod.

I can't figure out how shall I do it. It would be of much help if someone may help me write code for these two options. Thank you

% myrandom.m  
% Units are mm.

% domain size
bx = 160;
by = 40;
bz = 40;

lf = 12; % rod length
nf = 500; % Number of rods

rns = rand(nf,3);      % Start
rne = rand(nf,3)-0.5;  % End

% Start Points
for i = 1:nf
    rns(i,1) = rns(i,1)*bx;
    rns(i,2) = rns(i,2)*by;
    rns(i,3) = rns(i,3)*bz;
end

% Unit Deltas
delta = zeros(nf,1);
for i = 1:nf
   temp = rne(i,:);
   delta(i) = norm(temp);
end

% Length Deltas
rne = lf*rne./delta;

% End Points
rne = rns + rne;


fileID = fopen('scfibers.txt','w');
for i = 1:nf
    fprintf(fileID,'%12.8f %12.8f %12.8f\r\n',rns(i,1),rns(i,2),rns(i,3));
    fprintf(fileID,'%12.8f %12.8f %12.8f\r\n\r\n',rne(i,1),rne(i,2),rne(i,3));
end
fclose(fileID);




Aucun commentaire:

Enregistrer un commentaire