Create output of rectangles with 200 different colours

6 次查看(过去 30 天)
Hi , I have been asked to create 200 different output of rectangles, all with different colours. Thank you in advacne for help

回答(2 个)

Adam Danz
Adam Danz 2021-8-18
编辑:Adam Danz 2021-8-18
pos is a 1x4 vector defining [LeftSide, BottomSide, Width, Height].
See the FaceColor and EdgeColor name-values pairs to control color.
You could call it in a loop to produce multiple rectangles.
hold on
for i = 1:20
rectangle('position', rand(1,4).*randi(5,1,4), ...
'FaceColor', rand(1,3), 'EdgeColor','k')
end
axis tight
axis equal

DGM
DGM 2021-8-18
编辑:DGM 2021-8-19
Adam already covered the sensible solution. I'll post my own.
MIMT randspots() generates an image containing random colored shapes:
% create a 800x1000 image containing 200 rectangles
% blob dimensions vary between 20-60 px
% blob opacity varies between 10-80%
A = randspots([800 1000 3],200,[20 60],[0.1 0.8],'rectangle');
I've been meaning to rewrite it, but I haven't bothered yet. The syntax is clumsy, but it works.
randspots() is from the MIMT on the File Exchange.
If neither of these solutions are what you're after, then you'll need to be more specific.
EDIT:
Ah. I guess if you're working from a color table, then randspots doesn't apply. As the name implies, all it can do is random.

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by