9

Just a quick question. I'm trying to fill in a parabola shape in tikz. I've managed to make it work horizontally. However, when I do the same method vertically. There's a weird deformity that I can't solve. The image is below!

Image

This is the code to create this image:

\documentclass{article}

\usepackage{hyperref}
\usepackage[usenames,dvipsnames,x11names]{xcolor}
\usepackage{tikz}

\usetikzlibrary{positioning}
\usetikzlibrary{arrows,snakes,shapes}

\begin{document}

\begin{figure}[h] \label{Interesting_Pic}

\begin{tikzpicture}

\begin{scope} 
    %Grid and outer layers 
    \draw[black, very thick] (4,-0.5) rectangle (9,4.5);
    \draw[black, very thick] (4.5,0) rectangle (8.5,4);
    \draw[step=5mm,black] (4.5,0) grid (8.5,4);
    %diagonal lines in corner 
    \draw[fill=lime] (4,-0.5) -- (4.5,0);
    \draw[fill=lime] (4,4.5) -- (4.5,4);
    \draw[fill=lime] (9,-0.5) -- (8.5,0);
    \draw[fill=lime] (8.5,4) -- (9,4.5);
    %Labels lines in corners 
    \fill[font=\footnotesize]
        (4,-0.5) node [left] {$A_{4}$}
        (4,4.5) node [left] {$A_{1}$}
        (9,-0.5) node [right] {$A_{3}$}
        (9,4.5) node [right] {$A_{2}$};
    %Draw parabola bends
    \draw[bend left,->] (4.5,0) to node [auto] {} (8.5,0);
    \draw[bend right,->] (4.5,0) to node [auto] {} (4.5,4);
    \draw[bend left,->] (8.5,0) to node [auto] {} (8.5,4);
    \draw[bend left,->] (8.5,4) to node [auto] {} (4.5,4);
    %Colour in parabola areas
    \draw[fill=white] (8.5,0) -- (4.5,0) parabola bend (6.5,0.6) (8.5,0);
    \draw[fill=white] (8.5,0) -- (8.5,4) to node (7.1,2) {} (8.5,0);
    \draw[fill=white] (4.5,0) parabola bend (5.1,2) (4.5,4) -- (4.5,0);
    \draw[fill=white] (4.5,4) -- (8.5,4) parabola bend (6.5,3.4) (4.5,4);
\end{scope}



\end{tikzpicture}
\caption{An interesting diagram...}
\end{figure}

\end{document}

I tried two different methods for the left and right sides, (as seen by the %right side and %left side lines at the bottom) but neither worked correctly.

I am trying to do the same thing that I did with the curves at the top and bottom, but doing the same method inverts the parabola bend (as seen on the left hand side). Does anyone know of a way to make the left and right side the same as the top and bottom?

Cheers! :)

edit: Added entire code, so it will create the image above!

3
  • 1
    Welcome to TeX.se. Please don't post code fragments. Instead put the code into a complete compilable document that people can play with.
    – Alan Munn
    Commented May 4, 2017 at 2:16
  • I'll edit it now! Commented May 4, 2017 at 4:31
  • Completely unrelated: Always place the \label after (or within) the \caption, otherwise cross references wont work. See e.g. tex.stackexchange.com/questions/32325 Commented May 4, 2017 at 6:00

2 Answers 2

11

You've used bend left/bend right earlier in the drawing, use that again, and it works fine. For example \draw[fill=white] (8.5,0) to[bend right] (4.5,0);

This image shows output of your original code, with some style changes. The blue arrow is made with bend left, the dashed red line with parabola bend. They're not 100% equivalent, but very close.

enter image description here

