2

I'm trying to achieve the following picture in TikZ:

a dilated polygon

Of course, I just did this in TikZ, but I did it this way:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        \draw (0,1) coordinate (A);
        \draw (1,1) coordinate (B);
        \draw (3,3) coordinate (C);
        \draw (3,4) coordinate (D);
        \draw (0,4) coordinate (E);
        \fill[fill=blue!30,draw=black,thick,rounded corners=20pt] ($(A)+(-20pt,-20pt)$) -- ($(B)+(8.284pt,-20pt)$) -- ($(C)+(20pt,-8.284pt)$) -- ($(D)+(20pt,20pt)$) -- ($(E)+(-20pt,20pt)$) -- cycle;
        \fill[fill=red!30,draw=red] (A) -- (B) -- (C) -- (D) -- (E) -- cycle;
    \end{tikzpicture}
\end{document}

What I really want is to just specify the coordinates of the red polygon and then draw a dilated version of it with rounded corners. In the end this is to highlight certain areas in a graph.

Any ideas and hints are appreciated!

  • 2
    I'm an asymptote guy, so this may sound dumb. Maybe you could... 1-define the red polygon, 2-draw the polygon with a large round black pen, 3-draw the polygon with a slightly smaller round blue pen, 4-fill the polygon with a red pen. – James Sep 2 '16 at 19:27
  • @James It doesn't sound dumb at all! That does the job for my purpose and is very easy to implement. – Christoph Sep 3 '16 at 7:33
4

Direct implementation in tikz:

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \path (0,1) coordinate (A) (1,1) coordinate (B) (3,3) coordinate (C)(3,4) coordinate (D) (0,4) coordinate (E);
  \draw[rounded corners, line width=40pt] (A) -- (B) -- (C) -- (D) -- (E) -- cycle;
  \draw[rounded corners, blue!30, line width=39pt] (A) -- (B) -- (C) -- (D) -- (E) -- cycle;
  \draw[red, fill=red!30] (A) -- (B) -- (C) -- (D) -- (E) -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
2

Here is James's idea implemented in Metapost (I use ConTeXt, but the same thing will work in LaTeX):

\starttext
\startMPpage[offset=2mm]
  path p;
  p := (0,1) -- (1,1) -- (3,3) -- (3,4) -- (0,4) -- cycle;
  p := p scaled 1cm;

  path q ;
  q := p cornered 10pt;

  draw q withcolor blue
      withpen pencircle scaled 21pt;

  draw q withcolor 0.3[white, blue]
      withpen pencircle scaled 20pt;

  fill p withcolor 0.3[white, red];
  draw p withcolor red;


\stopMPpage
\stoptext

which gives

enter image description here

| improve this answer | |
  • Just curious, what happens if you draw path p with each of your draw commands? I'm not sure why you need to define a path q. – James Sep 2 '16 at 22:17
  • @James: That would work as well. Defining a new path or now is just a matter of coding style. I could have used draw p cornered 10pt withpen ... instead of draw q .... – Aditya Sep 2 '16 at 23:53
  • But are you sure you need the cornered path? If the pen tip is round then you will automatically have rounded corners. – James Sep 3 '16 at 0:12
  • 1
    @James: interesting idea. I am not on my computer right now, but with a circular pen, the radius will be proportional to the dialation. One might want to use a smaller or a larger radius (as in the other answer) – Aditya Sep 3 '16 at 2:05
  • @Aditya This could be solved with a rounded square shape pen. Not sure if Metapost and/or TikZ provide such. – Christoph Sep 3 '16 at 21:14
1

Edit: New, more simple solution with use of transform canvas:

enter image description here

The MWE:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,positioning}

\begin{document}
    \begin{tikzpicture}
\coordinate                 (A) at (-1.5,-2);
\coordinate[above=4cm of A] (E);
\coordinate[right=3cm of E] (D);
\coordinate[below=1cm of D] (C);
\coordinate[right=1cm of A] (B);
%
\coordinate[right=2mm of B] (BC);
\coordinate[below=2mm of C] (CB);
%
\draw[draw=blue, fill=blue!30,thick,rounded corners=2mm,
      transform canvas={yscale=1.15,xscale=1.2}]
    (A) |- (D) -- (CB) -- (BC) --  cycle;
\draw[draw=red, fill=red!30] 
    (A) |- (D) -- (C) -- (B) -- cycle;

    \end{tikzpicture}
\end{document}
| improve this answer | |
  • This depends on the fact that you chose (A) such that the transformation is central. Also, the distance between blue and red will not be the same for all edges this way. – Christoph Sep 3 '16 at 13:34
  • exactly. better solution provide AmoAmmar, it is independent from transfromation ... – Zarko Sep 3 '16 at 14:06

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.