Drawing random lines in rectangular

8 vues (au cours des 30 derniers jours)
HyeongJu Lee
HyeongJu Lee le 21 Nov 2021
Modifié(e) : Image Analyst le 21 Nov 2021
I so far coded to draw random lines in circumference.
However, I want to draw random lines.
It will be great if anyone can give me some tips.
Here is my code so far.
--------------------------------------------------------
close all; clearvars; clc;
% SIMULATION DISK DIMENSIONS
xx0=0; yy0=0; % ceneter of disk
r=1; % disk radius
numbLines=30;
theta = zeros(numbLines,1);
theta(1) = 2*pi*rand();
for i = 2: numbLines
theta(i) = 2*pi*rand();
end
p=r*rand(numbLines,1);
q=sqrt(r.^2-p.^2);
sin_theta=sin(theta);
cos_theta=cos(theta);
xx1=xx0+p.*cos_theta+q.*sin_theta;
yy1=yy0+p.*sin_theta-q.*cos_theta;
xx2=xx0+p.*cos_theta-q.*sin_theta;
yy2=yy0+p.*sin_theta+q.*cos_theta;
%%% START Plotting %%%START
%draw circle
t=linspace(0,2*pi,300);
xp=xx0+r*cos(t); yp=yy0+r*sin(t);
plot(xp,yp,'k');
axis square; hold on;
axis tight;
xticks([]);yticks([]);
set(gca,'Visible','off');
%plot segments of Poisson line process
plot([xx1';xx2'],[yy1';yy2'],'b','LineWidth',2);
%%%END Plotting END%%%
slope = zeros(numbLines,1);
ycept = zeros(numbLines,1);
xx = [xx1'; xx2'];
yy = [yy1'; yy2'];
Total_length=[sqrt((xx1-xx2).^2+(yy1-yy2).^2)];
sumOfElements = sum(sum(Total_length));
Average_length = sumOfElements/numbLines
for ii = 1:numbLines
slope(ii,1) = (yy(2,ii)-yy(1,ii))/(xx(2,ii)-xx(1,ii));
ycept(ii,1) = -(yy(2,ii)-yy(1,ii))/(xx(2,ii)-xx(1,ii))*xx(1,ii)...
+yy(1,ii); %% Might as well use xx(2,ii) & yy(2,ii) for the y-intercept
end
x_int = []; %% X and Y intersecting points
y_int = [];
for ii = 1:numbLines-1
slope_temp = slope(ii,1); ycept_temp = ycept(ii,1);
for jj = ii+1:numbLines
xtemp = -(ycept(jj,1)-ycept_temp)/(slope(jj,1)-slope_temp);
ytemp = slope_temp*xtemp+ycept_temp;
rr_temp = xtemp^2+ytemp^2;
if rr_temp<=r %% Pick only the intersecting points that are in the circle
x_int = [x_int; xtemp];
y_int = [y_int; ytemp];
end
end
end
scatter(x_int,y_int,100,'r','*');
n = numel(x_int);
Mean_fiber_segment_length = sumOfElements / (numbLines+2*n)
Network_density = sumOfElements
Crosslink_number = numel(x_int)
  1 commentaire
Image Analyst
Image Analyst le 21 Nov 2021
Modifié(e) : Image Analyst le 21 Nov 2021
Just to show the missing image of what his code produces:

Connectez-vous pour commenter.

Réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 21 Nov 2021
Some of the loops can be substituted with vectorization operations, i.e.:
...
numbLines=30;
theta = 2*pi*rand(numbLines, 1);
...
%%% START Plotting %%%START
...
slope(:,1) = (yy(2,:)-yy(1,:))./(xx(2,:)-xx(1,:));
ycept(:,1) = -(yy(2,:)-yy(1,:))./(xx(2,:)-xx(1,:)).*xx(1,:)+yy(1,:); %% Might as well use xx(2,ii) & yy(2,ii) for the y-intercept
...
  1 commentaire
HyeongJu Lee
HyeongJu Lee le 21 Nov 2021
Hello,
First of all, thank you for your reply.
I just want to change boundary from circle to square (length L) and find out each lines start point and end point.
(i.e.) If L = 1 square,
Line1 - Start point (-1,-0.572) and End point (0.508, -1).
I cannot understand your solution...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by