function C3(N)
% Draws two hexwheels with side consisting of N hex tiles
% In Figure 1 the wheel has vertices in the order 'g','y','r','m','b','c'.
% In Figure 2 is the complement of the wheel in Figure 1.
close all
figure
axis equal off
hold on
DrawWheel(N,0)
hold off
figure
axis equal off
hold on
DrawWheel(N,1)
hold off
shg

function DrawWheel(N,flip)
% Adds a hexagonal color wheel to the current figure window.
% The edge of the wheel has N hexagons.
% The input flip is either zero or 1.
% If flip==0 then the vertices are in the order 'g','y','r','m','b','c'
% and saturated interpolation is used.
% DrawWheel(N,1) does the same thing except that complement color is
% displayed.

red = [1 0 0];
green = [0 1 0];
blue = [0 0 1];
magenta = [1 0 1];
cyan = [0 1 1];
yellow = [1 1 0];
y = 0;
xStart = -N+1;
for k=0:N-1
% Draw rows k and -k...
x = xStart;
f = k/(N-1);
LTop = InterpSat(f,magenta,red); % Color of leftmost tile in row k
RTop = InterpSat(f,green,yellow); % Color of rightmost tile in row k
LBot = InterpSat(f,magenta,blue); % Color of leftmost tile in row -k
RBot = InterpSat(f,green,cyan); % Color of rightmost tile in row -k
for j=1:2*N-k-1
% Draw the jth tile in row k...
f1 = (j-1)/(2*N-k-2);
cTop = InterpSat(f1,LTop,RTop);
DrawHex(x,y,cTop,flip)
% Draw the jth tile in row -k...
cBot = InterpSat(f1,LBot,RBot);
DrawHex(x,-y,cBot,flip)
x = x+sqrt(3);
end
xStart = xStart+sqrt(3)/2;
y = y+3/2;
end

function c = InterpSat(f,c1,c2)
% Interpolates and saturates between colors c1 and c2.
% f satisies 0<=f<=1 and is the fraction of c that is "made up" of c2.
c = (1-f)*c1 + f*c2;
c = c/max(c);

function DrawHex(xc,yc,c,flip)
% Assumes that "hold" is on and adds a colored hexagon to the current figure
% window. The hexagon is center is (xc,yc) and its edge length is 1.
% Assumes that c is a valid length-3 rgb vector, i.e., 0<=c(i)<=1 for i=1:3.
a = sqrt(3)/2;
b = 1/2;
x = xc + [0 a a 0 -a -a];
y = yc + [1 b -b -1 -b b];
if flip
c = [1 1 1] - c;
end
fill(x,y,c)