% Script Tower
% Draws Octagon Towers

% Get N...
clc
N = input('Enter the number of rows in the tower: ');
%Set up the figure window....
close all
figure
axis equal off
hold on

% Initializations...
% (x,y) is the center of the next octagon that is to be drawn...
x = 0;
y = 0;
% xStart is the x-coordinate of the first tile in the current row...
xStart = 0;
% Key values...
s = sqrt(2 + sqrt(2))/2; % Center-to-side distance
side = 2*sqrt(1-s^2); % Side length

for k = 1:N
% Draw the k-th row of octagons. The x-coordinate of the first one is
x = xStart;
for j = 1:k
% Draw the jth tile in the kth row
if rem(k,2)==1 && rem(j,2) == 1
% Odd row and odd tile...
DrawOct(x,y,'y');
elseif rem(k,2)==1 && rem(j,2)==0
% Odd row and even tiles...
DrawOct(x,y,'g');
elseif rem(k,2)==0 && rem(j,2)==1
% Even row and odd tile...
DrawOct(x,y,'m');
else
% Even row and even tile...
DrawOct(x,y,'c');
end
% Increase x for the next tile to th eright...
x = x+(2*s+side);
end
% Decrease xStart for the next row...
xStart = xStart-(s+side/2);
% Decrease y for the next row..
y = y-(s+side/2);
end
hold off
shg