5

In a recent tutorial style answer for How can I draw these polygons with shaded regions?, I encountered some strange behavior with tikz. The code below is a slightly reduced version of the code from that answer. I attempted to reduce it even further, but that made the issue harder to see.

Here is what I get if I end the line below in black with -- cycle:

\draw (A1) -- (intersection-1) -- (A4) -- cycle;

enter image description here

Notice how the black lines go past the points at A1 and A4. But, by not using -- cycle and instead re-specifying the starting point as the ending point, I get better results for the A1:

\draw (A1) -- (intersection-1) -- (A4) -- (A1);

enter image description here

A way to fix this is below, but why is the behavior different if using --cycle or re-specifying the starting point as the end point?

References:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}

% Define all the points:
    \coordinate (A1) at (0,4);
    \coordinate (A2) at (3,6);
    \coordinate (A3) at (5,5);
    \coordinate (A4) at (6,3);
    \coordinate (A5) at (4.5,1.5);
    \coordinate (An) at (0,0);

% Draw the lines
    \draw [ultra thick, blue, solid ] (An) -- (A1) -- (A2) -- (A3) -- (A4) -- (A5);

% Label the nodes:
    \foreach \Index/\Position in {1/left, 2/above, 3/above right, 4/right, 5/below right, n/left} {
        \node [\Position] at (A\Index) {$A_{\Index}$};
    }%

    \draw [gray, ultra thick] 
            [name path=A1A3] (A1) -- (A3)
            [name path=A2A4] (A2) -- (A4)
            [name path=A1A4] (A1) -- (A4);


    \fill [fill=red!20, draw=black, ultra thick, name intersections={of=A1A3 and A2A4}]
        (A1) -- (intersection-1) -- (A4) -- cycle;% produces wacky vertices
        %(A1) -- (intersection-1) -- (A4) -- (A1);% this fixes problem at (A)

%    % Following fix works
%    \fill [fill=red!20, draw=black, line join=round, ultra thick, name intersections={of=A1A3 and A2A4}]
%       (A1) -- (intersection-1) -- (A4) -- cycle;

\end{tikzpicture}
\end{document}
7

From PGF manual, I found the following statement.

enter image description here

  • Well that was easy. I guess for the angles/line widths I have the left triangle from the manual works better. – Peter Grill Oct 19 '12 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.