# Filling angles with arcs in TikZ

Using TikZ, how can I fill in an angle of a given polygon with a colored arc? For example, given the triangle

\filldraw[fill=yellow, draw=black] (10,0) -- (-2,9) -- (4,-3) -- (10,0);

I'd like a blue arc of radius 1 centered on (10,0) sweeping out the angle bounded by the sides of the triangle.

Edit: I forgot an important part of my question, which (I think) makes the links provided in the comments insufficient.

How can I fill only half the angle with a shaded arc?

-
–  Torbjørn T. Jan 9 '12 at 21:19
This has nothing to do with the question but do you know you can use cycle? This tikz tool let's you draw polygons without having to type the start/end point twice: \draw[draw=black] (10,0)--(-2,9)--(4,-3)--cycle;. This might seem trivial (only one less character to type in your case), but it comes in very handy when say you want to change the start/end-point from (10,0) to some other coordinate. Tikz is all about that: doing everything relative to only a small number of expressly defined points. This prevents that small adding small changes will force you to do all the work over again. –  romeovs Jan 9 '12 at 21:30
The very first tutorial in the excellent TikZ manual is giving the recipe for it. :) –  Count Zero Jan 9 '12 at 21:40
@CountZero I agree that I could probably do it using the methods of that example, however, I suspect there is a cleaner way. –  Quinn Culver Jan 9 '12 at 22:18
cycle also closes the path. This isn't really noticeable on the triangle in question but you can see the effect if you enlarge the line width. The third corner isn't properly closed as-is, but is nicely closed if cycle is used. –  Loop Space Jan 10 '12 at 9:41

Here is something that gets the job done using basic tikz commands. One of the experts will probably be able to make something nice of it. One major problem is the limitations on number size in tikz computations. The triangle you asked about has dimensions that make the computations too big for tikz to handle properly (pstricks wouldn't have that problem), that is why I used another triangle.

Once the coordinates for the corners are given, the angles are computed automatically, using the let operation. The code is

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}

\coordinate (A) at (1,0);
\coordinate (B) at (-2,1);
\coordinate (C) at (2,3);

\filldraw[fill=yellow, draw=black] (A) node {$\bullet$} -- (B) -- (C) -- cycle;

\draw[->] let \p{AB} = ($(B)-(A)$),
\p{AC} = ($(C)-(A)$),
\n{lAB} = {veclen(\x{AB},\y{AB})},
\n{lAC} = {veclen(\x{AC},\y{AC})},
\n{angAB} = {atan2(\x{AB},\y{AB})},
\n{cos} = {(\x{AB}*\x{AC}+\y{AB}*\y{AC})/(\n{lAB}*\n{lAC})},
\n{angle} = {acos(\n{cos})} in
($(A)+0.2*(\p{AB})$) arc[radius={0.2*\n{lAB}},start angle=\n{angAB},delta angle=-\n{angle}];

\end{tikzpicture}

\end{document}


and the result is

To actually get a (half-)shaded arc, replace the draw command by the following code

\shadedraw[fill=blue] let \p{AB} = ($(B)-(A)$),
\p{AC} = ($(C)-(A)$),
\n{lAB} = {veclen(\x{AB},\y{AB})},
\n{lAC} = {veclen(\x{AC},\y{AC})},
\n{angAB} = {atan2(\x{AB},\y{AB})},
\n{cos} = {(\x{AB}*\x{AC}+\y{AB}*\y{AC})/(\n{lAB}*\n{lAC})},
\n{angle} = {acos(\n{cos})} in
(A) -- ++ ($0.2*(\p{AB})$) arc[radius={0.2*\n{lAB}},start angle=\n{angAB},delta angle={-\n{angle}/2}] -- cycle;


With this code, the result is

Another modification of the code, that does not use cosine and arccosine is

\draw[->] let \p{AB} = ($(B)-(A)$),
\p{AC} = ($(C)-(A)$),
\n{lAB} = {veclen(\x{AB},\y{AB})},
\n{lAC} = {veclen(\x{AC},\y{AC})},
\n{angAB} = {atan2(\x{AB},\y{AB})},
\n{angAC} = {atan2(\x{AC},\y{AC})},
\n{angle} = {mod(\n{angAB}-\n{angAC},180)} in
($(A)+0.2*(\p{AB})$) arc[radius={0.2*\n{lAB}},start angle=\n{angAB},delta angle=-\n{angle}];

