Pythagoras tree is an infinite repetition pattern by Pythagoras according to the Pythagorean theorem are drawn. Because the shape is like a tree after repeated several times, so called Pythagoras tree, also called the “Gougu tree”.

Right triangle two rectangular square and is equal to the square of the hypotenuse. The area of two adjacent small squares equal to the size of a large square adjacent to each other. At the same time all small square is equal to the maximum area and the area of a square, triangle two right angle sides of the square and the square of the hypotenuse is equal to the. The inequality of A^2+B^2 over 2AB

A triangle between three squares whose area is less than or equal to 1/4 of the area of a large square, greater than or equal to a small square area of about 1/2. According to the shape of a triangle, repeat the triangles Pythagoras tree “trunk” dense degree is different.

function M = Pythagor_tree(m,n,Colormap) % function M = Pythagor_tree(m,n,Colormap) % Compute Pythagoras_tree % The Pythagoras Tree is a plane fractal constructed from squares. % It is named after Pythagoras because each triple of touching squares % encloses a right triangle, in a configuration traditionally used to % depict the Pythagorean theorem. % http://en.wikipedia.org/wiki/Pythagoras_tree % % Input : % - m ( double m> 0) is the relative length of one of the side % right-angled triangle. The second side of the right-angle is % taken to be one. % To have a symmetric tree, m has to be 1. % - n ( integer ) is the level of recursion. % The number of elements of tree is equal to 2^(n+1)-1. % A reasonnable number for n is 10. % - Colormap: String used to generate color of the different levels % of the tree. % All these arguments are optional: the function can run with % argument. % Output : % - Matrix M: Pyhagoras tree is stored in a matrix M. % This matrix has 5 columns. % Each row corresponds to the cordinate of each square of the tree % The two first columns give the bottom-left position of each % square. The third column corresponds to the orientation angle of % each square. The fourth column gives the size of each square. The % fifth column specifies the level of recursion of each square. % The first row corresponds to the root of the tree. It is always % the same % M(1,:) = [0 -1 0 1 1]; % The leaf located at row i will give 2 leaves located at 2*i and % 2*i+1. % - A svg file giving a vectorial display of the tree. The name of % file is generated from the parameter m,n,Colormap. The file is % stored in the current folder. % % 2010 02 29 % Guillaume Jacquenot % guillaume dot jacquenot at gmail dot com %% Check inputs narg = nargin; if narg <= 2 % Colormap = 'jet'; Colormap = 'summer'; if narg <= 1 n = 12; % Recursion level if nargin == 0 m = 0.8; end end end if m <= 0 error([mfilename ':e0'],'Length of m has to be greater than zero'); end if rem(n,1)~=0 error([mfilename ':e0'],'The number of level has to be integer'); end if ~iscolormap(Colormap) error([mfilename ':e1'],'Input colormap is not valid'); end %% Compute constants d = sqrt(1+m^2); % c1 = 1/d; % Normalized length 1 c2 = m/d; % Normalized length 2 T = [0 1/(1+m^2);1 1+m/(1+m^2)]; % Translation pattern alpha1 = atan2(m,1); % Defines the first rotation angle alpha2 = alpha1-pi/2; % Defines the second rotation angle pi2 = 2*pi; % Defines pi2 nEle = 2^(n+1)-1; % Number of elements (square) M = zeros(nEle,5); % Matrice containing the tree M(1,:) = [0 -1 0 1 1]; % Initialization of the tree %% Compute the level of each square contained in the resulting matrix Offset = 0; for i = 0:n tmp = 2^i; M(Offset+(1:tmp),5) = i; Offset = Offset + tmp; end %% Compute the position and size of each square wrt its parent for i = 2:2:(nEle-1) j = i/2; mT = M(j,4) * mat_rot(M(j,3)) * T; Tx = mT(1,:) + M(j,1); Ty = mT(2,:) + M(j,2); theta1 = rem(M(j,3)+alpha1,pi2); theta2 = rem(M(j,3)+alpha2,pi2); M(i ,1:4) = [Tx(1) Ty(1) theta1 M(j,4)*c1]; M(i+1,1:4) = [Tx(2) Ty(2) theta2 M(j,4)*c2]; end %% Display the tree Pythagor_tree_plot(M,n); %% Write results to an SVG file Pythagor_tree_write2svg(m,n,Colormap,M); function Pythagor_tree_write2svg(m,n,Colormap,M) % Determine the bounding box of the tree with an offset % Display_metadata = false; Display_metadata = true; nEle = size(M,1); r2 = sqrt(2); LOffset = M(nEle,4) + 0.1; min_x = min(M(:,1)-r2*M(:,4)) - LOffset; max_x = max(M(:,1)+r2*M(:,4)) + LOffset; min_y = min(M(:,2) ) - LOffset; % -r2*M(:,4) max_y = max(M(:,2)+r2*M(:,4)) + LOffset; % Compute the color of tree ColorM = zeros(n+1,3); eval(['ColorM = flipud(' Colormap '(n+1));']); co = 100; Wfig = ceil(co*(max_x-min_x)); Hfig = ceil(co*(max_y-min_y)); filename = ['Pythagoras_tree_1_' strrep(num2str(m),'.','_') '_'... num2str(n) '_' Colormap '.svg']; fid = fopen(filename, 'wt'); fprintf(fid,'\n'); if ~Display_metadata fprintf(fid,'\n'); end fprintf(fid,'\n'); if Display_metadata fprintf(fid,'\tPythagoras tree\n'); fprintf(fid,'\t\n'); fprintf(fid,'\t\t\n'); fprintf(fid,'\t\t\t<cc:Work\n'); fprintf(fid,'\t\t\t\trdf:about="">\n'); fprintf(fid,'\t\t\t\timage/svg+xml\n'); fprintf(fid,'\t\t\t\t<dc:type\n'); fprintf(fid,'\t\t\t\t\trdf:resource="http://purl.org/dc/dcmitype/StillImage" />\n'); fprintf(fid,'\t\t\t\tPythagoras tree\n'); fprintf(fid,'\t\t\t\t\n'); fprintf(fid,'\t\t\t\t\t\n'); fprintf(fid,'\t\t\t\t\t\tGuillaume Jacquenot\n'); fprintf(fid,'\t\t\t\t\t\n'); fprintf(fid,'\t\t\t\t\n'); fprintf(fid,'\t\t\t\t<cc:license\n'); fprintf(fid,'\t\t\t\t\t\trdf:resource="http://creativecommons.org/licenses/by-nc-sa/3.0/" />\n'); fprintf(fid,'\t\t\t\n'); fprintf(fid,'\t\t\t<cc:License\n'); fprintf(fid,'\t\t\t\trdf:about="http://creativecommons.org/licenses/by-nc-sa/3.0/">\n'); fprintf(fid,'\t\t\t\t<cc:permits\n'); fprintf(fid,'\t\t\t\t\trdf:resource="http://creativecommons.org/ns#Reproduction" />\n'); fprintf(fid,'\t\t\t\t<cc:permits\n'); fprintf(fid,'\t\t\t\t\trdf:resource="http://creativecommons.org/ns#Reproduction" />\n'); fprintf(fid,'\t\t\t\t<cc:permits\n'); fprintf(fid,'\t\t\t\t\trdf:resource="http://creativecommons.org/ns#Distribution" />\n'); fprintf(fid,'\t\t\t\t<cc:requires\n'); fprintf(fid,'\t\t\t\t\trdf:resource="http://creative