How to calculate the line segment?

5 visualizaciones (últimos 30 días)
HyeongJu Lee
HyeongJu Lee el 13 de Nov. de 2021
Respondida: Vidhi Agarwal el 28 de Nov. de 2024
Hi,
I am trying to draw random lines in a circle.
I need to figure out the average of the random lines, the number of intersection of lines, and the average of line segment.
I cannot figure out how to calculate the average of line segment.
The codes are as follows:
close all; clearvars; clc;
% SIMULATION DISK DIMENSIONS
xx0=0; yy0=0; % ceneter of disk
r=1; % disk radius
numbLines=4;
theta = zeros(numbLines,1);
theta(1) = 2*pi*rand();
for i = 2: numbLines
theta(i) = theta(i-1) + 20*(pi/180) - 60*(pi/180)*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','*');
** Line segement indicates that the line between two intersections.

Respuestas (1)

Vidhi Agarwal
Vidhi Agarwal el 28 de Nov. de 2024
To calculate the average length of line segments between intersections within the circle, you can follow these steps:
  • After you have calculated the intersection points of the lines within the circle, for each pair of intersections on the same line, calculate the line segment length. Sample code for the same is given below:
% Calculate line segment lengths
segment_lengths = [];
for i = 1:numbLines
% Find intersections on the current line
line_intersections = [];
for j = 1:length(x_int)
if abs(slope(i) * x_int(j) + ycept(i) - y_int(j)) < 1e-6
line_intersections = [line_intersections; x_int(j), y_int(j)];
end
end
% Sort intersections along the line
line_intersections = sortrows(line_intersections);
% Calculate segments between intersections
for k = 1:size(line_intersections, 1) - 1
seg_length = sqrt((line_intersections(k+1, 1) - line_intersections(k, 1))^2 + ...
(line_intersections(k+1, 2) - line_intersections(k, 2))^2);
segment_lengths = [segment_lengths; seg_length];
end
end
  • Compute the average length of these segments.
% Average segment length
if ~isempty(segment_lengths)
average_segment_length = mean(segment_lengths);
else
average_segment_length = 0;
end
Hope this helps!

Categorías

Más información sobre Spline Postprocessing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by