line circle intersection not working,look at the image. i want to avoid the path that crossess ot touches the circle but my code is not working

4 visualizaciones (últimos 30 días)
please see my code, i have a line segment with end points 3,3 and -2,-2 and i plot a circle with centre 0,0 with radius r. i disperse random point around the circle and i want to plot the minimum path distance form 3,3 to -2,-2 without colliding the circle.
if true
angle=linspace(0,2*pi,360);
x=cos(angle);
y=sin(angle);
plot(x,y)
axis('equal')
s=1;
a=[3,3];
b=[0,0];
c=0;
d=[-2,-2];
m=0;
slope=0;
slope1=0;
c=0;
c1=0;
j=0;
n=20; % number of points that you want
center = [0 ,0]; % center coordinates of the circle [x0,y0]
radius = 2; % radius of the circle
angle = 2*pi*rand(n,1);
rr = radius*sqrt(rand(n,1));
rx = rr.*cos(angle)+ center(1);
ry = rr.*sin(angle)+ center(2);
%end of dispersal
hold on
plot(rx,ry,'.k');
mx=rx;
my=ry;
nx=rx;
ny=ry;
smallest=100;
for i=1:length(rx)
test=rx(i)^2+ry(i)^2;
b=[rx(i),ry(i)];
if(a(1)==b(1))
slope=inf;
c=b(1);
else
slope=a(2)-b(2)/a(1)-b(1);
c=b(2)-slope*b(1);
end
[m,~]=linecirc(slope,c,0,0,1);
mx(i)=m(1);
my(i)=m(2);
j=m(1);
if(isnan(j))
disp(rx(i));
disp(ry(i));
disp('they do not intersect circle')
if(d(1)==b(1))
slope=inf;
c=b(1);
else
slope1=d(2)-b(2)/d(1)-b(1);
c1=b(2)-slope*b(1);
end
[~,n]=linecirc(slope,c,0,0,1);
nx(i)=n(1);
ny(i)=n(2);
k=n(1);
if(isnan(k))
disp(rx(i));
disp(ry(i));
disp('they do not intersect with the goal')
if(test>1)%if the points lies outside thecircle
one=[3,3;rx(i),ry(i)];
two=[rx(i),ry(i);-2,-2];
dis1=pdist(one,'euclidean');
dis2=pdist(two,'euclidean');
if(dis1+dis2<smallest)
smallest=dis1+dis2;
resx=rx(i);
resy=ry(i);
end
end
end
end
end
plot([3 resx],[3 resy],'k');
plot([resx -2],[resy -2],'g');
end

Respuestas (1)

KSSV
KSSV el 22 de Mayo de 2018
Editada: KSSV el 22 de Mayo de 2018
How about the present approach? Download the function InterX from the file-exchange link: https://in.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections?focused=5165138&tab=function
angle=linspace(0,2*pi,360);
x=cos(angle);
y=sin(angle);
figure
hold on
plot(x,y)
axis('equal')
% two points
a=[3,3];
b=[-2,-2];
plot(a(1),a(2),'*b') ;
plot(b(1),b(2),'*g') ;
n=20; % number of points that you want
center = [0 ,0]; % center coordinates of the circle [x0,y0]
radius = 2; % radius of the circle
angle = 2*pi*rand(n,1);
rr = radius*sqrt(rand(n,1));
rx = rr.*cos(angle)+ center(1);
ry = rr.*sin(angle)+ center(2);
%end of dispersal
hold on
plot(rx,ry,'.k');
% get the path
R = [rx ry ] ; % random points
N = size(R,1) ; % number of random points
C = [x' y'] ; % circle
P = a ;
count = 0 ;
path = zeros([],2) ;
path(1,:) = P ;
while ~isequal(P,b)
count = count+1 ;
% Get distances from P to other points
[val,idx] = sort(pdist2(P,R),'descend') ;
% check which point doesn't intersect with circle
for i = 1:length(idx)
% check does the line intersects circle
L = [P' R(idx(i),:)'] ;
Pi = InterX(C',L) ;
if isempty(Pi)
P = R(idx(i),:) ;
plot(L(1,:),L(2,:),'color',rand(1,3)) ;
drawnow
break
end
end
% check the preent point and needed point
L0 = [P' b'] ;
Pi = InterX(C',L0) ;
if isempty(Pi)
P = b ;
plot(L0(1,:),L0(2,:),'color',rand(1,3)) ;
end
path(count+1,:) = P ;
end

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by