Colored Hexagons using Patch
11 views (last 30 days)
Show older comments
Hi to everyone, I was trying to make a honeycomb with Matlab, I've had no problem with the structure but I've some trouble trying to colour those hexagons with some defined color (e.g. red, blue and violet). This is the way I get the hexagons:
theta=0:60:360;
x=sind(theta);
y=cosd(theta);
v=x(2);
for i=1:4; x=x+v*2; a(i,:)=x; b(i,:)=y; %Create the row
end
c=a'; d=b';
for j=1:4; e=d+1.5*j; re=rem(j,2); %Create the columns
if re==0, g=c;
else g=c-v;
end
colour=linspace(0,1,i)
patch(g,e,colour)
end
hold on
patch(c,d,colour)
But the colour is not specified by me, but is randomly chosen from 0 to 1.
I've read the help but I don't get the point.
Thanks in advance to everyone!!
0 Comments
Answers (2)
Image Analyst
on 6 May 2016
Replace this:
colour=linspace(0,1,i)
patch(g,e,colour)
with this
% Define color with whatever values you need for the color you want.
colour=[1, 0.4, 0.5]; % Any 3 values between 0 and 1, or predefined colors 'r', 'b', 'y', etc.
patch(g,e,colour);
3 Comments
Mike Garrity
on 6 May 2016
cindex = 1+mod(2*mod(i,2) + 1*mod(j,2),7);
Did you try that? The basic idea is to have both your row index and column index go into generating the color.
stefano stefano
on 7 May 2016
2 Comments
Image Analyst
on 31 Dec 2020
Looks like the same as his comment
just with a different set of colors.
See Also
Categories
Find more on Surface and Mesh Plots 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!


