Colored Hexagons using Patch

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!!

回答 (2 件)

Image Analyst
Image Analyst 2016 年 5 月 6 日

0 投票

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 件のコメント

stefano stefano
stefano stefano 2016 年 5 月 6 日
Thanks for your reply, what I get with your code is a colouration per rows, like
But I want to be able to colour each element of a row with a different colour and make a permutation over the index of the column to obtain a result like
Mike Garrity
Mike Garrity 2016 年 5 月 6 日
What I did in the blog post that picture came from was this:
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
stefano stefano 2016 年 5 月 7 日
I see your answer just now: I was inspired by your post on the blog and I want to try a 2-d version!
theta=0:pi/3:2*pi;
x=sin(theta);
y=cos(theta);
v=x(2);
u=y(1)+y(2);
for i=1:columns;
x=x+v*2;
a(i,:)=x; b(i,:)=y;
c=a'; d=b';
r1=rem(i,2);
if r1==0, col1(i)='r';
else col1(i)='c';
end
axis equal
patch(c(:,i),d(:,i),col1(i));
for k=1:rows/2;
g=d+3*k;
patch(c(:,i),g(:,i),col1(i))
end
end
for j=1:columns;
e=c-v; f=d+u;
r2=rem(j,2);
if r2==0, col2(j)='y';
else col2(j)='m';
end
patch(e(:,j),f(:,j),col2(j))
for h=1:rows/2;
l=f+3*h;
patch(e(:,j),l(:,j),col2(j))
end
end
I'm now able to realize the image but I'm not able to find the proper colours as yours, can you please give me a hint?
Thanks

サインインしてコメントする。

stefano stefano
stefano stefano 2016 年 5 月 7 日

0 投票

Finally I came to the solution by creating my own color map; The result is this:
Thanks to everyone!

2 件のコメント

teimurazi gochitashvili
teimurazi gochitashvili 2020 年 12 月 31 日
Can you please upload final code?
Image Analyst
Image Analyst 2020 年 12 月 31 日
Looks like the same as his comment
just with a different set of colors.

サインインしてコメントする。

カテゴリ

質問済み:

2016 年 5 月 6 日

コメント済み:

2020 年 12 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by