How to check a row in a matrix against every other row in a different matrix.
1 view (last 30 days)
Show older comments
Stefan Lewoski
on 21 Apr 2020
Commented: Stefan Lewoski
on 21 Apr 2020
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 Comments
Deepak Gupta
on 21 Apr 2020
Hello Stefen,
Problem is not very clear. Looking at code, its hard to understand what you are trying to achive. I see following possibilities.
- You have one circle and want to find out if this circle touches other circles from a set of circles.
- You have a set of circles and want to find out if circles from this set touch the circles from other sets
- 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
Accepted Answer
Deepak Gupta
on 21 Apr 2020
Edited: Deepak Gupta
on 21 Apr 2020
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 Comments
Deepak Gupta
on 21 Apr 2020
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.
More Answers (1)
Ameer Hamza
on 21 Apr 2020
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 Comments
Ameer Hamza
on 21 Apr 2020
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?
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!