Speaking of the arrow tips, do you want to make them larger (they're hardly visible)? Or remove them altogether?


Some other comments:

  • The hyperref package should in general be loaded late, see Which packages should be loaded after hyperref instead of before?
  • As I mentioned in a comment, you need to place the \label after the \caption, or cross references wont work, see Why does an environment's label have to appear after the caption?
  • Your scope environment doesn't really do anything useful.
  • I added \centering before \begin{tikzpicture}
  • In the block starting with %Draw Pin-Cushion Distortion, you have an empty node in each line, which isn't needed.
  • When you draw the lines between the corners, you also have fill=lime. Did you intend to fill any region with lime, or is that just something you've forgotten to remove?

That said, here is a complete example. Using named coordinates can be useful sometime, I rewrote the code to show one method using such. Of course, you don't have to use it.

enter image description here

\documentclass{article}

\usepackage[usenames,dvipsnames,x11names]{xcolor}
\usepackage{tikz}
\usepackage{hyperref}

\begin{document}

\begin{figure}[ht]
\centering
\begin{tikzpicture} %detector on right (Pin-Cushion distorted)

\begin{scope}[every label/.style={font=\footnotesize}]
    % coordinates at outer corners
    \coordinate [label=left:$A_1$]  (A1) at (4,4.5);
    \coordinate [label=right:$A_2$] (A2) at (9,4.5);
    \coordinate [label=right:$A_3$] (A3) at (9,-0.5);
    \coordinate [label=left:$A_4$]  (A4) at (4,-0.5);
\end{scope}

    % inner corners, relative to outer corners
    \path (A1) ++(0.5,-0.5)  coordinate (B1);
    \path (A2) ++(-0.5,-0.5) coordinate (B2);
    \path (A3) ++(-0.5,0.5)  coordinate (B3);
    \path (A4) ++(0.5,0.5)   coordinate (B4);

    %Grid and outer layers of detector 
    \draw[black, very thick] (A4) rectangle (A2);
    \draw[black, very thick] (B4) rectangle (B2);
    \draw[step=5mm,black] (B4) grid (B2);

    %Anode wiring 
    \foreach \i in {1,...,4}
       \draw (A\i) -- (B\i);

    % draw and fill bendy lines
    \draw[bend left,fill=white]  (B4) to (B3);
    \draw[bend right,fill=white] (B4) to (B1);
    \draw[bend left,fill=white]  (B3) to (B2);
    \draw[bend left,fill=white]  (B2) to (B1);

\end{tikzpicture}
\caption{An interesting diagram...}
\label{PCDistort}
\end{figure}

\end{document}
5
  • you have not used the parabola bend which was asked for. Does it make a difference whether one uses a parabola bend or a bend left/bend right?
    – jakun
    Commented May 4, 2017 at 6:59
  • @jakun Looked like it didn't make a difference, but I may be wrong. Note also that he has already drawn those lines using bend left/right. Commented May 4, 2017 at 7:13
  • @jakun See the image I just added. Commented May 4, 2017 at 7:53
  • Thank you for all the help! I'm not too fussed with whether parabola bend is used or not. Also thanks for all the others tips as well. I'd like to remove the arrows. I just want lines. So I'd just put \draw[bend left, --] ? Commented May 4, 2017 at 18:39
  • @AlphaBetaGamma96 Right, then you can do drawing and filling in one step, will update the code in a bit. Commented May 4, 2017 at 18:39
10

It is possible to draw the parabola bend horizontally and then rotate it. I have named the coordinates to make things easier for me:

\documentclass{article}

\usepackage{hyperref}
\usepackage[usenames,dvipsnames,x11names]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows,snakes,shapes}

\begin{document}
\begin{tikzpicture}
    % Coordinates
    \coordinate (sw) at (4.5, 0);
    \coordinate (ne) at ([shift=(sw)] 4, 4);
    \coordinate (nw) at (sw |- ne);
    \coordinate (se) at (sw -| ne);

    \coordinate[label=left:$A_1$] (A1) at ([shift=(nw)] -.5, .5);
    \coordinate[label=right:$A_2$] (A2) at ([shift=(ne)] .5, .5);
    \coordinate[label=right:$A_3$] (A3) at ([shift=(se)] .5, -.5);
    \coordinate[label=left:$A_4$] (A4) at ([shift=(sw)] -.5, -.5);

    % Grid and outer layers of detector 
    \draw[black, very thick] (A1) rectangle (A3);
    \draw[black, very thick] (sw) rectangle (ne);
    \draw[step=5mm,black] (sw) grid (ne);

    % Anode wiring 
    \draw (A1) -- (nw);
    \draw (A2) -- (ne);
    \draw (A3) -- (se);
    \draw (A4) -- (sw);

    %Colour in distorted areas
    \draw[fill=white] (sw) parabola bend +(2, .6) (se) -- cycle;
    \draw[fill=white] (nw) parabola bend +(2, -.6) (ne) -- cycle;
    \begin{scope}[shift=(sw), rotate=90]
        \draw[fill=white] (0,0) parabola bend +(2, -.6) ++(4, 0) -- cycle;
    \end{scope}
    \begin{scope}[shift=(se), rotate=90]
        \draw[fill=white] (0,0) parabola bend +(2, .6) ++(4, 0) -- cycle;
    \end{scope}
\end{tikzpicture}
\end{document}

screenshot

2
  • Thank you jakun! It looks fantastic! Just one quick question. With the lines which end in '-- cycle'. Does that just cycle around the 3 points earlier to create a shape which is then filled in? Commented May 4, 2017 at 18:37
  • 1
    @AlphaBetaGamma96 "The --cycle causes the current path to be closed (actually the current part of the current path) by smoothly joining the first and last point." see tikz documentation page 36/37.
    – jakun
    Commented May 5, 2017 at 5:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .