I would like to change how the color of the circles change in my code.

11 views (last 30 days)
My code uses "color = rand(1,3)" when the circle intersects or is nested within another circle to choose a different color. Sometimes when I run my code and choose a circle that intersects or is nested within another circle, the color that the "rand(1,3)" chooses is very similar to the color of the previous circle even though it is not the same color. I want the code to change the color of the circle to a color that it noticably different than the previous color when the circles intersect or are nested within one another.
My code:
clc;
clear;
clf;
hold on;
axis manual;
title('Circles');
xlim([0 10]);
ylim([-4 4]);
theta= linspace(0,2*pi,1000);
set_xc = [];
set_yc = [];
set_r = [];
% Circle loop
while true
color = 'r';
fprintf('Left click on the figure to choose a center point. Right click to quit. \n');
[xc,yc,button] = ginput(1);
if button > 1
fprintf('finished \n');
break;
end
plot(xc,yc,'+','Color',color,'LineWidth',1);
fprintf('Left click on the figure to choose a point on the circle''s perimeter. \n');
[ra,rb,button] = ginput(1);
plot(ra,rb);
r = sqrt((xc-ra).^2+(yc-rb).^2);
% intersection
for n = 1:numel(set_r)
centerdistance = sqrt((yc-set_yc(n)).^2 + (xc-set_xc(n)).^2);
radiisum = r+set_r(n);
if centerdistance > radiisum
else
color = rand(1,3);
break;
end
end
plot(xc,yc,'+','Color',color,'LineWidth',1);
area2 = pi*(r).^2;
txt2 = ['area: ', num2str(area2)];
text(xc-0.55,yc+0.3,txt2,'Color',color);
x = r.*cos(theta)+ xc;
y = r.*sin(theta)+ yc;
plot(x,y,'Color',color,'LineWidth',2);
set_xc(end+1) = xc;
set_yc(end+1) = yc;
set_r(end+1) = r;
end
For example: In this run, I kept intersecting circles so the color of the new circle kept changing. As you can see there are two circles that look purple even though they are not exactly the same color because of using rand(1,3). The RGB values of the circles are too similar and I want there to be no circles that look the same for at least like 5-10 different circles. Maybe there is a way to add 'g','b','y','m', etc to a list or dictionary and use the next color if conditions are met. So, if the chosen circle intersects or is nested within another, the color changes to green, if they intersect again go to blue and so on.

Answers (1)

Voss
Voss on 3 May 2022
  1 Comment
Image Analyst
Image Analyst on 3 May 2022
Of course the more circles you have the harder it will be to get colors that are dramatically different.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!