# Best way to create this image, square and angle

I have just started looking into tikz, and trying to create the simple image below. I took a stab at it but could not quite do it.

ABCD is a square and E is the midpoint on DC and F is the midpoint of BC

Ofcourse the labels should be a tad smaler, and closer to the actual figure. I am just trying to learn tikz, so a few examples how to create the image beautifuly would truly help me. Here is my attempt at it. Only using the simple tikzpackage.

I had mainly two problems creating this image

1) How do one place points in tex, and anchor text to them?

2) How do one create the angle... I know it is some arc, but yeah

\begin{figure}[h!] \centering
\begin{tikzpicture}[scale=3]
\coordinate [label=left:\textcolor{blue}{$A$}] (A) at (0.,0);
\coordinate [label=right:\textcolor{blue}{$B$}] (B) at (1,0);
\coordinate [label=right:\textcolor{blue}{$C$}] (C) at (1,1);
\coordinate [label=left:\textcolor{blue}{$D$}] (D) at (0,1);
\draw (0,0) -- (1,0);
\draw (0,0) -- (0,1);
\draw (1,0) -- (1,1);
\draw (0,1) -- (1,1);
\draw (0,0) -- (1,0.5);
\draw (0,0) -- (0.5,1);

\end{tikzpicture}
\end{figure}


I also guess there is some simple way to do this, like a built in square command or something...

-
The first example contained within the tikz/pgf documentation (p 30-31) contains some information on how to draw arcs. –  Werner Nov 13 '11 at 0:13

Below is a solution using tkz-euclide (as Regis da Silva mentioned in a comment that I only now saw).

Edit: I've changed it slightly. Here you can change the size of the square simply by changing the position of point B. The reason that I didn't use tkzDrawPolygon to draw the triangle, is that the corners would extend a little outside the rectangle. Uncomment that line to see what I mean. Also, if the position of B is (1,0) the arc showing the angle is too large, and then you will want to uncomment the line size=0.4.

\documentclass{article}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}
\tkzDefPoint(0,0){A}  \tkzLabelPoint[below left](A){A}
\tkzDefPoint(3,0){B}  \tkzLabelPoint[below right](B){B}
\tkzDefSquare(A,B)    \tkzGetPoints{C}{D}
\tkzDrawSquare(A,B)
\tkzLabelPoint[above right](C){C} \tkzLabelPoint[above left](D){D}
\tkzDefMidPoint(C,D) \tkzGetPoint{E}  \tkzLabelPoint[above](E){E}
\tkzDefMidPoint(B,C) \tkzGetPoint{F}  \tkzLabelPoint[right](F){F}
\tkzDrawSegments(A,E E,F A,F)
%\tkzDrawPolygon(A,E,F)
\tkzDrawPoints[fill=black](E,F)
\tkzMarkAngle[fill=red,%
%size=0.4,   % for small squares you may want to use this
opacity=0.4](F,A,E)
\tkzLabelAngle[pos=1.2](E,A,F){$\alpha$}
\end{tikzpicture}
\end{document}


The problem with this package is that the manual is only in French, as far as I know.

You can mix this with normal TikZ, code, so wh1t3's code can be modified slightly, using tkz-euclide to draw the arc.

Note that instead of using \textcolor for all the labels, you can edit the every label style. The updated example shows this, as well as how to change the color of a single label (note the extra set of braces).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}
\noindent
\begin{tikzpicture}[scale=3,every label/.style={color=blue}]
\coordinate [label=left:$A$] (A) at (0,0);
\coordinate [label=right:$B$] (B) at (1,0);
\coordinate [label=right:$C$] (C) at (1,1);
\coordinate [label=left:$D$] (D) at (0,1);
\coordinate [label=above:$E$] (E) at ($(D)!.5!(C)$);
\coordinate [label={[black]right:$F$}] (F) at ($(B)!.5!(C)$);
\draw (A) rectangle (C);
\draw [fill] (A) -- (E) circle[radius=.5pt]
\tkzMarkAngle[fill=red,opacity=.5,size=.3](F,A,E)
\tkzLabelAngle[pos=.4](E,A,F){$\alpha$}
\end{tikzpicture}
\end{document}