-
This is almost angle bisector theorem. Sweet! –  percusse Jan 9 '12 at 23:57

I can give an example about how you can bisect an angle but I feel that it might not pass your cleanness threshold but it is not that demanding. Anyway here it is:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (c) at (2,2);
\coordinate (b) at (3,0);
\draw (a) node[left] {A} -- (b) node[right] {B} -- (c) node[above] {C} -- cycle;
\draw[fill=black!20] (c) -- ($(c)!8mm!(b)$) to[bend left] ($(c)!8mm!(a)$)  -- cycle;

\path ($(a)!1cm!(c)$) to coordinate [midway] (h) ($(a)!1cm!(b)$); %Get the bisector coord.
\shadedraw (a) -- (h) to[bend left=20] ($(a)!9mm!(b)$)  -- cycle; %Shade the result
\end{tikzpicture}
\end{document}


This gives the following:

I forgot to use your coordinates but it only requires to change the location of your node labels.

-
This is equivalent to my solution (so I shan't post mine). The main difference was that I put it all in one command since you can nest calc calculations. My key line was thus: \filldraw[fill=cyan] (c) -- ($(c)!\angrad!(a)$) to[bend right] ($(c)!\angrad!($($(c)!1cm!(b)$)!.5!($(c)!1cm!(a)$)$)$) -- cycle; (here \angrad is the radius of the arc). I also like (in your solution) putting the coordinates first and then defining everything else in terms of those. –  Loop Space Jan 10 '12 at 9:53
@AndrewStacey I have no doubt that you would come up with something nicer, but for this particular example it wasn't readable. Behind closed doors, I would probably make a one liner that even I can't decode afterwards. –  percusse Jan 10 '12 at 13:45
Oh, I completely agree that for this particular example it is completely unreadable! I mentioned it more to point out the possibility than anything else (some strange people find TikZ's verbosity off-putting). Functionally, there's no difference between what I came up with and your answer. –  Loop Space Jan 10 '12 at 19:06
Is there a way to "coordinatize" the 8mm or 1cm? I had to change the value to 80mm in my picture. I'd like a specific radius in "coordinate point lengths". –  rubenvb Jan 18 '13 at 13:27
Nvm, I guess $(a)!.1!(b)$ would do :) –  rubenvb Jan 18 '13 at 13:32

You can draw the whole circle and use clipping to restrict it to a sector:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (10,0);
\coordinate (b) at (-2,9);
\coordinate (c) at (4,-3);

\filldraw[fill=yellow, draw=black] (a) -- (b) -- (c) -- cycle;

\begin{scope}
\clip (a) -- (b) -- (c) -- cycle;
\end{scope}

%construct the clipping path for the half circle.  Make sure the whole arc
%we need fits inside the path

\coordinate (ab) at ($(a)!\radius!(b)$);
\coordinate (ac) at ($(a)!\radius!(c)$);
\coordinate (ab1) at ($(ab)!\radius!-90:(a)$);
\coordinate (ac1) at ($(ac)!\radius!90:(a)$);
%Is there any way how to make the choice of the 90 or -90 automatic here?

\begin{scope}
\clip (a) -- (ab) -- (ab1) -- ($(ab1)!.5!(ac1)$) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}


will produce

-

Here a solution without tkz-euclide with only tikz:

The next code is simple but it's difficult to work with it (add an arrow or a label)

