# How to draw squares on the sides of a quadrilateral and triangle in tikz?

How to draw squares on the sides of a quadrilateral and triangle in tikz? I want to replicate the 2 images below:

• again a MWE maybe? Aug 4 '14 at 17:36
• These diagrams are from Tristan Needham's Visual Complex Analysis (OUP, 1997, ISBN 9780198534464) by the way. Page 16 in my edition. In the preface the author states that he drew most of the illustrations with Coreldraw. Aug 4 '14 at 22:04

Using tkz-euclide this is easy:

The code:

\documentclass[border=3pt]{standalone}
\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}

\begin{tikzpicture}[>=latex,baseline]
\tkzDefPoint(0,0){A}
\tkzDefPoint(2,2){B}
\tkzDefPoint(1,4){C}
\tkzDefPoint(-2,3){D}

\tkzDefSquare(B,A)\tkzGetPoints{E}{F}
\tkzDefSquare(C,B)\tkzGetPoints{G}{H}
\tkzDefSquare(D,C)\tkzGetPoints{I}{J}
\tkzDefSquare(A,D)\tkzGetPoints{K}{L}

\tkzFillPolygon[draw,fill = red!50 ](A,B,F,E)
\tkzFillPolygon[draw,fill = blue!50 ](C,B,G,H)
\tkzFillPolygon[draw,fill = purple!50](C,D,J,I)
\tkzFillPolygon[draw,fill = green!50](D,A,L,K)
\tkzFillPolygon[draw,opacity=.5,fill = orange](A,B,C,D)

\tkzInterLL(A,F)(B,E)
\tkzGetPoint{P}
\tkzDrawPoint(P)

\tkzInterLL(B,H)(C,G)
\tkzGetPoint{Q}
\tkzDrawPoint(Q)

\tkzInterLL(C,J)(D,I)
\tkzGetPoint{R}
\tkzDrawPoint(R)

\tkzInterLL(D,L)(A,K)
\tkzGetPoint{S}
\tkzDrawPoint(S)

\tkzDrawSegments[->,thick](R,P Q,S)

\tkzInterLL(P,R)(Q,S)
\tkzGetPoint{T}

\tkzMarkRightAngle(R,T,S)

\tkzLabelPoint[below](P){$r$}
\tkzLabelPoint[right](Q){$q$}
\tkzLabelPoint[above](R){$p$}
\tkzLabelPoint[left](S){$s$}

\tkzLabelPoint[left=5pt](D){$0$}

\tkzDrawSegment[->,thick](D,C)
\tkzLabelPoint[below=2pt](C){$2a$}
\tkzDrawSegment[->,thick](C,B)
\tkzLabelPoint[left=2pt](B){$2b$}
\tkzDrawSegment[->,thick](B,A)
\tkzLabelPoint[above=4pt](A){$2c$}
\tkzDrawSegment[->,thick](A,D)
\tkzLabelPoint[right=4pt](D){$2d$}

\begin{scope}[xshift=11cm]
\tkzDefPoint(0,0){A}
\tkzDefPoint(1,4){B}
\tkzDefPoint(-2,3){C}

\tkzDefSquare(C,B)\tkzGetPoints{D}{E}
\tkzDefSquare(A,C)\tkzGetPoints{F}{G}

\tkzFillPolygon[draw,fill = purple!50](B,C,E,D)
\tkzFillPolygon[draw,fill = green!50](C,A,G,F)
\tkzFillPolygon[draw,opacity=.5,fill = orange](A,B,C)

\tkzInterLL(C,D)(B,E)
\tkzGetPoint{P}
\tkzDrawPoint(P)

\tkzInterLL(A,F)(C,G)
\tkzGetPoint{Q}
\tkzDrawPoint(Q)

\tkzDefMidPoint(A,B)
\tkzGetPoint{R}
\tkzDrawPoint(R)

\tkzDrawSegments[dashed](Q,R R,P)
\tkzMarkRightAngle(Q,R,P)

\tkzLabelPoint[below](Q){$s$}
\tkzLabelPoint[right](R){$m$}
\tkzLabelPoint[left](P){$p$}
\end{scope}
\end{tikzpicture}

\end{document}


And the more sober version changing the filling colors to gray!50 and white, adding the frames and the labels:

\documentclass[border=3pt]{standalone}
\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}

\begin{tikzpicture}[>=latex,baseline]
\tkzDefPoint(0,0){A}
\tkzDefPoint(2,2){B}
\tkzDefPoint(1,4){C}
\tkzDefPoint(-2,3){D}

