Graphics, Figures & TablesFilling for a custom Area

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
Bozack
Posts: 117
Joined: Wed Feb 06, 2008 4:21 pm

Filling for a custom Area

Post by Bozack »

I'm quite new to TikZ, and am trying to make a simple shape and fill it in with a color. As far as I understand, I can simply make a path, draw all the lines and curves, and then fill them as in the example code below:

Code: Select all

\documentclass[11pt]{article}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    \path [fill=Red, draw=Black, line width=1mm]
      (1.34,-3.95) -- (4.36,-1.51)
      arc [radius=3.77, start angle=201.24007, end angle=302.93512]
      (9.92,-3.33) -- (8.96,-2.69) -- (12.56,-1.37) -- (12.58,-5.09) -- (11.48,-4.35)
      arc [radius=6.57, start angle=318.5605, end angle=217.13745];
  \end{tikzpicture}
\end{document}
However, this results in a less than satisfactory result (see attachment). There is this weird line from the starting point to somewhere in the "pointy" end of the figure. Can you see what is wrong? :(
Attachments
result.png
result.png (7.82 KiB) Viewed 12309 times
OS, LaTeX-system, editor: Arch Linux 64bit, TeXlive, Kile | Windows 10 Professional 64bit, MikTeX 4.9, TeXnicCenter 2.02 64bit

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Klausiber
Posts: 16
Joined: Sun Apr 14, 2013 11:41 pm

Filling for a custom Area

Post by Klausiber »

Bozack wrote:As far as I understand, I can simply make a path, draw all the lines and curves, and then fill them as in the example code below
[...]
There is this weird line from the starting point to somewhere in the "pointy" end of the figure. Can you see what is wrong? :(
There are two things that contribute to your problem:

Whenever you tell TikZ to fill a path, this path needs to be closed, i.e. it must end at its starting point. When it does not, TikZ closes the path for you - this is the line you see.

Now you may wonder why your path is not closed and especially why the "closing line" starts after the first arc. The problem is the move-to operation you use there. The arc already ends in a point and yet you tell TikZ to move to another point (9.92,-3.33) - although this happens to be the same physical spot in this case - because you give it no line-to operation. This ends the path and starts a new one. I have no idea why the final filling operation works none the less.

Also note, that in your picture there's an ugly line joint at this very point. Maybe zoom in to see it better. This also indicates that TikZ does not see this a continued path as it would join the lines properly in this case. Have a look at the lower arc for reference.

Here's a corrected version:

Code: Select all

\documentclass[11pt]{article}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    \path [draw=black, fill=red, line width=1mm]
      (4.36,-1.51) arc [radius=3.77, start angle=201.24007, end angle=302.93512]
      -- (8.96,-2.69) -- (12.56,-1.37) -- (12.58,-5.09) -- (11.48,-4.35)
      arc [radius=6.57, start angle=318.5605, end angle=217.13745] -- cycle;
  \end{tikzpicture}
\end{document}
User avatar
Bozack
Posts: 117
Joined: Wed Feb 06, 2008 4:21 pm

Filling for a custom Area

Post by Bozack »

Klausiber wrote:... you may wonder why your path is not closed and espacially why the "closing line" starts after the first arc. The problem is the move-to operation you use there. The arc already ends in a point and yet you tell TikZ to move to another point (9.92,-3.33) - although this happens to be the same physical spot in this case - because you give it no line-to operation. This ends the path and starts a new one.
Thank you so much for that solution, now I understand! :)
OS, LaTeX-system, editor: Arch Linux 64bit, TeXlive, Kile | Windows 10 Professional 64bit, MikTeX 4.9, TeXnicCenter 2.02 64bit
Post Reply