\newcommand*\fillangle[4][]{%
\pgfkeys{/fillangle/.cd,
\pgfqkeys{/fillangle}{#1}
\begin{scope}
\clip (#2) -- (#3) -- (#4) --  cycle;
\end{scope}
}


Better but more complex is the next code. First we need to calculate the angles defined with three points. It's possible to use \pgfmathanglebetweenpoints. It's easy to add shading because an optional argument is authorized.

Todo

It would be better to use boolean and to avoid to create \fillhalfangle and \markangle. Only one macro is enough.

Interesting is to use a negative radius to get the opposed angle (last example)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}

\begin{document}

\pgfkeys{%
/fillangle/.cd,
/fillangle/.unknown/.code   =  {\let\searchname=\pgfkeyscurrentname
\pgfkeysalso{\searchname/.try=#1,
/tikz/\searchname/.retry=#1},\pgfkeysalso{\searchname/.try=#1,
/pgf/\searchname/.retry=#1}}
}

\newcommand*\fillangle[4][]{%
\pgfkeys{/fillangle/.cd,
\pgfqkeys{/fillangle}{#1}

\pgfmathanglebetweenpoints{\pgfpointanchor{#3}{center}}{%
\pgfpointanchor{#2}{center}}
\let\tmpai\pgfmathresult
\pgfmathanglebetweenpoints{\pgfpointanchor{#3}{center}}{%
\pgfpointanchor{#4}{center}}
\let\tmpaf\pgfmathresult
\draw[#1] (#3) -- ($(#3)!\fillangleradius!(#2)$) arc (\tmpai:\tmpaf:\fillangleradius) -- cycle;
}

\newcommand*\fillhalfangle[4][]{%
\pgfkeys{/fillangle/.cd,
\pgfqkeys{/fillangle}{#1}

\pgfmathanglebetweenpoints{\pgfpointanchor{#3}{center}}{%
\pgfpointanchor{#2}{center}}
\let\tmpai\pgfmathresult
\pgfmathanglebetweenpoints{\pgfpointanchor{#3}{center}}{%
\pgfpointanchor{#4}{center}}
\let\tmpaf\pgfmathresult

\draw[#1]
(#3) -- ($(#3)!\fillangleradius!(#2)$) arc (\tmpai:0.5*\tmpai+0.5*\tmpaf:\fillangleradius) -- cycle;
}

\newcommand*\markangle[4][]{%
\pgfkeys{/fillangle/.cd,
\pgfqkeys{/fillangle}{#1}

\pgfmathanglebetweenpoints{\pgfpointanchor{#3}{center}}{%
\pgfpointanchor{#2}{center}}
\let\tmpai\pgfmathresult
\pgfmathanglebetweenpoints{\pgfpointanchor{#3}{center}}{%
\pgfpointanchor{#4}{center}}
\let\tmpaf\pgfmathresult
\draw[#1] (#3) -- ($(#3)!\fillangleradius!(#2)$) arc (\tmpai:\tmpaf:\fillangleradius) -- cycle;
\draw[->] ($(#3)!\fillangleradius!(#2)$) arc (\tmpai:\tmpaf:\fillangleradius);
}

\begin{tikzpicture}
\coordinate (A) at (2,0);
\coordinate (B) at (-2,2);
\coordinate (C) at (-4,-1);

\filldraw[fill=yellow, draw=black] (A) -- (B) -- (C) -- cycle;
\end{tikzpicture}

\begin{tikzpicture}
\coordinate (A) at (3,-1);
\coordinate (C) at (-2,-1);

\filldraw[fill=yellow, draw=black] (A) -- (B) -- (C) -- cycle;
\end{tikzpicture}

\begin{tikzpicture}
\coordinate (B) at (-2,2);
\coordinate (C) at (-2,-2);

\draw[black] (A) -- (C) -- (B) ;

\end{tikzpicture}
\begin{tikzpicture}[>=latex']

\draw[black] (A) -- (C) -- (B) ;

\end{tikzpicture}
\end{document}


-

Here is two examples using tikz-euclide, I believe this solution is somewhat simpler

\documentclass{minimal}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}

\begin{tikzpicture}[scale=0.75]
\tkzInit[xmax=11,xmin=1,ymax=10,ymin=-4] \tkzClip
\tkzDefPoint(10,0){A}
\tkzDefPoint(2,9){B}
\tkzDefPoint(4,-3){C}
\tkzDrawPolygon[color=black,fill=yellow](A,B,C)
\tkzDefLine[bisector](B,A,C) \tkzGetPoint{c}
\tkzInterLL(B,C)(A,c) \tkzGetPoint{P}
\tkzMarkAngle[color=black,scale=1](B,A,C)
\end{tikzpicture}

\begin{tikzpicture}[scale=0.75]
\tkzInit[xmax=11,xmin=1,ymax=10,ymin=-4] \tkzClip
\tkzDefPoint(10,0){A}
\tkzDefPoint(2,9){B}
\tkzDefPoint(4,-3){C}
\tkzDrawPolygon[color=black,fill=yellow](A,B,C)
\tkzFindAngle(C,A,B) \tkzGetAngle{tkzang}
\tkzDefPointBy[rotation= center A angle {0.5*\tkzang+180} ](C) \tkzGetPoint{P}
\tkzMarkAngle[color=black,scale=1](B,A,C)