Drawing random lines in rectangular

조회 수: 3(최근 30일)
HyeongJu Lee 2021년 11월 21일
편집: Image Analyst 2021년 11월 21일
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
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
댓글 수: 1표시숨기기 없음
Image Analyst 2021년 11월 21일
Just to show the missing image of what his code produces: 댓글을 달려면 로그인하십시오.

답변(1개)

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 2021년 11월 21일
Hello,
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).