-

You can use the rectangle command and some commands from the calc library to do this. First the code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\noindent
\begin{tikzpicture}[scale=3]
\coordinate [label=left:\textcolor{blue}{$A$}] (A) at (0,0);
\coordinate [label=right:\textcolor{blue}{$B$}] (B) at (1,0);
\coordinate [label=right:\textcolor{blue}{$C$}] (C) at (1,1);
\coordinate [label=left:\textcolor{blue}{$D$}] (D) at (0,1);
\coordinate [label=above:\textcolor{blue}{$E$}] (E) at ($(D)!.5!(C)$);
\coordinate [label=right:\textcolor{blue}{$F$}] (F) at ($(B)!.5!(C)$);
\path[draw] (A) rectangle (C);
\path[draw, fill] (A) -- (E) circle[black, radius=.5pt];
\path[draw, fill] (A) -- (F) circle[black, radius=.5pt];
\path[draw] (E) -- (F);
\path[draw, fill=red!40] (A) -- ++({atan(.5)}:.3) arc ({atan(.5)}:{90-atan(.5)}:.3) -- (A);
\path[draw] ++(45:.4) node {$\alpha$};
\end{tikzpicture}
\end{document}


Now come explanation. The E and F coordinates are computed using partway modifiers. That's the ! notation you are seeing. It says take the point that's x% on the line from A to B. So in our case, we take the point that's 50% or 0.5 between points D and C or B and C to get E and F respectively. Then we use the rectangle command and some separate paths to draw the lines. Finally we draw the arc. Note that we need to draw the lines as well (or at least one of them) to get a good fill (TikZ fills paths by closing them, try it without drawing both lines to A). We draw both of them because the arc is drawn last and thus on top of existing lines. We simply use some math (the inverse tangent) to get the right angle. Finally we place the node by putting it at .4cm (scaled, so 1.2cm, really) at an angle of 45 degrees from the origin. The resulting picture:

To demonstrate why I think the arc approach is preferred for dealing with angles, consider the case where you want to draw all the angles around A. So BAF, FAE and EAD. When you use clipping you have to use different scopes for each and it doesn't give you any additional information about the actual angle. If you want to print the degrees of the angles, for instance, you would still need to do calculations. Whereas when using tan you have this information, because you are already calculating it. For the three angles it would look like this:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\noindent
\begin{tikzpicture}[scale=3]
\coordinate [label=left:\textcolor{blue}{$A$}] (A) at (0,0);
\coordinate [label=right:\textcolor{blue}{$B$}] (B) at (1,0);
\coordinate [label=right:\textcolor{blue}{$C$}] (C) at (1,1);
\coordinate [label=left:\textcolor{blue}{$D$}] (D) at (0,1);
\coordinate [label=above:\textcolor{blue}{$E$}] (E) at ($(D)!.5!(C)$);
\coordinate [label=right:\textcolor{blue}{$F$}] (F) at ($(B)!.5!(C)$);
\pgfmathsetmacro{\firstAngle}{int(atan(.5)*100)/100}
\path[draw,green,fill=green!40] (A) -- ++(0:.3) arc[start angle=0, delta angle=\firstAngle,radius=.3];
\path[draw] ++(12:.45) node[rotate=12] {\tiny\firstAngle$^\circ$};
\pgfmathsetmacro{\secondAngle}{round((atan(2)-atan(.5))*100)/100}
\path[draw,red, fill=red!40] (A) -- ++(\firstAngle:.3) arc[start angle=\firstAngle, delta angle=\secondAngle,radius=.3];;
\path[draw] ++(45:.45) node[rotate=45] {\tiny\secondAngle$^\circ$};
\pgfmathsetmacro{\thirdAngle}{int((90-atan(2))*100)/100}
\path[draw,blue,fill=blue!40] (A) -- ++(\firstAngle+\secondAngle:.3) arc[end angle=90, delta angle=\thirdAngle, radius=.3];
\path[draw] ++(78:.45) node[rotate=78] {\tiny\thirdAngle$^\circ$};
\path[draw] (A) rectangle (C);
\path[draw, fill] (A) -- (E) circle[black, radius=.5pt];
\path[draw, fill] (A) -- (F) circle[black, radius=.5pt];
\path[draw] (E) -- (F);
\end{tikzpicture}
\end{document}


