Drawing random lines in rectangular

3 次查看(过去 30 天)
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 个)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021-11-21
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 个评论
HyeongJu Lee
HyeongJu Lee 2021-11-21
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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by