% Tangram_mzip % MATLAB zip file, a self-extracting MATLAB archive. % Usage: Run this file to recreate the original directory. fname = mfilename; fin = fopen([fname '.m'],'r'); dname = fname(1:find(fname=='_',1,'last')-1); mkdir(dname); mkdir([dname filesep 'lib']) addpath(dname) addpath([dname filesep 'lib']) L = fgetl(fin); while length(L) < 2 || ~isequal(L(1:2),'%%') L = fgetl(fin); end while ~isequal(L,'%% EOF') F = [dname filesep L(4:end)]; disp(F) fout = fopen(F,'w'); L = fgetl(fin); while length(L) < 2 || ~isequal(L(1:2),'%%') fprintf(fout,'%s\n',L); L = fgetl(fin); end fclose(fout); end fclose(fin); %% Tangram.m function Pout = Tangram % Tangram Tangram puzzle % https://blogs.mathworks.com/cleve/2025/10/23/tangram-puzzles/ % Copyright 2025 Cleve Moler % '26-Oct-2025 16:37:46' i = 1i; triangle = @(s) [-s-s*i s-s*i 0 -s-s*i]; square = @(s) [0 s s+s*i s*i 0]; parallelogram = @(s) [-2*s-2*s*i 0-2*s*i s-s*i -s-s*i -2-2*s*i]; T{1} = triangle(2); T{2} = triangle(2); T{3} = triangle(sqrt(2)); T{4} = triangle(1); T{5} = triangle(1); T{6} = square(sqrt(2)); T{7} = parallelogram(1); P = init_plot(T); if nargout > 0 Pout = P; end xit = init_buttons; while 1 drawnow if xit.Value == 0 return end end % ------------------------ function P = init_plot(T) % Initialize primary plot. clf shg h = 6; w = 8; set(gcf,'position',[100 100 800 600], ... 'numbertitle','off', ... 'menubar','none', ... 'name','Tangram') ax = axes( ... 'color',get(gcf,'color'), ... 'xlim',[-w w], ... 'ylim',[-h h], ... 'userdata',[]); set(gcf,'userdata',ax) str = "[" + int2str(w) + "," + int2str(h) + "]"; set(gcf,'tag',str) hold on axis off equal colors = dark_2; shift = [-2.25-1i 2.25-1i -2.5+1.5i 0.5+1i ... 3.5+1i -2.5+2i 2.5+4i]; for k = 1:7 t = T{k} + shift(k); P{k,1} = fill(real(t),imag(t),colors(k,:), ... 'markeredgecolor','black', ... 'linewidth',2, ... 'tag',int2str(k)); end x = [-w w w -w -w]; y = [-h -h h h -h]; plot(x,y,'k-','linewidth',3); end function xit = init_buttons(varargin) % Initialize buttons and callbacks. x = 0.79; y = 0.16; w = 0.10; h = 0.06; pusher([x y+.24 w h],'info',@info); pusher([x y+.16 w h],'restart',@restart_cb); pusher([x y+.08 w h],'flipper',@flipper); xit = pusher([x y w h],'exit',@close_cb); set(gcf, ... 'windowbuttonmotionfcn',@button_motion, ... 'windowbuttondownfcn',@button_down, ... 'windowbuttonupfcn',@button_up) end function uic = pusher(pos,str,cb) uic = uicontrol( ... 'units','normalized', ... 'position',pos, ... 'string',str, ... 'callback',cb, ... 'style','pushbutton', ... 'fontsize',12, ... 'fontweight','bold', ... 'backgroundcolor','w'); end function button_motion(varargin) % Button motion, either drag or rotate a piece. % Use complex arithmetic. point = get(gca,'currentpoint'); v = point(1,1) + point(1,2)*1i; hit = get(gca,'userdata'); if ~isempty(hit) && ishandle(hit) && hit ~= 1 u = get(hit,'xdata') + get(hit,'ydata')*1i; z = get(hit,'userdata'); % Check if closer to center or vertex. w = mean(u(1:end-1)); if abs(w-v) < min(abs(u-v)) % Drag u = u - (z - v); else % Rotate about the center by an integer % multiple of pi/20, which is 9 degrees. dtheta = pi/20; theta = angle(v-w) - angle(z-w); theta = round(theta/dtheta)*dtheta; omega = exp(1i*theta); u = omega*(u-w) + w; v = omega*(z-w) + w; end set(hit,'xdata',real(u),'ydata',imag(u),'userdata',v); end end function button_down(varargin) % Select a piece and remember it in userdata. point = get(gca,'currentpoint'); z = point(1,1) + point(1,2)*1i; delete(findobj(gca,'type','line')) h = flipud(get(gca,'children')); hit = []; for k = 1:length(h) x = get(h(k),'xdata'); y = get(h(k),'ydata'); if inregion(real(z),imag(z),x,y) hit = h(k); set(hit,'userdata',z) break end end set(gca,'userdata',hit) wh = str2num(get(gcf,'tag')); h = wh(2); w = wh(1); x = [-w w w -w -w]; y = [-h -h h h -h]; line(x,y,'color','k','linewidth',3) end function button_up(varargin) % Button up, snap to any nearby piece, then deselect. % Use complex arithmetic. delete(findobj(gca,'type','line')) hit = get(gca,'userdata'); set(gca,'userdata',[]) % Compute distance to nearest vertex of other pieces. z = get(hit,'xdata') + get(hit,'ydata')*1i; h = get(gca,'children'); w = []; for k = 1:length(h) if h(k) ~= hit w = [w; get(h(k),'xdata')+get(h(k),'ydata')*1i]; end end dz = 1; for k = 1:length(z) d = z(k)-w; dw = d(find(abs(d)==min(abs(d)),1)); if abs(dw) < abs(dz) dz = dw; end end wh = str2num(get(gcf,'tag')); h = wh(2); w = wh(1); x = [-w w w -w -w]; y = [-h -h h h -h]; line(x,y,'color','k','linewidth',3) % If close enough, snap to nearby piece. tol = 0.5; if abs(dz) < tol set(hit,'xdata',real(z-dz),'ydata',imag(z-dz)) hitcolor = get(hit,'facecolor'); set(hit,'facecolor',min(1.25*hitcolor,1.0)) pause(1.5) set(hit,'facecolor',hitcolor) end end function restart_cb(varargin) e = findobj('string','exit'); set(e,'value',0) P = Tangram; end function close_cb(varargin) e = findobj('string','exit'); set(e,'value',0) close(gcf) end function info(~,~) helpwin Tangram end end %% Test.m function Test swan pause(2) kitty pause(2) square pause(2) Tangram end %% kitty.m function kitty % kitty N = 50; paws = 0.5/N; P = start(kittycolors); pause(paws) for k = 1:N tanmove(P{2},0.0,-5/N) tanrot(P{2},-135/N) pause(paws) end for k = 1:N tanmove(P{7},-1.25/N,-8.25/N) pause(paws) end snap(P{7}) for k = 1:N tanmove(P{1},3.875/N,-2.75/N) tanrot(P{1},-90/N) pause(paws) end snap(P{1}) for k = 1:N tanmove(P{3},-6/N,-2.3/N) tanrot(P{3},90/N) pause(paws) end snap(P{3}) for k = 1:N tanmove(P{6},-2.5/N,-3/N) tanrot(P{6},-45/N) pause(paws) end snap(P{6}) for k = 1:N tanmove(P{4},2.7/N,-2.0/N) tanmove(P{5},1.1/N,-1.8/N) tanrot(P{4},-90/N) tanrot(P{5},90/N) pause(paws) end snap(P{4}) snap(P{5}) end %% square.m function square % square N = 50; paws = 0.5/N; P = start; pause(paws) for k = 1:N tanrot(P{1},-90/N) pause(paws) end for k = 1:N tanmove(P{2},-0.70/N,-3.75/N) pause(paws) end for k = 1:N tanmove(P{1},2.25/N,-2.30/N) pause(paws) end snap(P{1}) for k = 1:N tanmove(P{6},-1.21/N,-5.50/N) tanrot(P{6},-45/N) pause(paws) end snap(P{6}) for k = 1:N tanmove(P{4},4.0/N,-4.3/N) tanrot(P{4},-180/N) tanmove(P{5},2.5/N,-6.0/N) tanrot(P{5},90/N) pause(paws) end snap(P{4}) snap(P{5}) for k = 1:N tanmove(P{3},-3.1/N,-1.0/N) tanrot(P{3},-45/N) pause(paws) end snap(P{3}) for k = 1:N tanmove(P{7},-6.5/N,-1.75/N) pause(paws) end flipper for k = 1:N tanmove(P{7},1.2/N,-1.7/N) pause(paws) end snap(P{7}) end %% swan.m function swan % swan N = 50; paws = 0.5/N; P = start(dark_2); pause(paws) for k = 1:N tanmove(P{2},-0.5/N,-5.0/N) tanrot(P{2},-135/N) pause(paws) end for k = 1:N tanmove(P{1},3.0/N,-3.5/N) tanrot(P{1},180/N) pause(paws) end snap(P{1}) pause(paws) for k = 1:N tanmove(P{3},-3.0/N,-4.0/N) tanrot(P{3},-90/N) pause(paws) end snap(P{3}) for k = 1:N tanmove(P{4},6/N,-6.5/N) tanrot(P{4},90/N) pause(paws) end snap(P{4}) for k = 1:N tanmove(P{6},-0.75/N,-5.75/N) tanrot(P{6},-45/N) pause(paws) end snap(P{6}) for k = 1:N tanmove(P{7},-5.25/N,-3.5/N) tanrot(P{7},90/N) pause(paws) end flipper for k = 1:N tanmove(P{7},1.5/N,0) pause(paws) end snap(P{7}) for k = 1:N tanmove(P{5},3.0/N,-3.0/N) tanrot(P{5},135/N) pause(paws) end snap(P{5}) end %% lib\dark_2.m function dk2 = dark_2 dk2 = [ ... 27 158 119 217 95 2 117 112 179 231 41 138 102 166 30 230 171 2 167 118 28 102 102 102]/256; end %% lib\flipper.m function flipper(~,~) P7 = findobj(gca,'tag','7'); fc = P7.FaceColor; P7.FaceColor = fc/max(fc); x = P7.XData; x = 2*mean(x(1:end-1))-x; d = dir; str = string({d.name}'); if any(contains(str,".gif")) gif_frame else pause(1) end P7.XData = x; if any(contains(str,".gif")) gif_frame else pause(1) end P7.FaceColor = fc; if any(contains(str,".gif")) gif_frame else pause(1) end end %% lib\kittycolors.m function meow = kittycolors meow = ... [184 228 68 248 211 0 121 160 238 184 113 198 184 113 198 160 82 45 235 235 200]/256; end %% lib\snap.m function snap(hit) % Button up, snap to any nearby piece, then deselect. % Use complex arithmetic. % Compute distance to nearest vertex of other pieces. z = get(hit,'xdata') + get(hit,'ydata')*1i; h = get(gca,'children'); w = []; for k = 2:length(h) if h(k) ~= hit w = [w; get(h(k),'xdata')+get(h(k),'ydata')*1i]; end end dz = 1; for k = 1:length(z) d = z(k)-w; dw = d(find(abs(d)==min(abs(d)),1)); if abs(dw) < abs(dz) dz = dw; end end % If close enough, snap to nearby piece. tol = 0.5; if abs(dz) < tol set(hit,'xdata',real(z-dz),'ydata',imag(z-dz)) hitcolor = get(hit,'facecolor'); set(hit,'facecolor',min(1.25*hitcolor,1.0)) pause(1.5) set(hit,'facecolor',hitcolor) end end %% lib\start.m function P = start(colors) % Tangram Tangram puzzle % https://blogs.mathworks.com/cleve/2025/10/23/tangram-puzzles/ % Copyright 2025 Cleve Moler if nargin < 1 colors = dark_2; end i = 1i; triangle = @(s) [-s-s*i s-s*i 0 -s-s*i]; square = @(s) [0 s s+s*i s*i 0]; parallelogram = @(s) [-2*s-2*s*i 0-2*s*i s-s*i -s-s*i -2-2*s*i]; T{1} = triangle(2); T{2} = triangle(2); T{3} = triangle(sqrt(2)); T{4} = triangle(1); T{5} = triangle(1); T{6} = square(sqrt(2)); T{7} = parallelogram(1); clf shg h = 6; w = 8; set(gcf,'position',[100 100 800 600], ... 'numbertitle','off', ... 'menubar','none', ... 'name','Tangram') ax = axes( ... 'color',get(gcf,'color'), ... 'xlim',[-w w], ... 'ylim',[-h h], ... 'userdata',[]); set(gcf,'userdata',ax) str = "[" + int2str(w) + "," + int2str(h) + "]"; set(gcf,'tag',str) hold on axis off equal offset = [-4.25+3i 0.25+3i 4.25+2.5i -4.25+5i ... -1.25+5i 0.75+4i 4.75+6i]; for k = 1:7 t = T{k} + offset(k); P{k,1} = fill(real(t),imag(t),colors(k,:), ... 'markeredgecolor','black', ... 'linewidth',2, ... 'tag',int2str(k)); end x = [-w w w -w -w]; y = [-h -h h h -h]; plot(x,y,'k-','linewidth',3); end %% lib\tanmove.m function tanmove(who,x,y) v = get(who,'vertices'); w = v + [x,y]; set(who,'vertices',w) end %% lib\tanrot.m function tanrot(who,deg) v = get(who,'vertices'); mu = mean(v(1:end-1,:)); R = [cosd(deg) sind(deg); -sind(deg) cosd(deg)]; w = (v - mu)*R + mu; set(who,'vertices',w) end %% EOF