And the resulting picture:

Mind you, this is all perfectly possible with the clipping approach as well. It does require different scopes for all of the clipping and you still need to do the calculations if you wish to print the angles though.

-
I am not sure which answer I should accept. As i used somewhat of a combination of both. I really liked the way you made the square, and showed me how to calculate the midpoint. Alas the method of using tan displeases me. So i used the other method to make the angle. –  N3buchadnezzar Nov 13 '11 at 0:55
Another option is to use the package tkz-euclide. –  Regis da Silva Nov 13 '11 at 1:57

\begin{figure}[h!] \centering
\begin{tikzpicture}[scale=3]
\coordinate [label=left:\textcolor{blue}{$A$}] (A) at (0.,0);
\coordinate [label=right:\textcolor{blue}{$B$}] (B) at (1,0);
\coordinate [label=right:\textcolor{blue}{$C$}] (C) at (1,1);
\coordinate [label=left:\textcolor{blue}{$D$}] (D) at (0,1);
\coordinate [label=above:\textcolor{blue}{$E$}] (E) at (0.5,1);
\coordinate [label=right:\textcolor{blue}{$F$}] (F) at (1,0.5);

\draw (A) -- (B);
\draw (A) -- (D);
\draw (B) -- (C);
\draw (C) -- (D);
\draw (A) -- (E);
\draw (A) -- (F);
\draw (E) -- (F);

\fill (E) circle (1pt);
\fill (F) circle (1pt);

\begin{scope}
\path[clip] (A) -- (E) -- (F) -- cycle;
\draw [red, fill=red!20] (A) circle (10pt);
\end{scope}

\end{tikzpicture}
\end{figure}


This gives the result

-

I propose the following two lines of tikz code just to get the figure with no labels!

\begin{tikzpicture}
\draw[make begin point and middle of lineto a coordinate =
{Middle}{Edge}] (0,0) rectangle ++(5,5);
\draw[mark angle] (Middle-3) -- (Edge-1) -- (Middle-2) -- cycle;
\end{tikzpicture}


So you want the labels. Ok, it'll take a few more lines of code, using a \foreach loop based on the node the two above lines of code created.

\begin{tikzpicture}
\draw[make begin point and middle of lineto a coordinate =
{Middle}{Edge}] (0,0) rectangle ++(5,5);
% Labels
\foreach \i/\NameEdge/\NameMiddle/\PosEdge/\PosMiddle in
{1/A//left/,2/D/E/left/above,3/C/F/right/right,4/B//right/} {%
\node[circle,fill,blue,inner sep=1pt,label=\PosEdge:$\NameEdge$] at
(Edge-\i) {};
\unless\ifx\NameMiddle\MyEmpty
\node[circle,fill,red,inner
sep=1pt,label=\PosMiddle:$\NameMiddle$] at (Middle-\i) {};
\fi}
% Segments and angles
\draw[mark angle] (Middle-3) -- (Edge-1) -- (Middle-2) -- cycle;
\end{tikzpicture}


How simple it is to work with tikz, isn't it! However, this won't work out of the box and I'm going to take you to a tour to the decorations.pathreplacing library, that is a really powerful tool (remember, in tikz/pgf, nearly everything is custumisable at user level).

Here is the complete code. Yes it might seem complex at first sight, but remember: the decoration library is a powerful tool so you have from time to time to deal with complex things ;-)

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathreplacing}