\tkzDefSquare(B,A)\tkzGetPoints{E}{F}
\tkzDefSquare(C,B)\tkzGetPoints{G}{H}
\tkzDefSquare(D,C)\tkzGetPoints{I}{J}
\tkzDefSquare(A,D)\tkzGetPoints{K}{L}

\tkzFillPolygon[draw,fill = gray!50 ](A,B,F,E)
\tkzFillPolygon[draw,fill = gray!50 ](C,B,G,H)
\tkzFillPolygon[draw,fill = gray!50](C,D,J,I)
\tkzFillPolygon[draw,fill = gray!50](D,A,L,K)
\tkzFillPolygon[draw,opacity=.5,fill = white](A,B,C,D)

\tkzInterLL(A,F)(B,E)
\tkzGetPoint{P}
\tkzDrawPoint(P)

\tkzInterLL(B,H)(C,G)
\tkzGetPoint{Q}
\tkzDrawPoint(Q)

\tkzInterLL(C,J)(D,I)
\tkzGetPoint{R}
\tkzDrawPoint(R)

\tkzInterLL(D,L)(A,K)
\tkzGetPoint{S}
\tkzDrawPoint(S)

\tkzDrawSegments[->,thick](R,P Q,S)

\tkzInterLL(P,R)(Q,S)
\tkzGetPoint{T}

\tkzMarkRightAngle(R,T,S)

\tkzLabelPoint[below](P){$r$}
\tkzLabelPoint[right](Q){$q$}
\tkzLabelPoint[above](R){$p$}
\tkzLabelPoint[left](S){$s$}

\tkzLabelPoint[left=5pt](D){$0$}

\tkzDrawSegment[->,thick](D,C)
\tkzLabelPoint[below=2pt](C){$2a$}
\tkzDrawSegment[->,thick](C,B)
\tkzLabelPoint[left=2pt](B){$2b$}
\tkzDrawSegment[->,thick](B,A)
\tkzLabelPoint[above=4pt](A){$2c$}
\tkzDrawSegment[->,thick](A,D)
\tkzLabelPoint[right=4pt](D){$2d$}

\node[font=\bfseries]
at (current bounding box.north west)
{[a]};
\draw[densely dotted,thin,rounded corners=10pt]
([shift={(-10pt,10pt)}]current bounding box.north west)
rectangle
([shift={(10pt,-10pt)}]current bounding box.south east);
\begin{tikzpicture}[>=latex,baseline]
\tkzDefPoint(0,0){A}
\tkzDefPoint(1,4){B}
\tkzDefPoint(-2,3){C}

\tkzDefSquare(C,B)\tkzGetPoints{D}{E}
\tkzDefSquare(A,C)\tkzGetPoints{F}{G}

\tkzFillPolygon[draw,fill = gray!50](B,C,E,D)
\tkzFillPolygon[draw,fill = gray!50](C,A,G,F)
\tkzFillPolygon[draw,opacity=.5,fill = white](A,B,C)

\tkzInterLL(C,D)(B,E)
\tkzGetPoint{P}
\tkzDrawPoint(P)

\tkzInterLL(A,F)(C,G)
\tkzGetPoint{Q}
\tkzDrawPoint(Q)

\tkzDefMidPoint(A,B)
\tkzGetPoint{R}
\tkzDrawPoint(R)

\tkzDrawSegments[dashed](Q,R R,P)
\tkzMarkRightAngle(Q,R,P)

\tkzLabelPoint[below](Q){$s$}
\tkzLabelPoint[right](R){$m$}
\tkzLabelPoint[left](P){$p$}

\node[font=\bfseries]
at (current bounding box.north west)
{[b]};
\draw[densely dotted,thin,rounded corners=10pt]
([shift={(-10pt,10pt)}]current bounding box.north west)
rectangle
([shift={(10pt,-10pt)}]current bounding box.south east);
\end{tikzpicture}

\end{document}


And in plain Metapost, showing

• how to set the labels in Times New Roman (as in the OP)
• how to share paths and points between two sub-figures
• how to put neat rounded boxes round the sub-figures
• how to do open-dot labels

and so on...

prologues := 3;
outputtemplate := "%j%c.eps";
verbatimtex
%&latex
\documentclass{minimal}
\usepackage{amssymb}
\usepackage{mathptmx}
\begin{document}
etex
defaultfont := "ptmr8r";

vardef almost expr p = p cutafter fullcircle scaled ahlength shifted point infinity of p enddef;

