![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192335/image.png)
Create polygons from circles x, y and radii
35 views (last 30 days)
Show older comments
I have 11k circles with the x/y coordinates and radii, but I want to patch them all with specific colours from the 11000-by-3 RGB colour matrix. While I use patch for my other problems, it only accepts polygon input. So I want to create polygons (with many points/faces so they still look like round) from these circles. I think it can be done with `polyshape`, but I'm not really sure (maybe use sin/cos) on how to to create polygon coordinates from the x/y/radii of the circles.
EDIT
To give you an impression on what I'm working with, I plotted the circles. The circles are derived from delaunay triangles.
0 Comments
Accepted Answer
Bruno Luong
on 21 Oct 2018
Edited: Bruno Luong
on 21 Oct 2018
Your input
xc = 1;
yc = 2;
r = 3;
n = 100;
Generate polyshape n-points of a circle with center (xc,yc) radius r
theta = (0:n-1)*(2*pi/n)
x = xc + r*cos(theta);
y = yc + r*sin(theta);
P = polyshape(x,y)
P =
polyshape with properties:
Vertices: [100×2 double]
NumRegions: 1
NumHoles: 0
Plot it
plot(P)
axis equal
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192335/image.png)
8 Comments
Bruno Luong
on 21 Oct 2018
Edited: Bruno Luong
on 21 Oct 2018
To remove the for-loop, you can still set RGB via colormap (kind of overkill to have one color by circle)
n = 50;
theta = (0:n-1)*(2*pi/n);
nc = 11000;
xc = rand(nc,1);
yc = rand(nc,1);
r = 1e-3+0.025*rand(nc,1);
X = xc + r.*cos(theta);
Y = yc + r.*sin(theta);
close all
RGB = rand(nc,3); % one RGB per circle
colormap(RGB);
C = rand(1,nc); % patch with random face color
patch(X',Y',(1:nc),'EdgeColor','none','FaceAlpha',0.7);
set(gca,'clim',[0.5 nc+0.5]);
axis equal
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193344/image.png)
More Answers (1)
Image Analyst
on 21 Oct 2018
One way is to use poly2mask() several times, ORing them together, to get a binary image of all shapes. Then use bwlabel() to get each region a different ID label (number). Then use label2rgb() to give each region a unique color.
binaryImage = false(rows, columns);
for k = 1 : numRegions
% Somehow get the x and y for this particular shape
x = ....
y = ....
binaryImage = binaryImage | poly2mask(x, y, rows, columns);
end
labeledImage = bwlabel(binaryImage);
rgbImage = label2rgb(labeledImage, yourColorMap);
Create your colormap as a numRegions by 3 array of color numbers in the range 0-1.
See Also
Categories
Find more on Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!