Generate coordinates for plotting a hexagonal mesh with the nsidedpoly function

Hi, I am trying to plot a hexagonal mesh using the nsidedpoly function as shown in the following code. I am taking the coordinates (coord array) from a file but I want to generate them myself in the code. How can I do this, by rings and counterclockwise, as shown in the figure?
Thank in advance.
radius = 5.6617/(sqrt(3)/2);
coord = [x(230:235, :)
x(256:262, :)
x(282:289, :)
x([308:310,312:316], :)
x(334:343, :)
x([360:364,366,368:370], :)
x(387:396, :)
x([414:416,418:422], :)
x(441:448, :)
x(468:474, :)
x(495:500, :)];
for i = 1:length(coord)
poly(i) = nsidedpoly(6, 'Center', coord(i, :), 'Radius', radius);
end
pg = plot(poly);
axis off
axis equal
% Convert num2str
POS = num2str(POSITIONS);
% Convert str2double
for i = 1:length(POSITIONS)
c = POS(i, :);
p(i, :) = str2double(c(~isspace(c)));
end
a = 0.5;
b = 1.5;
r = (b-a).*rand(87, 1) + a;
r = [p [1:87]'];
power = repmat([], 87, 1);
power(1:6, :) = r(ib(1:6), :);
power(7:13, :) = r(ib(7:13), :);
power(14:21, :) = r(ib(14:21), :);
power(22:29, :) = r(ib(22:29), :);
power(30:39, :) = r(ib(30:39), :);
power(40:48, :) = r(ib(40:48), :);
power(49:58, :) = r(ib(49:58), :);
power(59:66, :) = r(ib(59:66), :);
power(67:74, :) = r(ib(67:74), :);
power(75:81, :) = r(ib(75:81), :);
power(82:87, :) = r(ib(82:87), :);
nLevel = 256;
cmap = colormap(jet(nLevel));
polyFaceColor = cmap(ceil((power(:, 2)-a)*nLevel/100), :);
for i = 1:length(pg)
pg(i).FaceColor = polyFaceColor(i, :);
pg(i).FaceAlpha = 0.5;
end
str = num2str(power(:, 2), 4);
t2 = text(coord(:, 1), coord(:, 2), str,'HorizontalAlignment', 'center','VerticalAlignment','middle', 'FontSize', 10, 'FontName', 'Times');

 Réponse acceptée

C=arrayfun(@edgesamp,1:6,'uni',0); C=vertcat(C{:});
x=C(:,1); y=C(:,2);
n=numel(x);
p0=nsidedpoly(6,'Radius',1);
p=arrayfun(@(x,y)translate(p0,x,y),x,y);
plot(p); axis equal
text(x,y,string(1:n),'FontSize',12,'Horiz','center','Vert','mid')
function c=edgesamp(i)
p=rotate(nsidedpoly(6,'Radius',i*sqrt(3)),30);
v=flipud(p.Vertices([1:end,1],:));
c=diff(v,1,1).*reshape((0:i-1)/i,1,1,[])+v(1:end-1,:);
c=permute(c,[3,2,1]);
c=cell2mat(reshape( num2cell(c,[1,2]),[],1));
c=circshift(c,[(i-1),0]);
end

3 commentaires

Yro
Yro le 6 Mar 2022
Modifié(e) : Yro le 6 Mar 2022
Thank you very much for your reply. The function is great. But there is a way to plot the center [0 0] and move the count as I show in the figure. Because I tried to rotate but it doesn't work.
Thanks
Yep, see the revised code.
It's amazing, thank you so much for your help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by