vardef open_dotlabel@#(expr s,z) text t_ =
label@#(s,z) t_;
interim linecap:=rounded;
undraw z withpen pencircle scaled dotlabeldiam;
draw fullcircle scaled dotlabeldiam shifted z t_;
enddef;

def frame_and_label(expr s) =
label(s,ulcorner currentpicture);
draw (left--right) scaled 10 shifted urcorner currentpicture withcolor background;
draw quartercircle rotated   0 scaled 1cm shifted urcorner currentpicture
-- quartercircle rotated  90 scaled 1cm shifted ulcorner currentpicture
-- quartercircle rotated 180 scaled 1cm shifted llcorner currentpicture
-- quartercircle rotated 270 scaled 1cm shifted lrcorner currentpicture
-- cycle dashed withdots scaled .2 withcolor .4 white;
enddef;

beginfig(12);
u := 8mm;
z0 = z4 = origin;
z1 =      (3.0u,0) rotated 20;
z2 = z1 + (2.0u,0) rotated -60;
z3 = z2 + (2.1u,0) rotated -140;
path s[], a[];
forsuffixes $=0,1,2,3: s$ = unitsquare scaled (length (z[$+1]-z$)) rotated (angle (z[$+1]-z$)) shifted z$; z[$+5] = .5[point 0 of s$,point 2 of s$];
endfor
a1 = z5--z7;
a2 = z6--z8;
z9 = a1 intersectionpoint a2;

picture figa, figb;
figa = image(
forsuffixes $=0,1,2,3: fill s$ withcolor .7 white;
draw s$; drawarrow z$ -- z[$+1] if$=3: cutafter fullcircle scaled 3 fi;
endfor
draw unitsquare scaled 5 rotated (angle (z5-z7)) shifted z9 withcolor .5 white;
drawarrow almost a1;
drawarrow almost a2;

open_dotlabel.top(btex $p$ etex, z5);
open_dotlabel.rt (btex $q$ etex, z6);
open_dotlabel.bot(btex $r$ etex, z7);
open_dotlabel.lft(btex $s$ etex, z8);
open_dotlabel.lft(btex $0$ etex, z0);

label.bot (btex $\scriptstyle 2a$ etex, .9[z0,z1]);
label.lft (btex $\scriptstyle 2b$ etex, .9[z1,z2]);
label.ulft(btex $\scriptstyle 2c$ etex, .8[z2,z3]);
label.urt (btex $\scriptstyle 2d$ etex, .85[z3,z4]);
label.urt (btex $B$ etex, point .85 of a1);
label.ulft(btex $A$ etex, point .85 of a2);

frame_and_label("[a]");
);
figb = image(
forsuffixes $=0,3: fill s$ withcolor .7 white;
draw s$; endfor draw point 1 of s0 -- point 0 of s3; draw unitsquare scaled 5 rotated (angle (z5-z7)) shifted z9 withcolor .5 white; draw z5 -- z9 -- z8 dashed evenly; open_dotlabel.top(btex$p$etex, z5); open_dotlabel.lft(btex$s$etex, z8); open_dotlabel.lrt(btex$m\$ etex, z9);

frame_and_label("[b]");
);

draw figa;
draw figb shifted (10u,0);

endfig.

end.


Just for fun with PSTricks. I starts from the simpler one first. The other diagram will be added later.

## The second diagram

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-eucl}
\newpsobject{myframe}{psframe}{fillstyle=solid,fillcolor=lightgray,opacity=0.5}

\begin{document}
\begin{pspicture}[showgrid=false,saveNodeCoors,NodeCoorPrefix=N](7,10)
\pstGeonode[PosAngle={90,-135}](4,7){p}(3,3){s}
\psset{PointName=none,PointSymbol=none}
\pstGeonode(4,5){r}
\pstMiddleAB{p}{s}{c}
\pstRotation[RotAngle=90]{p}{r}[qtemp]\pnode(qtemp){q}
\pstRotation[RotAngle=-90]{s}{r}[ttemp]\pnode(ttemp){t}
\rput{!Nqy Nry sub Nqx Nrx sub atan}(r){\myframe[](!Nqx Nqy Nrx Nry Pyth2 dup)}
\rput{!Nty Nry sub Ntx Nrx sub atan 90 sub}(r){\myframe(!Ntx Nty Nrx Nry Pyth2 dup)}
\psline(t)(q)
\pstInterLC[PointNameB=m,PointSymbolB=*]{t}{q}{c}{p}{M}{N}
\psline[linestyle=dashed](s)(N)(p)
\pstRightAngle{s}{N}{p}
\end{pspicture}
\end{document}