How to check a row in a matrix against every other row in a different matrix.

1 回表示 (過去 30 日間)
I am trying to use circcirc to check for intersections of a circle against every other circle that has been generated as I plot them on a graph. I am struggling to think of a way to make this happen. What I have so far is a for loop but it just confuses me the more I think about it, and help would be greatly appreciated
%For circle
if shape == 1
%Number of Shapes
shapeAmount =10000
%shape size(for now goes from 1 to shapeSize)
shapeSize = 1000
shapeSizeArray = rand(shapeAmount, 1) * shapeSize;
%ask for placements of shapes (Arbritary for now)
shapePlaceX = rand(shapeAmount,1)*100000;
shapePlaceY = rand(shapeAmount,1)*100000;
hold on;
%Generate first circle to check against
th = 0:pi/50:2*pi;
xunit = shapeSizeArray(1) * cos(th) + shapePlaceX(1);
yunit = shapeSizeArray(1) * sin(th) + shapePlaceY(1);
plot(xunit,yunit)
%Make array of center point and radius to check against other circles
circlestats = [shapePlaceX(1), shapePlaceY(1), shapeSizeArray(1)];
%Check if circles intersect
%if they do, throw away, if they don't keep and plot
count = 1
while count <= shapeAmount
for N = 1:size(circlestats,1)
[xCheck,yCheck] = circcirc(shapePlaceX(N+1), shapePlaceY(N+1), shapeSizeArray(N+1), circlestats(count,1), circlestats(count,2), circlestats(count,3));
if count >= shapeAmount
continue
elseif sum(isnan(xCheck)) ~= 0
circlestats = [circlestats; shapePlaceX(count), shapePlaceY(count), shapeSizeArray(count)];
xunit = shapeSizeArray(count) * cos(th) + shapePlaceX(count);
yunit = shapeSizeArray(count) * sin(th) + shapePlaceY(count);
plot(xunit,yunit)
N = N+1;
count = count+1
else
circlestats = [circlestats; shapePlaceX(1), shapePlaceY(1), shapeSizeArray(1)];
end
end
end
hold off
  4 件のコメント
Deepak Gupta
Deepak Gupta 2020 年 4 月 21 日
Hello Stefen,
Problem is not very clear. Looking at code, its hard to understand what you are trying to achive. I see following possibilities.
  1. You have one circle and want to find out if this circle touches other circles from a set of circles.
  2. You have a set of circles and want to find out if circles from this set touch the circles from other sets
  3. You have only one set of circles and want to find out which circles touch each other and plot them only if they touch atleast one other circle.
Please clarify further. You are also trying to create another circlestats matrix in the program, whats the point of it?
Thanks,
Deepak
Stefan Lewoski
Stefan Lewoski 2020 年 4 月 21 日
編集済み: Stefan Lewoski 2020 年 4 月 21 日
Hi, I have posted the whole code, hopefully that clears things up a little.
It is almost number 1. I have a set of circles A(the number of circles in the set grows). I also have a set of circles B which I would like to check if it intersects with any circles in set A. If it doesn't intersect, it is added to the set A. This process was meant to repeat until all of B has been checked against all of A, at which point all the circles in A are plotted on a graph.

サインインしてコメントする。

採用された回答

Deepak Gupta
Deepak Gupta 2020 年 4 月 21 日
編集済み: Deepak Gupta 2020 年 4 月 21 日
Hello Stefen,
Here is a piece of code. There is a list of non overlapping circles and a list of circles to be tested against non overlapping list. If we find a new circle not over lapping with existing non overlapping list of circles then we add this circles to the list of non over lapping circles and then test new circle for no overlap against this latest non- overlapping list of circles. If i understood you correctly, then this i what you want.
shapePlaceX = 20*rand(10, 1);
shapePlaceY = 20*rand(10, 1);
shapeSizeArray = 5*rand(10, 1);
circlestats = [shapePlaceX, shapePlaceY, shapeSizeArray]; %Circles to be compared with test circle.
nointersectCircles = [0, 0, 1]; %Initial test circle
noinstersectCount = 1;
flag =0;
for count= 1:size(shapePlaceX, 1)
for ii = 1:noinstersectCount
[xCheck,yCheck] = circcirc(nointersectCircles(ii, 1), nointersectCircles(ii, 2), nointersectCircles(ii, 3), circlestats(count,1), circlestats(count,2), circlestats(count,3));
if ~isnan(xCheck)
flag =1;
break;
end
end
if(flag ==0)
nointersectCircles = [nointersectCircles; circlestats(count, :)]; %Circle which passes test of no touch, will be added to no intersect list.
noinstersectCount = noinstersectCount+1; % Increase the no intersect count accordingly.
end
flag = 0;
end
subplot(3, 1, 1)
viscircles( [nointersectCircles(1,1), nointersectCircles(1,2)], nointersectCircles(1,3));
subplot(3, 1, 2)
viscircles( [circlestats(:, 1), circlestats(:, 2)], circlestats(:, 3));
subplot(3, 1, 3)
viscircles( [nointersectCircles(:, 1), nointersectCircles(:, 2)], nointersectCircles(:, 3));
Hope this helps.
Thanks,
Deepak
  3 件のコメント
Deepak Gupta
Deepak Gupta 2020 年 4 月 21 日
Hello Stefen,
As circcirc returns nan if two circles have same property(same x, y coordinates and radius) and i am not putting any extra checks in there is repition of circles so circles with same properties will also be added to the non-overlapping list.
Stefan Lewoski
Stefan Lewoski 2020 年 4 月 21 日
Hi, this is exactly what I was looking for, Thankyou!

サインインしてコメントする。

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 4 月 21 日
Try the code without any loop. It stores all the non-overlapping circles in 'no_overlap_cir'.
x = [12*rand(5,2)-6 6*rand(5,1)]; % random circles
centers = x(:,1:2);
radius = x(:,3);
center_dist = pdist2(centers, centers);
radius_sum = radius + radius.';
idx = find(sum(center_dist > radius_sum) == (numel(radius)-1));
no_overlap_cir = x(idx, :);
Following code helps in visualizing that the logic is correct. Run this complete code, and it will show the circles on axes. You can see that non-overlapping circles are placed in no_overlap_cir
x = [12*rand(5,2)-6 6*rand(5,1)]; % random circles
figure;
ax = axes();
axis([-10 10 -10 10]);
axis equal
for i=1:size(x)
images.roi.Circle(ax, 'Center', x(i,1:2), 'Radius', x(i,3), 'Label', num2str(i));
end
centers = x(:,1:2);
radius = x(:,3);
center_dist = pdist2(centers, centers);
radius_sum = radius + radius.';
idx = find(sum(center_dist > radius_sum) == (numel(radius)-1));
no_overlap_cir = x(idx, :);
  7 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 21 日
Ok. I read your question again. Is this correct:
Say you have 500 random circles. You take the first circle and put it in the matrix no_overlap_cir. Then you take the second circle and check if it overlaps with the circle in the no_overlap_cir. If yes, then it is discarded, and if no, then it is added to the matrix no_overlap_cir. And the process continues till the last circle.
If this is correct?
Stefan Lewoski
Stefan Lewoski 2020 年 4 月 21 日
Yes that is correct. I will mention just in case that it should be checked against every circle that is in no_overlap_cir not just one.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by