\tikzset{%
make begin point and middle of lineto a coordinate/.style 2 args= {%
postaction = {%
/utils/exec = {\gdef\pgf@lib@decorations@countmacro{0}},
decorate,
decoration = {%
show path construction,
lineto code = {%
\xdef\pgf@lib@decorations@countmacro{%
\number\numexpr\pgf@lib@decorations@countmacro+1\relax}%
\path (\tikzinputsegmentfirst) -- node[coordinate]
(#1-\pgf@lib@decorations@countmacro) {} (\tikzinputsegmentlast);
\node[coordinate] (#2-\pgf@lib@decorations@countmacro) at
(\tikzinputsegmentfirst) {};},
closepath code={%
\xdef\pgf@lib@decorations@countmacro{%
\number\numexpr\pgf@lib@decorations@countmacro+1\relax}%
\path (\tikzinputsegmentfirst) -- node[coordinate]
(#1-\pgf@lib@decorations@countmacro) {} (\tikzinputsegmentlast);
\node[coordinate] (#2-\pgf@lib@decorations@countmacro) at
(\tikzinputsegmentfirst) {};}}}}}

\makeatletter

\pgfdeclaredecoration{mark angle}{init}{%
\state{init}[width = 0pt, next state = check for moveto,
persistent precomputation = {%
\xdef\pgf@lib@decorations@numofconsecutivelineto{0}}]{}
\state{check for moveto}[width = 0pt,
next state=check for lineto,persistent precomputation={%
\begingroup
\pgf@lib@decoraions@installinputsegmentpoints
\ifx\pgfdecorationpreviousinputsegment\pgfdecorationinputsegmentmoveto
\gdef\pgf@lib@decorations@numofconsecutivelineto{0}%
\fi
\endgroup}]{}
\state{check for lineto}[width=\pgfdecoratedinputsegmentremainingdistance,
next state=check for moveto,persistent precomputation={%
\begingroup
\pgf@lib@decoraions@installinputsegmentpoints
\ifx\pgfdecorationcurrentinputsegment\pgfdecorationinputsegmentlineto
\xdef\pgf@lib@decorations@numofconsecutivelineto{%
\number\numexpr\pgf@lib@decorations@numofconsecutivelineto+1\relax}%
\ifcase\pgf@lib@decorations@numofconsecutivelineto\relax
\or
\pgf@process{\pgf@decorate@inputsegment@first}%
\xdef\pgf@lib@decorations@first@lineto@point{\the\pgf@x,\the\pgf@y}%
\pgf@process{\pgf@decorate@inputsegment@last}%
\xdef\pgf@lib@decorations@second@lineto@point{\the\pgf@x,\the\pgf@y}%
\pgfmathanglebetweenpoints{\pgf@decorate@inputsegment@last}{%
\pgf@decorate@inputsegment@first}%
\xdef\pgf@lib@decorations@lineto@startangle{\pgfmathresult}%
\or
\pgf@process{\pgf@decorate@inputsegment@last}%
\xdef\pgf@lib@decorations@third@lineto@point{\the\pgf@x,\the\pgf@y}%
\pgfmathanglebetweenpoints{\pgf@decorate@inputsegment@first}{%
\pgf@decorate@inputsegment@last}%
\xdef\pgf@lib@decorations@lineto@endangle{\pgfmathresult}%
\pgfdecoratedmarkanglecode
\fi
\fi
\endgroup}]{}
}

\pgfqkeys{/pgf/decoration}{%
mark angle code/.store in = \pgfdecoratedmarkanglecode,
mark angle code = {%
\fill[red,nearly transparent]
(\pgf@lib@decorations@second@lineto@point) --
($(\pgf@lib@decorations@second@lineto@point)!1cm! (\pgf@lib@decorations@first@lineto@point)$)
arc(\pgf@lib@decorations@lineto@startangle:
\pgf@lib@decorations@lineto@endangle:1cm) -- cycle;
\node at ($(\pgf@lib@decorations@second@lineto@point) + ({\pgf@lib@decorations@lineto@startangle + (\pgf@lib@decorations@lineto@endangle - \pgf@lib@decorations@lineto@startangle)/2}:1.25cm)$) {$\alpha$};}}

\makeatletter

\tikzset{mark angle/.style = {%
postaction = {%
decorate,
decoration = {mark angle}}}}

\def\MyEmpty{}

\begin{document}
\begin{tikzpicture}
\draw[make begin point and middle of lineto a coordinate =
{Middle}{Edge}] (0,0) rectangle ++(5,5);
% Labels
\foreach \i/\NameEdge/\NameMiddle/\PosEdge/\PosMiddle in
{1/A//left/,2/D/E/left/above,3/C/F/right/right,4/B//right/} {%
\node[circle,fill,blue,inner sep=1pt,label=\PosEdge:$\NameEdge$] at
(Edge-\i) {};
\unless\ifx\NameMiddle\MyEmpty
\node[circle,fill,red,inner
sep=1pt,label=\PosMiddle:$\NameMiddle$] at (Middle-\i) {};
\fi}
% Segments and angles
\draw[mark angle] (Middle-3) -- (Edge-1) -- (Middle-2) -- cycle;
\end{tikzpicture}
\end{document}

-

This is a solution with PSTricks just for comparison purpose. Note that the \pstMarkAngle*[fillcolor=<color name>]{}{}{}{} was added to the package a few hours ago, so make sure to update your package (from DANTE now or from other repositories several days later) before compiling the code below.

\documentclass[pstricks,border=15pt]{standalone}
\usepackage{pst-eucl}
\begin{document}
\begin{pspicture}(4,4)
\pstGeonode[CurveType=polygon,PointSymbol=none,PosAngle={-135,-45,45,135}](0,0){A}(4,0){B}(4,4){C}(0,4){D}
\pstMiddleAB{C}{B}{F}
\pstMiddleAB{D}{C}{E}
\psset{linejoin=2}
\pspolygon(A)(E)(F)
\pstMarkAngle*[MarkAngleRadius=0.6,fillcolor=red!50]{F}{A}{E}{$\alpha$}
\end{pspicture}
\end{document}


## Animated version:

\documentclass[pstricks,border={23pt 23pt 15pt 15pt}]{standalone}
\usepackage{pst-eucl}
\begin{document}
\multido{\i=0+20}{18}{%
\begin{pspicture}(4,4)
\pstGeonode[CurveType=polygon,PointSymbol=none,PosAngle={-135,-45,45,135}]
\pscircle[linestyle=dashed,linecolor=lightgray](1,1){!2 sqrt}
\pstMiddleAB{C}{B}{F}
\pstMiddleAB{D}{C}{E}
\psset{linejoin=2}
\pspolygon(A)(E)(F)
\pstMarkAngle*[MarkAngleRadius=0.6,fillcolor=red!50]{F}{A}{E}{$\alpha$}
\end{pspicture}}
\end{document}

-

Here is yet another TikZ solution with an emphasis on the compactness: (After wh1t3's further trim)

\begin{tikzpicture}
\node[draw,inner sep=2cm,label=45:$C$,label=135:$D$,label=225:$A$,label=-45:$B$] (squ) at (0,0) {};
\draw (squ.225) -- (squ.90) node[draw,circle,fill=red,inner sep=1pt,label={90:$E$}] {};
\draw (squ.225) -- (squ.0) node[draw,circle,fill=red,inner sep=1pt,label={0:$F$}] {};
\draw[fill=red!15] (squ.225) -- ++({atan(0.5)}:1cm) arc ({atan(.5)}:{atan(2)}:1cm)--cycle;
\path (squ.225) ++(45:1.2cm) node {$\alpha$};
\end{tikzpicture}


I just tried to see if I can shorten those neat answers without going too specialized. If you play with the inner sep=2cm on line 3, it even gives you a rough handle for customization. It can further be improved but since you have full solutions given, I would leave it to your taste.

-
You can lose the calc lib like this, even a little shorter. \begin{tikzpicture} \node[draw,inner sep=2cm,label=45:$C$,label=135:$D$,label=225:$A$,label=-45:$B$] (squ) at (0,0) {}; \draw (squ.225) -- (squ.90) node[draw,circle,fill=red,inner sep=1pt,label={90:$E$}] {}; \draw (squ.225) -- (squ.0) node[draw,circle,fill=red,inner sep=1pt,label={0:$F$}] {}; \draw[fill=red!15] (squ.225) -- ++({atan(0.5)}:1cm) arc ({atan(.5)}:{atan(2)}:1cm)--cycle; \path (squ.225) ++(45:1.2cm) node {$\alpha$}; \end{tikzpicture} –  Roelof Spijker Nov 14 '11 at 7:59
@wh1t3 Ah, looks like the most obvious thing once you said it. How did I miss that? :) Thanks. –  percusse Nov 14 '11 at 8:42